1 package com.doctor; 2 /** 3 * 测试类 4 * @author jjit 5 * 6 */ 7 public class SeeDoctor implements Runnable{ 8 9 @Override 10 public void run() { 11 for(int i = 0;i<50;i++) { 12 System.out.println("特需号"+(i+1)+"正在看病!"); 13 try { 14 Thread.sleep(1000); 15 } catch (InterruptedException e) { 16 e.printStackTrace(); 17 } 18 } 19 } 20 21 }
1 package com.doctor; 2 /** 3 * 看病 4 * @author jjit 5 * 6 */ 7 public class Main { 8 public static void main(String[] args) { 9 SeeDoctor sd = new SeeDoctor(); 10 Thread tr = new Thread(sd); 11 //tr线程的优先级比Thread的优先级大 12 tr.setPriority(Thread.MAX_PRIORITY); 13 tr.start(); 14 for(int i = 0;i<20;i++) { 15 try { 16 Thread.sleep(500); 17 System.out.println("普通号"+(i+1)+"正在看病!"); 18 } catch (InterruptedException e) { 19 e.printStackTrace(); 20 } 21 if(i == 8) { 22 try { 23 tr.join(); 24 } catch (InterruptedException e) { 25 e.printStackTrace(); 26 } 27 } 28 } 29 } 30 }
输出: