可以用setPriority来设置线程的优先级,getPriority取得线程的优先级:
1 package priority; 2 3 import sharevar.Machine; 4 5 public class priority extends Thread { 6 private static StringBuffer log=new StringBuffer(); 7 private static int count=0; 8 9 public void run() {10 for (int a=0;a<20;a++) {11 log.append(currentThread().getName()+":"+a);12 if (++count %10==0) log.append("\n");13 }14 }15 16 /**17 * @param args18 */19 public static void main(String[] args) {20 // TODO Auto-generated method stub21 Machine m1=new Machine();22 Machine m2=new Machine();23 m1.setName("m1");24 m2.setName("m2");25 26 Thread main=Thread.currentThread(); //获得主线程27 //查看和设置线程的优先级28 System.out.println("default priority of main:"+main.getPriority());29 //打印m2线程默认优先级30 System.out.println("default priority of m1:"+m1.getPriority());31 //打印m2线程默认优先级32 System.out.println("default priority of m2:"+m2.getPriority());33 34 m2.setPriority(Thread.MAX_PRIORITY);35 m1.setPriority(Thread.MIN_PRIORITY);36 37 m1.start();38 m2.start();39 System.out.println(log);40 }41 42 }