yield线程礼让
作者:互联网
线程礼让:
1. 礼让线程,让当前正在执行的线程暂停,但不阻塞
2. 将线程从运行状态转为就绪状态
3. 让cpu重新调度,礼让不一定成功!看CPU的心情
package Runnable1;
//测试礼让线程,礼让不一定成功,看cpu心情
public class TestYield
{
public static void main(String[] args)
{
MyYield myYield=new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable
{
@Override
public void run()
{
System.out.println(Thread.currentThread().getName()+ "线程开始执行");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
标签:Thread,MyYield,yield,线程,new,public,礼让 来源: https://www.cnblogs.com/zjwcoblogs/p/16525475.html