模擬病人叫號
阿新 • • 發佈:2019-01-01
1 package demo7; 2 3 public class Test5 { 4 public static void main(String[] args) { 5 MyThread5 mt = new MyThread5(); 6 Thread t = new Thread(mt); 7 t.start(); 8 9 for (int i = 0; i < 50; i++) { 10 try { 11 Thread.sleep(500);12 System.out.println("普通號:" + (i+1) + "號病人在看病!"); 13 } catch (InterruptedException e) { 14 e.printStackTrace(); 15 } 16 if (i == 9) { 17 try { 18 t.join(); 19 } catch (InterruptedException e) {20 e.printStackTrace(); 21 } 22 } 23 } 24 } 25 }
1 package demo7; 2 3 //模擬叫號看病,特需號10個,普通號50個,看病時間特需號是普通號的2倍 4 //叫到普通號第10號時要先看完特需號 5 public class MyThread5 implements Runnable{ 6 7 public void run() { 8 for (int i = 0; i <10; i++) {9 try { 10 Thread.sleep(1000); 11 System.out.println("特需號:"+(i+1)+"號病人在看病!"); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 16 } 17 } 18 }