Learn about Condition thread communication and condition thread through an interview question
Condition
- The Condition interface describes the conditional variables that may be associated with the lock. These variables are used in a similar way to the implicit monitor accessed using Object. wait, but provide more powerful functions. Note that a single Lock may be associated with multiple Condition objects. To avoid compatibility problems, the name of the Condition method is different from that of the corresponding Object version;
- In the Condition object, await, signal, and signalAll correspond to the wait, notify, and policyall methods;
- The Condition instance is essentially bound to a lock. To obtain a Condition instance for a specific Lock instance, use the newCondition () method.
Interview Questions
1 package com. ccfdod. juc; 2 3 import java. util. concurrent. locks. condition; 4 import java. util. concurrent. locks. lock; 5 import java. util. concurrent. locks. reentrantLock; 6 7/** 8 * question: write A program and enable three threads. the IDs of these three threads are A, B, and C, each thread prints its ID 10 times on the screen, and the output result must be displayed in order. 9 * For example, ABCABCABC ...... 10 */11 class AlternateDemo {12 // indicates the current thread output, 1 indicates A, 2 indicates B, and 3 indicates C 13 private int number = 1; 14 15 Lock lock = new ReentrantLock (); 16 17 // The Power of Condition is that it can create different Condition 18 Condition condition1 = lock for multiple threads. newCondition (); 19 Condition condition2 = lock. newCondition (); 20 Condition condition3 = lock. newCondition (); 21 22 // loopNum: current number of rounds 23 public void loopA (int loopNum) {24 // lock 25 lock. lock (); 26 try {27 while (number! = 1) {28 // wait for 29 condition1.await (); 30} 31 32 System. out. println (Thread. currentThread (). getName () + ", currentLoopNum is" + loopNum); 33 number = 2; 34 // wake up 35 condition2.signal (); 36 37} catch (InterruptedException e) {38 e. printStackTrace (); 39} finally {40 // make sure to release the lock 41 lock. unlock (); 42} 43} 44 45 public void loopB (int loopNum) {46 lock. lock (); 47 try {48 while (number! = 2) {49 condition2.await (); 50} 51 52 System. out. println (Thread. currentThread (). getName () + ", currentLoopNum is" + loopNum); 53 number = 3; 54 condition3.signal (); 55 56} catch (InterruptedException e) {57 e. printStackTrace (); 58} finally {59 lock. unlock (); 60} 61} 62 63 public void loopC (int loopNum) {64 lock. lock (); 65 try {66 while (number! = 3) {67 condition3.await (); 68} 69 70 System. out. println (Thread. currentThread (). getName () + ", currentLoopNum is" + loopNum); 71 number = 1; 72 condition1.signal (); 73 74} catch (InterruptedException e) {75 e. printStackTrace (); 76} finally {77 lock. unlock (); 78} 79} 80} 81 82 public class TestABCAlternate {83 84 public static void main (String [] args) {85 AlternateDemo ad = new AlternateDemo (); 86 87 new Thread (new Runnable () {88 89 @ Override 90 public void run () {91 for (int I = 0; I <10; I ++) 92 ad. loopA (I); 93} 94}, ""). start (); 95 96 new Thread (new Runnable () {97 98 @ Override 99 public void run () {100 for (int I = 0; I <10; I ++) 101 ad. loopB (I); 102} 103}, "B "). start (); 104 105 new Thread (new Runnable () {106 107 @ Override108 public void run () {109 for (int I = 0; I <10; I ++) 110 ad. loopC (I); 111} 112}, "C "). start (); 113} 114}