public class SchedualDemo {
public static void main(String[] args) {
// 多线程加强之yield()的用法
// 用来使具有相同优先级的线程获得执行的机会。如果具有相同优先级的其它线程是可运行的,
// yield()将把线程放到可运行池中并使另一个线程运行。如果没有相同优先级的可运行线程,
// 则什么都不做。
// 注意,执行一次yield()方法,该线程只是放弃当前这一次机会,然后又会重新和其它线程一起抢占CPU,很可能又比其它线程先抢到。
MyRun r1 = new MyRun(1);
Thread t1 = new Thread(r1);
MyRun r2 = new MyRun(2);
Thread t2 = new Thread(r2);
t1.setPriority(5);
t1.setPriority(5);
t1.start();
t2.start();
}
}
class MyRun implements Runnable{
private int num;
public MyRun(int num) {
this.num = num;
}
public void run() {
if(num==1){
Thread.yield();
}
for(int i =1;i<10;i++){
System.out.println(num+":"+i);
}
}
}