线程优先级 11
1. java提供线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器
按照优先级决定应该调度哪个线程来执行。
优先级:1. java中线程是有优先级的。
2. Thread类有3个优先级的静态常量:Thread.MIN_PRIORITY优先级最小
Thread.MAX_PRIORITY 优先级最高
Thread.NORM_PRIORITY 缺省优先级,默认值5
2. 改变线程的优先级:对象.setPriority(Thead.优先级);
package Runnable1;
//测试线程优先级
public class TestPriority
{
public static void main(String[] args)
{
//主线程默认优先级
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
Mypriority mypriority=new Mypriority();
Thread t1=new Thread(mypriority);
Thread t2=new Thread(mypriority);
Thread t3=new Thread(mypriority);
Thread t4=new Thread(mypriority);
Thread t5=new Thread(mypriority);
Thread t6=new Thread(mypriority);
//先设置优先级,在启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);//10
t4.start();
t5.setPriority(8);
t5.start();
}
}
class Mypriority implements Runnable
{