优先级调度:
线程优先级共有十级,越大越优先——即被调度的概率高一些
setPriority(int n)——设置优先级:总共10个优先级
package strengthen.scheduling;
/**
* 按照优先级调度
* @author admin
*/
public class Priority {
public static void main(String[] args) {
MyRun mr1 = new MyRun(1);
Thread t1 = new Thread(mr1);
t1.setName("线程1");//设置线程名
t1.setPriority(7);//设置线程优先级
//--线程一共有10个优先级,默认优先级为5
MyRun mr2 = new MyRun(2);
Thread t2 = new Thread(mr2);
t2.setName("线程2");
//启动--就绪态
t1.start();
t2.start();
}
}
class MyRun implements Runnable{
private int num = 0;
public MyRun(int num) {
this.num = num;
}
@Override
public void run() {
//奇偶线程
for(int i=num;i<=10;i+=2){
//Thread.currentThread()--静态方法,返回对当前正在执行的线程对象的引用
Thread thread = Thread.currentThread();
String name = thread.getName();
System.out.print(name+":"+i);
}
System.out.println();//当其中一个线程run()方法运行完时,空行
}
}