1 package com.twod4z;
 2 /**
 3  * @program: com.twod4z
 4  * @description:把普通号看病写在主线程里,特需号看病写在子线程里,当普通号看到第10个人之后,让
 5  *              特需号的子线程强制执行,也就是在相应位置写一句 special.join()
 6  * @author: Mr.Lin
 7  * @create: 2019年8月8日
 8  **/
 9 public class SicknessDemo {    
10     public static void main(String[] args) {                  
11         // 创建特需号线程                 
12         MyThreads special = new MyThreads("特需号", 10, 3000);                  
13         // 修改主线程名称为普通号                  
14         Thread.currentThread().setName("普通号");                  
15         // 设置特需号线程优先级高,提高特需号叫到的概率                  
16         special.setPriority(8);                  
17         // 启动子线程,也就是特需号线程                  
18         special.start();              
19         // 主线程普通号循环叫号看病              
20         for (int i = 0; i < 20; i++) {                              
21             System.out.println(Thread.currentThread().getName() + ":" + (i + 1)+ "号病人在看病!");                  
22             // 普通号叫到第10个号,特需号线程强行执行,阻塞主线程                  
23             if (i == 9) {                      
24                 try {                          
25                     // 子线程(特需号线程)强行执行                          
26                     special.join();                      
27                 } catch (InterruptedException e) {                          
28                     e.printStackTrace();                      
29                 }                
30             }                  
31             try {                      
32                 // 普通号看病间隔时间                      
33                 Thread.sleep(500);                  
34             } catch (InterruptedException e) {                      
35                 e.printStackTrace();                  
36             }                    
37         }          
38     }          
39 }
40       
41 class MyThreads extends Thread {           
42     // 线程名字          
43     private int number;          
44     private int time;
45                 
46     public MyThreads(String name, int number, int time) {              
47         super(name); // 调用THread类的构造方法,在创建线程对象时指定线程名              
48         this.number = number;              
49         this.time = time;         
50     }
51                 
52     @Override          
53     // run方法里模拟子线程特需号的看病过程    
54     public void run() {              
55         for (int i = 0; i < number; i++) {                
56             System.out.println(this.getName() + ":" + (i + 1) + "号病人在看病!");// this是当前的线程对象                  
57             try {                      
58                 // 设置特需号看病时间                      
59                 Thread.sleep(time);                 
60             } catch (InterruptedException e) {                                      
61                 e.printStackTrace();                  
62             }              
63         }              
64     }
65 
66 }
实现

 

posted on 2019-08-08 10:11  MOCHICBEAM  阅读(307)  评论(0编辑  收藏  举报