We explain the use of condition using a practical example:
We're going to print 1 to 9 of these 9 numbers, which are printed by a thread first, then 4,5,6 by the B thread and then printed by a thread 7,8,9. There are many solutions to this problem, and now we use condition to do the problem (the solution of the Wait,notify method using object here).
Package Cn.xband.locks;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import Java.util.concurrent.locks.reentrantlock;public class Test{static class Numberwrapper {public int value = 1;} public static void Main (string[] args) {//Initialize Reentrant lock final lock lock = new Reentrantlock ();//First condition when the screen is output to 3final Condition REAC Hthreecondition = Lock.newcondition ();//The second condition when the screen output to 6final Condition reachsixcondition = lock.newcondition ();// Numberwrapper is just to encapsulate a number, one side can share the number object, and can be set to final//note here do not use Integer, Integer is immutable object final Numberwrapper num = new Numberwrapper ();//Initialize a thread thread Threada = new Thread (new Runnable () {@Overridepublic void Run () {//need to obtain lock lock.lock first (); try {System.out.println ("Threada start Write"),//a thread first outputs the first 3 digits while (Num.value <= 3) {System.out.println (num.value); num.value++;} Output to 3 o'clock to signal, tell B thread can start reachthreecondition.signal ();} finally {Lock.unlock ();} Lock.lock (); try {//wait for the condition of output 6 reachsixcondition.await (); System.out.println ("Threada start Write");//output remaining number while (nUm.value <= 9) {System.out.println (num.value); num.value++;}} catch (Interruptedexception e) {e.printstacktrace ();} finally {Lock.unlock ();}}}); Thread threadb = new Thread (new Runnable () {@Overridepublic void run () {try {lock.lock (); while (Num.value <= 3) {//wait 3 to lose The signal reachthreecondition.await ();}} catch (Interruptedexception e) {e.printstacktrace ();} finally {Lock.unlock ();} try {lock.lock ();//has received a signal to start outputting 4,5,6system.out.println ("threadb start write"); while (Num.value <= 6) { System.out.println (num.value); num.value++;} 4,5,6 output finished, tell a thread 6 output is finished reachsixcondition.signal ();} finally {Lock.unlock ();}}); /Start two threads Threadb.start (); Threada.start ();}}
There is a complete comment in the above code, please refer to the note to understand the use of condition.
The basic idea is to first a line enters upgradeable write, this time the B thread should wait for the reachthredcondition signal, and when a thread finished writing 3 through signal tell B thread "I wrote to 3, it's You", At this time a thread to wait for the reachsixcondition signal, while the B thread is notified, began to write 4,5,6, write 4,5,6 after the B thread notify a thread reachsixcondition condition is set up, when a thread began to write the remaining 7,8,9.
Java concurrency control: reentrantlock condition use