线程优先级
- 不设置优先级,默认为 5
- 先设置,在启动!!!
- 优先级只是相对的,大概率下是优先级高的先执行,不是一定先执行
package cn.usts.edu.lesson06;
public class ThreadPriorityDemo implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-----"+Thread.currentThread().getPriority());
}
public static void main(String[] args) {
ThreadPriorityDemo threadPriorityDemo = new ThreadPriorityDemo();
Thread t1 = new Thread(threadPriorityDemo,"A");
Thread t2 = new Thread(threadPriorityDemo,"B");
Thread t3 = new Thread(threadPriorityDemo,"C");
Thread t4 = new Thread(threadPriorityDemo,"D");
Thread t5 = new Thread(threadPriorityDemo,"E");
Thread t6 = new Thread(threadPriorityDemo,"F");
Thread t7 = new Thread(threadPriorityDemo,"G");
t1.start();
t2.setPriority(2);
t2.start();
t3.setPriority(3);
t3.start();
t4.setPriority(4);
t4.start();
t5.setPriority(5);
t5.start();
t6.setPriority(6);
t6.start();
t7.setPriority(Thread.MAX_PRIORITY);
t7.start();
}
}