多线程例子:join

来源:互联网 发布:淘宝网非主流女装 编辑:程序博客网 时间:2024/05/29 19:46
Java代码  收藏代码
  1. package sure;  
  2.   
  3. import java.util.Random;  
  4.   
  5. public class MultThread {    
  6.   
  7.         public static void main(String[] args) {  
  8.             System.out.println("in " + Thread.currentThread().getName());  
  9.             long start = System.currentTimeMillis();  
  10.             CounterThread[] ct = new CounterThread[3];  
  11.             for (int i = 0; i < ct.length; i++) {  
  12.                 ct[i] = new CounterThread();  
  13.                 ct[i].start();  
  14. //              try {  
  15. //                  ct[i].join();  
  16. //              } catch (InterruptedException e) {  
  17. //                  e.printStackTrace();  
  18. //              }  
  19.             }  
  20.             long end = System.currentTimeMillis();  
  21.             System.out.println("join total time = " + (end - start));  
  22.             int result = 0;  
  23.             for (int j = 0; j < ct.length; j++) {  
  24.                 result += ct[j].getResult();  
  25.             }  
  26.             System.out.println("the result is " + result);  
  27.   
  28.         }  
  29.   
  30.     }  
  31.   
  32.     class CounterThread extends Thread {  
  33.   
  34.         public CounterThread() {  
  35.         }  
  36.   
  37.         private int result;  
  38.   
  39.         public int getResult() {  
  40.             return result;  
  41.         }  
  42.   
  43.         public void run() {  
  44.             try {  
  45.                 int time = (new Random().nextInt() >>> 1) % 5000;  
  46.                 Thread.sleep(time);  
  47.                 System.out.println(Thread.currentThread().getName()  
  48.                         + " is blocked for " + time + "ms");  
  49.             } catch (InterruptedException ex) {  
  50.   
  51.             }  
  52.             result = 5;  
  53.         }  
  54.     }  

控制台结果是: 
Java代码  收藏代码
  1. in main  
  2. join total time = 0  
  3. the result is 0  
  4. Thread-2 is blocked for 897ms  
  5. Thread-0 is blocked for 1334ms  
  6. Thread-1 is blocked for 4623ms  

运行的直观感受是: 
前三行一瞬间就出来了,就是说main线程没有收到任何阻塞,一下子就运行结束了。 
其实这个时候三个子线程也在运行,不过sleep的阻塞时间有长短,因此就一个一个的执行完打印出了后面三行。 

但是如果把//那几行放开,则结果变为: 
Java代码  收藏代码
  1. in main  
  2. Thread-0 is blocked for 4734ms  
  3. Thread-1 is blocked for 2307ms  
  4. Thread-2 is blocked for 4562ms  
  5. join total time = 11609  
  6. the result is 15  

运行的直观感受是: 
第一行出来之后过了4734ms出现第二行,又过了2307ms出现第三行,又过了4562ms后面都出来了。 
其实这就表明了join()的用处,就是: 
比如在main线程中调用thread-0.join(),那么main线程就会阻塞,等到thread-0执行结束之后再继续运行main线程。 
因此这个例子的代码实现了一个多线程进行汇总的功能,很有意思。 


再来一个例子,也可以帮助加深理解: 
Java代码  收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Thread t1 = new MyThread1();  
  4.         t1.start();  
  5.   
  6.         for (int i = 0; i < 20; i++) {  
  7.             System.out.println("主线程第" + i + "次执行!");  
  8.             if (i > 2)  
  9.                 try {  
  10.                     // t1线程合并到主线程中,主线程停止执行过程,转而执行t1线程,直到t1执行完毕后继续。  
  11.                     t1.join();  
  12.                 } catch (InterruptedException e) {  
  13.                     e.printStackTrace();  
  14.                 }  
  15.         }  
  16.     }  
  17. }  
  18.   
  19. class MyThread1 extends Thread {  
  20.     public void run() {  
  21.         for (int i = 0; i < 10; i++) {  
  22.             System.out.println("线程1第" + i + "次执行!");  
  23.         }  
  24.     }  
  25. }  

控制台结果为: 
Java代码  收藏代码
  1. 线程10次执行!  
  2. 线程11次执行!  
  3. 线程12次执行!  
  4. 线程13次执行!  
  5. 线程14次执行!  
  6. 线程15次执行!  
  7. 主线程第0次执行!  
  8. 线程16次执行!  
  9. 主线程第1次执行!  
  10. 主线程第2次执行!  
  11. 主线程第3次执行!  
  12. 线程17次执行!  
  13. 线程18次执行!  
  14. 线程19次执行!  
  15. 主线程第4次执行!  
  16. 主线程第5次执行!  
  17. 主线程第6次执行!  
  18. 主线程第7次执行!  
  19. 主线程第8次执行!  
  20. 主线程第9次执行!  
  21. 主线程第10次执行!  
  22. 主线程第11次执行!  
  23. 主线程第12次执行!  
  24. 主线程第13次执行!  
  25. 主线程第14次执行!  
  26. 主线程第15次执行!  
  27. 主线程第16次执行!  
  28. 主线程第17次执行!  
  29. 主线程第18次执行!  
  30. 主线程第19次执行!  

在主线程第3次执行!之后,会先执行完线程1,再返回主线程进行执行。