java控制多线程执行顺序

[java]  view plain copy print ?
  1. package com.yihaomen.mybatis.inter;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. public class TestThread {  
  6.   
  7.     public static void main(String[] args) throws IOException {  
  8.         final Test obj = new Test();  
  9.   
  10.         new Thread() {  
  11.             public void run() {  
  12.                 obj.m1();  
  13.             }  
  14.         }.start();  
  15.         new Thread() {  
  16.             public void run() {  
  17.                 obj.m2();  
  18.             }  
  19.         }.start();  
  20.         new Thread() {  
  21.             public void run() {  
  22.                 obj.m3();  
  23.             }  
  24.         }.start();  
  25.   
  26.     }  
  27.   
  28. }  
  29.   
  30. class Test {  
  31.     static int count;  
  32.     volatile int target = 1;  
  33.   
  34.     synchronized void m1() {  
  35.         for (int i = 0; i < 10; i++) {  
  36.             while (target == 2 || target == 3) {  
  37.                 try {  
  38.                     wait();  
  39.                 } catch (InterruptedException e) {  
  40.                     e.printStackTrace();  
  41.                 }  
  42.             }  
  43.             System.out.println("m1() =" + i);  
  44.             target = 2;  
  45.             notifyAll();  
  46.         }  
  47.     }  
  48.   
  49.     synchronized void m2() {  
  50.         for (int i = 0; i < 10; i++) {  
  51.             while (target == 1 || target == 3) {  
  52.                 try {  
  53.                     wait();  
  54.                 } catch (InterruptedException e) {  
  55.                     e.printStackTrace();  
  56.                 }  
  57.             }  
  58.             System.out.println("m2() =" + i);  
  59.             target = 3;  
  60.             notifyAll();  
  61.         }  
  62.     }  
  63.   
  64.     synchronized void m3() {  
  65.         for (int i = 0; i < 10; i++) {  
  66.             while (target == 1 || target == 2) {  
  67.                 try {  
  68.                     wait();  
  69.                 } catch (InterruptedException e) {  
  70.                     e.printStackTrace();  
  71.                 }  
  72.             }  
  73.             System.out.println("m3() =" + i);  
  74.             target = 1;  
  75.             notifyAll();  
  76.         }  
  77.     }  
  78. }  

输出结果:

[java]  view plain copy print ?
  1. m1() =0  
  2. m2() =0  
  3. m3() =0  
  4. m1() =1  
  5. m2() =1  
  6. m3() =1  
  7. m1() =2  
  8. m2() =2  
  9. m3() =2  
  10. m1() =3  
  11. m2() =3  
  12. m3() =3  
  13. m1() =4  
  14. m2() =4  
  15. m3() =4  
  16. m1() =5  
  17. m2() =5  
  18. m3() =5  
  19. m1() =6  
  20. m2() =6  
  21. m3() =6  
  22. m1() =7  
  23. m2() =7  
  24. m3() =7  
  25. m1() =8  
  26. m2() =8  
  27. m3() =8  
  28. m1() =9  
  29. m2() =9  
  30. m3() =9  

参考:http://www.blogjava.net/santicom/archive/2011/09/02/357783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值