java笔记线程方式1优先级

* 我们的线程没有设置优先级,肯定有默认优先级。
 * 那么,默认优先级是多少呢?
 * 如何获取线程对象的优先级?
 *   public final int getPriority():返回线程对象的优先级
 * 如何设置线程对象的优先级呢?
 *   public final void setPriority(int newPriority):更改线程的优先级。
 *
 * 注意:
 *   线程默认优先级是5。 
 *   线程优先级的范围是:1-10。
 *   线程优先级高仅仅表示线程获取的 CPU时间片的几率高,但是要在次数比较多,或者多次运行的时候才能看到比较好的效果。
 *   
 * IllegalArgumentException:非法参数异常。
 * 抛出的异常表明向方法传递了一个不合法或不正确的参数。

 1 public class ThreadPriority extends Thread {
 2     @Override
 3     public void run() {
 4         for (int x = 0; x < 100; x++) {
 5             System.out.println(getName() + ":" + x);
 6         }
 7     }
 8 }
 9 public class ThreadPriorityDemo {
10     public static void main(String[] args) {
11         ThreadPriority tp1 = new ThreadPriority();
12         ThreadPriority tp2 = new ThreadPriority();
13         ThreadPriority tp3 = new ThreadPriority();
14 
15         tp1.setName("东方不败");
16         tp2.setName("岳不群");
17         tp3.setName("林平之");
18 
19         // 获取默认优先级
20         // System.out.println(tp1.getPriority());
21         // System.out.println(tp2.getPriority());
22         // System.out.println(tp3.getPriority());
23 
24         // 设置线程优先级
25         // tp1.setPriority(100000);
26         
27         //设置正确的线程优先级
28         tp1.setPriority(10);
29         tp2.setPriority(1);
30 
31         tp1.start();
32         tp2.start();
33         tp3.start();
34     }
35 }
View Code
原文地址:https://www.cnblogs.com/lanjianhappy/p/6383932.html