线程里面的yield();方法(让出线程)

package thread;

public class Thread_yield {

	public static void main(String[] args) {
		/**
		 * 线程让出当前占用的处理器CPU
		 */
		Thread t1 = new ThreadDemo("t1");
		Thread t2 = new ThreadDemo("t2");
		t1.start();
		t2.start();
	}
}
class ThreadDemo extends Thread {
	String str;

	public ThreadDemo(String str) {
		this.str = str;
	}

	@Override
	public void run() {
		for (int i = 0; i < 500; i++) {
			System.out.println(str + ":" + i);
			//"当前线程"让出正在使用的CPU处理器资源
			//如果时多处理器情况下,CUP占用率不高,这个方法运行结果不明显
			yield();
		}
	}

}

  

原文地址:https://www.cnblogs.com/xyk1987/p/8267294.html