Java笔记二:

值得注意的是,尽管Java提供了10个优先级,但它与多数操作系统都不能很好地映射.比如Windows 2000有7个优先级,并且不是固定
的,而SUN公司的Solaris操作系统有2的31次方个优先级.如果希望程序能移植到不同的平台中,应该确保在设置线程的优先级时,只使

用MAX_PRIORITY.NORM_PRIORITY.MIN_PRIORITY这三个优先级.这样才能保证在不同的操作系统中,对同样优先级的线程式采用同样的

调试方式.

  一个线程创A建了另一个线程B,则线程B的优先级就和线程A的优先级是一样的,比如主线程创建了另一个线程,则这个被创建的线程

式的优先级就和主线程的优先级是一样的.

下面是一个例子:


public class Machine extends Thread{
 
 public void run()
 {
  for(int a=0;a<3;a++)
  {
   System.out.println(currentThread().getName()+":"+a);
   /*if(a==1 && currentThread().getName().equals("m1"))
    throw new RuntimeException("Wrong from Machine");
   try
   {
    sleep(100);
   }
   catch(InterruptedException e)
   {
    throw new RuntimeException(e);
   }*/
  }
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
         Machine m=new Machine();
         m.setName("m1");
        
         Thread main=Thread.currentThread();
         System.out.println("the main thread's priority is:"+main.getPriority());
         m.start();
         System.out.println("the default priority is:"+m.getPriority());
         m.setPriority(MIN_PRIORITY);
         System.out.println("the min priority is:"+m.getPriority());
         //m.run();
         //System.out.println("Is machine alive "+m.isAlive());
         //System.out.println("main:end");
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章