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