About Java multithreading concurrency control, in addition to the built-in keyword synchronized, there is a Lock,lock implementation class is reetrantlock,lock can achieve more flexible multithreaded concurrency control.
How to use, raise a chestnut
public class Reetrantlockdemo {static final lock lock = new Reentrantlock ();
static Runnable Runnable1 = new Runnable () {@Override public void run () {lock.lock (); try {for (int i = 0; i < 5; i++) {System.out.println ("Runnable1 running")
+ i);
finally {Lock.unlock ();
}
}
};
static Runnable Runnable2 = new Runnable () {@Override public void run () {lock.lock (); try {for (int i = 0; i < 5; i++) {System.out.println ("Runnable2 running")
+ i);
finally {Lock.unlock ();
}
}
};
public static void Main (string[] args) {new Thread (Runnable1). Start ();
New Thread (Runnable2). Start (); }
}
This is the most basic use of lock, using the lock and unlock method to lock the code piece to understand the lock, the effect of the same as the synchronized, two threads serialized execution, the console output is as follows:
Runnable1 running 0
Runnable1 running 1
Runnable1 running 2
Runnable1 running 3
Runnable1 Running 4
Runnable2 running 0
Runnable2 running 1
Runnable2 running 2
Runnable2 running 3
Runnable2 Running 4
That means synchronized can do the lock, so see what lock can do. It also provides the Trylock method, which means that I will try to acquire the lock, but if the lock fails, it will not cause the current thread to block, skip directly, and change the chestnuts slightly.
public class Reetrantlockdemo {static final lock lock = new Reentrantlock ();
static Runnable Runnable1 = new Runnable () {@Override public void run () {lock.lock (); try {for (int i = 0; i < 5; i++) {System.out.println ("Runnable1 running")
+ i);
finally {Lock.unlock ();
}
}
}; static Runnable Runnable2 = new Runnable () {@Override public void run () {if (Lock.trylock ()) {try {for (int i = 0; i < 5; i++) {System.out.println ("
Runnable2 Running "+ i);
finally {Lock.unlock ();
} else {System.out.println ("Can not get the lock, skip running");
}
}
}; public static void Main (string[] args) {new THread (Runnable1). Start ();
New Thread (Runnable2). Start (); }
}
Try to acquire the lock in thread 2, skip it if it is not available, and the console output is as follows:
Runnable1 running 0
Can not get the lock, skip running
Runnable1 running 1
Runnable1 running 2
Runnable1 running 3
Runnable1 Running 4
In addition, the Trylock method can also set the wait time, wait for the number of time after the lock is skipped. When constructing Reetrantlock, you can set a Boolean value, the new Reetrantlock (True/false), to choose whether or not to construct a "fair lock", the so-called fair lock is to have all waiting threads to get the current lock in their waiting order, Prevent some threads from being executed for a long time, but pay attention to the need for some extra computational overhead to implement a fair lock.
Lock also has a function is condition, simple is the condition set. Take a chestnut, like the top 2 threads, the requirement is the first thread to loop to 3 when the second thread starts, when the first thread stops until the second thread executes to 3. So here are 2 conditions:
1. The second thread needs to be started when the first thread executes to 3, using Condition1 to represent
2. The first thread needs to be restarted when the second thread executes to 3. To express by Condition2
public class Reetrantlockdemo {static final lock lock = new Reentrantlock ();
static Boolean thread1arrive3 = false;
static Boolean thread2arrive3 = false;
Static final Condition Condition1 = Lock.newcondition ();
Static final Condition condition2 = Lock.newcondition ();
static Runnable Runnable1 = new Runnable () {@Override public void run () {lock.lock (); try {for (int i = 0; i < 5; i++) {System.out.println ("Runnable1 running")
+ i);
if (i==3) {thread1arrive3=true;
Condition1.signal ();
if (!thread2arrive3) {condition2.await (); catch (Exception e) {e.printstackt
Race ();
finally {Lock.unlock ();
}
}
}; static Runnable Runnable2 = new Runnable () {@Override public void run () {Lock.lock ()
;
try {if (!thread1arrive3) {condition1.await (); for (int i = 0; i < 5; i++) {System.out.println ("Runnable2 running" + I
);
if (i==3) {thread2arrive3=true;
Condition2.signal ();
A catch (Exception e) {e.printstacktrace ());
finally {Lock.unlock ();
}
}
};
public static void Main (string[] args) {new Thread (Runnable1). Start ();
New Thread (Runnable2). Start (); }
}
Run Result:
Runnable1 running 0
Runnable1 running 1
Runnable1 running 2
Runnable1 running 3
Runnable2 running 0
Runnable2 running 1
Runnable2 running 2
Runnable2 running 3
Runnable2 Running 4
Runnable1 Running 4
The condition await method allows the current thread to release the lock and wait on the specified condition, while the signal method notifies the thread waiting on the condition to acquire the lock and continue execution. Condition actually maintains the data structure of a queue, await equivalent to put in the queue, and signal is removed from the queue. Therefore, it is necessary to note that if there is no thread in this condition when executing signal, the Await,singal method will not block. Therefore, it is necessary to pay attention to the use of condition must be satisfied when the conditions to do await and signal (the example code using 2 Boolean variables as a condition of determination), if the signal first executed, await after the execution of the direct deadlock.