1. 程式人生 > >多執行緒 Thread.yield 方法到底有什麼用?

多執行緒 Thread.yield 方法到底有什麼用?

概念

我們知道 start() 方法是啟動執行緒,讓執行緒變成就緒狀態等待 CPU 排程後執行。

那 yield() 方法是幹什麼用的呢?來看下原始碼。

/**
 * A hint to the scheduler that the current thread is willing to yield
 * its current use of a processor. The scheduler is free to ignore this
 * hint.
 *
 * <p> Yield is a heuristic attempt to improve relative progression
 * between threads that would otherwise over-utilise a CPU. Its use
 * should be combined with detailed profiling and benchmarking to
 * ensure that it actually has the desired effect.
 *
 * <p> It is rarely appropriate to use this method. It may be useful
 * for debugging or testing purposes, where it may help to reproduce
 * bugs due to race conditions. It may also be useful when designing
 * concurrency control constructs such as the ones in the
 * {@link java.util.concurrent.locks} package.
 */
public static native void yield();

yield 即 “謙讓”,也是 Thread 類的方法。它讓掉當前執行緒 CPU 的時間片,使正在執行中的執行緒重新變成就緒狀態,並重新競爭 CPU 的排程權。它可能會獲取到,也有可能被其他執行緒獲取到。

實戰

下面是一個使用示例。

/**
 * 微信公眾號:Java技術棧
 */
public static void main(String[] args) {
	Runnable runnable = () -> {
		for (int i = 0; i <= 100; i++) {
			System.out.println(Thread.currentThread().getName() + "-----" + i);
			if (i % 20 == 0) {
				Thread.yield();
			}
		}
	};
	new Thread(runnable, "棧長").start();
    new Thread(runnable, "小蜜").start();
}

這個示例每當執行完 20 個之後就讓出 CPU,每次謙讓後就會馬上獲取到排程權繼續執行。

執行以上程式,可以有以下兩種結果。

結果1:棧長讓出了 CPU 資源,小蜜成功上位。

棧長-----29
棧長-----30
小蜜-----26
棧長-----31

結果2:棧長讓出了 CPU 資源,棧長繼續執行。

棧長-----28
棧長-----29
棧長-----30
棧長-----31

而如果我們把兩個執行緒加上執行緒優先順序,那輸出的結果又不一樣。

thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);

因為給小蜜加了最高優先權,棧長加了最低優先權,即使棧長先啟動,那小蜜還是有很大的概率比棧長先會輸出完的,大家可以試一下。

yield 和 sleep 的異同

1)yield, sleep 都能暫停當前執行緒,sleep 可以指定具體休眠的時間,而 yield 則依賴 CPU 的時間片劃分。

2)yield, sleep 兩個在暫停過程中,如已經持有鎖,則都不會釋放鎖資源。

3)yield 不能被中斷,而 sleep 則可以接受中斷。

總結

棧長沒用過 yield,感覺沒什麼鳥用。

如果一定要用它的話,一句話解釋就是:yield 方法可以很好的控制多執行緒,如執行某項複雜的任務時,如果擔心佔用資源過多,可以在完成某個重要的工作後使用 yield 方法讓掉當前 CPU 的排程權,等下次獲取到再繼續執行,這樣不但能完成自己的重要工作,也能給其他執行緒一些執行的機會,避免一個執行緒長時間佔有 CPU 資源。

動手轉發給更多的朋友吧!


更多 Java 多執行緒技術文章請在Java技術棧微信公眾號後臺回覆關鍵字:多執行緒。

本文原創首發於微信公眾號:Java技術棧(id:javastack),關注公眾號在後臺回覆 “多執行緒” 可獲取更多,轉載請原樣保留本資訊。