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

运行结果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM