Next, the implementation of the condition three conditions, there is such an application:
1, there are three processes, the first process executes 1 times, the second process executes 2 times, the third process executes 3 times;
2, first execute the second process, then the first one, then the third one;
3. Execute 5 cycles sequentially.
Analysis:
At this time if the wait and notify with object is not realized, we can use the lock lock condition implementation, we need to define three signal conditions, respectively, control the three processes.
The implementation is as follows:
Package Andy.thread.test;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.Lock; Import java.util.concurrent.locks.reentrantlock;/** * @author zhang,tianyou * @version November 9, 2014 PM 12:12:44 */public Class Threethreadcondition {static A tasks = new A ();p ublic static void Main (string[] args) {//Line 2 line new thread (new RUNNABL E () {@Overridepublic void Run () {for (int i = 1; I <= 5; i++) {//loop executes 5 times tasks.sub2 (i);}}). Start ();//Line 3 line new Thread (new Runnable () {@Overridepublic void Run () {for (int i = 1; I <= 5; i++) {//Loop execution 5 times TASKS.SUB3 (i);}}). Start ();//main thread instead of line Line 1 for (int i = 1; I <= 5; i++) {//Loop execution 5 times tasks.sub1 (i);}} Static class A {lock lock = new Reentrantlock (); Condition condition1 = Lock.newcondition (); Condition condition2 = Lock.newcondition (); Condition Condition3 = Lock.newcondition ();//First Execute line Line 2 private int execunum = 2;public void sub2 (int i) {lock.lock (); try {// If 2 is not blocked wait while (execunum! = 2) {try {condition2.await ()} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} for (int j = 1; J <= 2; j + +) {System.out.println ("Sub2 thread sequence of" + j+ ", task is" + i);} The execution is given to 1 thread execunum = 1;condition1.signal ();} finally {Lock.unlock ();}} public void sub1 (int i) {lock.lock (); try {//not 1 blocks wait while (execunum! = 1) {try {condition1.await ()} catch (Interruptede Xception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} System.out.println ("Sub1 thread sequence of" + 1+ ", task is" + i);//finish handing over to 3 thread execunum = 3;condition3.signal ();} finally {Lock.unlock ();}} public void sub3 (int i) {lock.lock (); try {//not 2 blocks wait while (execunum! = 3) {try {condition3.await ()} catch (Interrupted Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} for (int j = 1; J <= 3; j + +) {System.out.println ("SUB3 thread sequence of" + j+ ", task is" + i);} The execution is given to 1 thread execunum = 2;condition2.signal ();} finally {Lock.unlock ();}}}}
The results of the operation are as follows:
SUB2 thread sequence of 1, task is 1SUB2 thread sequence of 2, task was 1SUB1 thread sequence of 1, task is 1SUB3 thread SE Quence of 1, task is 1SUB3 thread sequence of 2, task was 1SUB3 thread sequence of 3, task is 1SUB2 thread sequence of 1, t Ask is 2SUB2 thread sequence of 2, task is 2SUB1 thread sequence of 1, task was 2SUB3 thread sequence of 1, task is 2SUB3 t Hread sequence of 2, task is 2SUB3 thread sequence of 3, task was 2SUB2 thread sequence of 1, task is 3SUB2 thread sequence of 2, task is 3SUB1 thread sequence of 1, task is 3SUB3 thread sequence of 1, task was 3SUB3 thread sequence of 2, task is 3SUB3 thread sequence of 3, task is 3SUB2 thread sequence of 1, task was 4SUB2 thread sequence of 2, task is 4SUB1 thread Sequence of 1, task is 4SUB3 thread sequence of 1, task was 4SUB3 thread sequence of 2, task is 4SUB3 thread sequence of 3, Task is 4SUB2 thread sequence of 1, task is 5SUB2 thread sequence of 2, task was 5SUB1 thread sequence of 1, task is 5SUB3 Thread sequence of 1, tAsk is 5SUB3 thread sequence of 2, task was 5SUB3 thread sequence of 3, task is 5
Multi-thread communication condition condition two