【0805作业】模拟叫号看病
1 package hospital; 2 3 public class VipThread implements Runnable{ 4 5 @Override 6 public void run() { 7 // TODO Auto-generated method stub 8 for(int i=0;i<10;i++) { 9 String name= Thread.currentThread().getName(); 10 System.out.println(name+(i+1)+"号病人在看病!"); 11 12 try { 13 Thread.sleep(1000); 14 } catch (InterruptedException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } 18 } 19 } 20 21 }
1 package hospital; 2 /** 3 * 一天看50个普通号,10个特需号 4 * 特需号时长是普通号的两倍 5 * 并行叫号,特需号优先级较高 6 * 普通号到10号时,优先看完剩余特需号。在继续普通号 7 * @author L 8 * 9 */ 10 public class Main { 11 public static void main(String[] args) { 12 VipThread vipthread = new VipThread(); 13 Thread vip = new Thread(vipthread, "特需号:"); 14 vip.setPriority(10); 15 vip.start(); 16 17 for (int i = 0; i < 50; i++) { 18 if (i == 10) { 19 try { 20 vip.join(); 21 } catch (InterruptedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 } 26 String name = Thread.currentThread().getName(); 27 System.out.println("普通号:"+(i + 1) + "号病人在看病!"); 28 try { 29 Thread.sleep(500); 30 } catch (InterruptedException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 } 35 36 } 37 }