Condition
- The condition interface describes the condition variables that may be associated with a lock. These variables are similar in usage to implicit monitors that use object.wait access, but provide more powerful functionality. It should be noted that a single lock may be associated with multiple condition objects. To avoid compatibility issues, the name of the condition method differs from the corresponding object version;
- In the condition object, the await, signal, and signalall correspond to the wait, notify, and Notifyall methods respectively;
- The condition instance is essentially bound to a lock. To obtain an condition instance for a particular lock instance, use the Newcondition () method.
Interview questions
1 PackageCom.ccfdod.juc;2 3 Importjava.util.concurrent.locks.Condition;4 ImportJava.util.concurrent.locks.Lock;5 ImportJava.util.concurrent.locks.ReentrantLock;6 7 /**8 * Title: Write a program to open 3 threads, the IDs of the three threads are a, B, C, each thread prints its own ID on the screen 10 times, and requires the output to be displayed sequentially. 9 * such as: Abcabcabc ...Ten */ One classAlternatedemo { A //flag is currently output by which thread, 1 represents a,2 on behalf of b,3 for C - Private intNumber = 1; - theLock lock =NewReentrantlock (); - - //the power of condition is that it can create different condition for multiple threads -Condition Condition1 =lock.newcondition (); +Condition Condition2 =lock.newcondition (); -Condition Condition3 =lock.newcondition (); + A //Loopnum: Current number of circular wheels at Public voidLoopA (intloopnum) { - //locked - Lock.lock (); - Try { - while(Number! = 1) { - //wait in condition1.await (); - } to +System.out.println (Thread.CurrentThread (). GetName () + ", Currentloopnum is" +loopnum); -Number = 2; the //Wake up * condition2.signal (); $ Panax Notoginseng}Catch(interruptedexception e) { - e.printstacktrace (); the}finally { + //make sure to release the lock A Lock.unlock (); the } + } - $ Public voidLOOPB (intloopnum) { $ Lock.lock (); - Try { - while(Number! = 2) { the condition2.await (); - }Wuyi theSystem.out.println (Thread.CurrentThread (). GetName () + ", Currentloopnum is" +loopnum); -Number = 3; Wu condition3.signal (); - About}Catch(interruptedexception e) { $ e.printstacktrace (); -}finally { - Lock.unlock (); - } A } + the Public voidLOOPC (intloopnum) { - Lock.lock (); $ Try { the while(Number! = 3) { the condition3.await (); the } the -System.out.println (Thread.CurrentThread (). GetName () + ", Currentloopnum is" +loopnum); inNumber = 1; the condition1.signal (); the About}Catch(interruptedexception e) { the e.printstacktrace (); the}finally { the Lock.unlock (); + } - } the }Bayi the Public classTestabcalternate { the - Public Static voidMain (string[] args) { -Alternatedemo AD =NewAlternatedemo (); the the NewThread (NewRunnable () { the the @Override - Public voidrun () { the for(inti = 0; I < 10; i++) the Ad.loopa (i); the }94}, "A"). Start (); the the NewThread (NewRunnable () { the 98 @Override About Public voidrun () { - for(inti = 0; I < 10; i++)101 AD.LOOPB (i);102 }103}, "B"). Start ();104 the NewThread (NewRunnable () {106 107 @Override108 Public voidrun () {109 for(inti = 0; I < 10; i++) the Ad.loopc (i);111 } the}, "C"). Start ();113 } the}
Understanding condition thread communication through a face question