孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

可以用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 args
18      */
19     public static void main(String[] args) {
20         // TODO Auto-generated method stub
21         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 }

 

posted on 2012-05-06 21:47  孤独的猫  阅读(1368)  评论(0编辑  收藏  举报