Sleep-Join方法理解

 1 package cn.mayday.test;
 2 
 3 public class JoinTest {
 4 
 5     public static void main(String[] args) {
 6 
 7         try {
 8 
 9             int count = 50;
10             // while (count > 1) {
11 
12             Thread thread1 = new Thread(
13                     new Runnable() {
14                         public void run() {
15 
16                             try {
17                                 System.out.println("+++++ 1111 start ++++");
18                                 Thread.sleep(10000);
19                                 System.out.println("+++++ 1111 end ++++");
20                             } catch (Exception e) {
21                                 System.out.println("Exception 1111 = " + e);
22                             }
23                         }
24                     }
25             );
26             thread1.start();
27 
28 
29             Thread thread2 = new Thread(
30                     new Runnable() {
31                         public void run() {
32                             try {
33                                 System.out.println("+++++ 2222 start ++++");
34                                 Thread.sleep(10000);
35                                 System.out.println("+++++ 2222 end ++++");
36 
37                             } catch (Exception e) {
38                                 System.out.println("Exception 2222 = " + e);
39                             }
40                         }
41                     }
42             );
43             thread2.start();
44 
45             thread1.join();
46             System.out.println("++++++++++++ thread1111 join end ++++++++++++");
47 
48             thread2.join();
49             System.out.println("++++++++++++ thread2222 join end ++++++++++++");
50 
51 
52             Thread.sleep(1000);
53             // }
54 
55         } catch (Exception e) {
56             System.out.println("Exception = " + e);
57         }
58 
59     }
60 }

 【总结】:从结果可以看出,主线程在线程1和线程2 join本身不阻塞,当时需要main线程做得动作全部阻塞

摘自

原文地址:https://www.cnblogs.com/snowwhite/p/11099195.html