多線程之線程通訊條件Condition二,多線程condition
接上一篇,實現Condition三個條件,有這樣一個應用:
1、 有三個進程,第一個進程執行1次,第二個進程執行2次,第三個進程執行3次;
2、 先執行第二個進程,然後第一個,然後第三個;
3、 依次執行5次迴圈。
分析:
此時若用Object的wait和notify是實現不了的,我們可以用Lock鎖的Condition實現,我們需要定義三個訊號條件,分別控制這三個進程。
實現如下:
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 2014年11月9日 下午12:12:44 */public class ThreeThreadCondition {static A tasks = new A();public static void main(String[] args) {// 2號線程new Thread(new Runnable() {@Overridepublic void run() {for (int i = 1; i <= 5; i++) {// 迴圈執行5次tasks.sub2(i);}}}).start();// 3號線程new Thread(new Runnable() {@Overridepublic void run() {for (int i = 1; i <= 5; i++) {// 迴圈執行5次tasks.sub3(i);}}}).start();// 主線程代替1號線程for (int i = 1; i <= 5; i++) {// 迴圈執行5次tasks.sub1(i);}}static class A {Lock lock = new ReentrantLock();Condition condition1 = lock.newCondition();Condition condition2 = lock.newCondition();Condition condition3 = lock.newCondition();// 先執行2號線程private int execuNum = 2;public void sub2(int i) {lock.lock();try {// 若不是2 則阻塞等待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);}// 執行完 交給1線程execuNum = 1;condition1.signal();} finally {lock.unlock();}}public void sub1(int i) {lock.lock();try {// 若不是1則阻塞等待while (execuNum != 1) {try {condition1.await();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("sub1 thread sequence of " + 1+ ", task is " + i);// 執行完 交給3線程execuNum = 3;condition3.signal();} finally {lock.unlock();}}public void sub3(int i) {lock.lock();try {// 若不是2 則阻塞等待while (execuNum != 3) {try {condition3.await();} catch (InterruptedException 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);}// 執行完 交給1線程execuNum = 2;condition2.signal();} finally {lock.unlock();}}}}
運行結果如下:
sub2 thread sequence of 1, task is 1sub2 thread sequence of 2, task is 1sub1 thread sequence of 1, task is 1sub3 thread sequence of 1, task is 1sub3 thread sequence of 2, task is 1sub3 thread sequence of 3, task is 1sub2 thread sequence of 1, task is 2sub2 thread sequence of 2, task is 2sub1 thread sequence of 1, task is 2sub3 thread sequence of 1, task is 2sub3 thread sequence of 2, task is 2sub3 thread sequence of 3, task is 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 is 3sub3 thread sequence of 2, task is 3sub3 thread sequence of 3, task is 3sub2 thread sequence of 1, task is 4sub2 thread sequence of 2, task is 4sub1 thread sequence of 1, task is 4sub3 thread sequence of 1, task is 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 is 5sub1 thread sequence of 1, task is 5sub3 thread sequence of 1, task is 5sub3 thread sequence of 2, task is 5sub3 thread sequence of 3, task is 5