给线程设置优先级,最大10,最小1,优先级设置完成后,只是增大了被cpu调度的机会,并不是绝对优先执行
package com.Thread1;
public class ThreadPriority {
public static void main(String[] args) {
//打印输出主线程的优先级
System.out.println(Thread.currentThread().getName()+"------->"+Thread.currentThread().getPriority());
MyPriority myPriority=new MyPriority();
Thread thread1=new Thread(myPriority);
Thread thread2=new Thread(myPriority);
Thread thread3=new Thread(myPriority);
Thread thread4=new Thread(myPriority);
Thread thread5=new Thread(myPriority);
Thread thread6=new Thread(myPriority);
thread1.start();
thread2.setPriority(9);
thread2.start();
thread3.setPriority(10);
thread3.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"------->"+Thread.currentThread().getPriority());
}
}