如何实现主线程需要等待子线程的结果,然后才能往下执行。

如何实现主线程需要等待子线程的结果,然后才能往下执行。

1.      使用FLAG标志位

[java] view plaincopy

  1. public class TestSync {  

  2.   

  3.     private static int FLAG = 0;  

  4.   

  5.     public static void main(String[] args) {  

  6.   

  7.         Thread1 thread1 = new Thread1();  

  8.         System.out.println("thread 1 is start");  

  9.         thread1.start();  

  10.           

  11.         while (FLAG != 1) {  

  12. //          System.out.println("main thread is waiting");  

  13.         }  

  14.           

  15.         FLAG = 0;  

  16.         System.out.println("main thread is back to work");  

  17.           

  18.           

  19.     }  

  20.   

  21.     static class Thread1 extends Thread {  

  22.   

  23.         int total;  

  24.   

  25.         public void run() {  

  26.   

  27.             System.out.println("thread 1 is running");  

  28.   

  29.             for (int i = 0; i < 5; i++) {  

  30.                 total = total + i;  

  31.   

  32.                 System.out.println("total = " + total);  

  33.             }  

  34.   

  35.             System.out.println("thread 1 is over and set flag to 1");  

  36.             FLAG = 1;  

  37.   

  38.         }  

  39.     }  

  40.   

  41.   

  42. }  


PS: 在这个期间,做一些跟thread1不相关的事情 还是可以的哦

[java] view plaincopy

  1. while (FLAG != 1) {  

  2. //          System.out.println("main thread is waiting");  

  3.         }  


[java] view plaincopy

  1.   

输出结果:

2.join(),理解成等待子线程终止,可用于等待一个或多个子线程的结果,然后主线程再继续执行。

[java] view plaincopy

  1. public class TestSync {  

  2.   

  3.       

  4.   

  5.     public static void main(String[] args) throws InterruptedException {  

  6.   

  7.         for(int i = 0; i<5 ;i++){  

  8.         Thread1 thread1 = new Thread1();  

  9.         System.out.println("thread " + i +"  is start");  

  10.         thread1.start();  

  11.         thread1.join();  

  12.         }  

  13.           

  14.         System.out.println("main thread is back to work");  

  15.           

  16.     }  

  17.   

  18.     static class Thread1 extends Thread {  

  19.   

  20.   

  21.         public void run() {  

  22.   

  23.             System.out.println("thread  is running");  

  24.   

  25.         }  

  26.     }  

  27.   

  28.       

  29. }  


输出结果:



3. CountDownLatch 这个原理应该是通过检查子线程的个数来实现的


[java] view plaincopy

  1. import java.util.concurrent.CountDownLatch;  

  2.   

  3. public class TestSync {  

  4.   

  5.     private static CountDownLatch countdownLatch = new CountDownLatch(5);  

  6.       

  7.     public static void main(String[] args) throws InterruptedException {  

  8.   

  9.       

  10.   

  11.         for (int i = 0; i < 5; i++) {  

  12.             Thread1 thread1 = new Thread1();  

  13.             System.out.println("thread " + i + "  is start");  

  14.             thread1.start();  

  15.         }  

  16.   

  17.         //用于等待  

  18.         countdownLatch.await();  

  19.   

  20.         System.out.println("main thread is back to work");  

  21.   

  22.           

  23.     }  

  24.   

  25.     static class Thread1 extends Thread {  

  26.   

  27.         public void run() {  

  28.             System.out.println("thread  is running");  

  29.             //执行完一个用于减少子线程计数的  

  30.             countdownLatch.countDown();  

  31.         }  

  32.     }  

  33.   

  34. }  


输出结果:



PS: 通过输出结果发现,join()的话,貌似子线程们是串行执行的啊。。。会影响效率的吧。。。


转载于:https://my.oschina.net/forMemory/blog/496920

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值