线程礼让,例如:线程A礼让线程B,就是线程A将CPU释放,也就是让当前执行的线程暂停,但是不阻塞,然后A和B一起竞争cup,但是cpu的调度是随机的,所以礼让是有可能失败的。
package com.Thread1; public class yeildTest { public static void main(String[] args) { YeildThread yeildThread=new YeildThread(); new Thread(yeildThread,"a").start(); new Thread(yeildThread,"b").start(); } } class YeildThread implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"线程运行"); Thread.yield(); System.out.println(Thread.currentThread().getName()+"线程运行"); } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219512.html原文链接:https://javaforall.net
