Java 多线程联系5:模拟叫号看病

需求说明:

 1 package KanBing;
 2 
 3 /**
 4  * 治病
 5  *
 6  */
 7 public class CureThread implements Runnable {
 8     public void run() {
 9         for (int i = 0; i < 10; i++) {
10             System.out.println("特需号:"+(i+1)+"号病人在看病!");
11             try {
12                 Thread.sleep(1000);
13             } catch (InterruptedException e) {
14                 // TODO Auto-generated catch block
15                 e.printStackTrace();
16             }
17         }
18     }
19 }
CureThread
 1 package KanBing;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         Thread thread = new Thread(new CureThread());
 6         thread.setPriority(Thread.MAX_PRIORITY);
 7         thread.start();
 8         //主线程模拟医院叫号
 9         for(int i=0;i<20;i++){
10             System.out.println("普通号:"+(i+1)+"号病人在看病!");
11             try {
12                 Thread.sleep(500);
13             } catch (InterruptedException e1) {                
14                 e1.printStackTrace();
15             }
16             if(i==9){                
17                 try {
18                     thread.join();
19                 } catch (InterruptedException e) {                    
20                     e.printStackTrace();
21                 }
22             }
23         }
24     }
25 }
Test

运行结果:

 

posted @ 2019-08-06 16:09  靖凯  阅读(1882)  评论(0编辑  收藏  举报