礼让线程

礼让线程

  • yield让出cpu,让别的线程去执行。
    (支持特别不好,很多时候看不出效果,只作为了解。)
package com.heima.threadmethod;

public class Demo06_Yield {
	public static void main(String[] args) {
		new MyThread().start();
		new MyThread().start();
	}
}

class MyThread extends Thread {
	public void run() {
		for(int i = 1; i <= 1000; i++) {
			if(i % 10 == 0) {
				Thread.yield();						//让出CPU
			}
			System.out.println(getName() + "..." + i);
		}
	}
}
发布了347 篇原创文章 · 获赞 11 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/LeoZuosj/article/details/104190331