13、线程的优先级(优先级(priority)要设置在.start之前)

线程的优先级(优先级(priority)要设置在.start之前)

 

  1. 线程的默认优先级为5(例如主线程main的),最低的优先级为1,最大的优先级为10.其中若是大于最大的优先级10或者小于最小的优先级1。他会抛出异常不会执行
  2. 优先级低cpu可能会发生先与优先级高的调度

 

 

还是要看cpu心情(麻了)

 

package org.example.threadpriority;public class TestPriority {public static void main(String[] args) {System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());Priority priority = new Priority();Thread thread1 = new Thread(priority);Thread thread2 = new Thread(priority);Thread thread3 = new Thread(priority);Thread thread4 = new Thread(priority);Thread thread5 = new Thread(priority);Thread thread6 = new Thread(priority);thread1.start();thread2.setPriority(1);thread2.start();thread3.setPriority(4);thread3.start();thread4.setPriority(Thread.MAX_PRIORITY);thread4.start();//        thread5.setPriority(11);thread5.setPriority(8);thread5.start();//        thread6.setPriority(-1);thread6.setPriority(6);thread6.start();}
}
class Priority implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部