多線程之線程通訊條件Condition二,多線程condition

來源:互聯網
上載者:User

多線程之線程通訊條件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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.