Java多线程之Join方法阻塞线程

 

 1 package org.study2.javabase.ThreadsDemo.status;
 2 
 3 /**
 4  * @Auther:GongXingRui
 5  * @Date:2018/9/19
 6  * @Description: 阻塞进程方法Join
 7  **/
 8 public class ThreadJoin {
 9     public static void main(String args[]) throws InterruptedException {
10         JoinDemo demo = new JoinDemo();
11         Thread t = new Thread(demo);
12         t.start();
13 
14         for (int i = 0; i < 21; i++) {
15             if (i == 10) {
16                 // 阻塞main进程,执行线程
17                 t.join();
18             }
19             System.out.println("main执行中: " + i);
20             Thread.sleep(100);
21         }
22     }
23 }
24 
25 class JoinDemo implements Runnable {
26     @Override
27     public void run() {
28         for (int i = 0; i < 21; i++) {
29             System.out.println("线程执行中: " + i);
30             try {
31                 Thread.sleep(100);
32             } catch (InterruptedException e) {
33                 e.printStackTrace();
34             }
35         }
36     }
37 }

 

posted @ 2018-09-19 16:57  星瑞  阅读(512)  评论(0编辑  收藏  举报