多線程 Thread.yield 方法到底有什麼用?

概念

咱們知道 start() 方法是啓動線程,讓線程變成就緒狀態等待 CPU 調度後執行。java

那 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 的調度權。它可能會獲取到,也有可能被其餘線程獲取到。多線程

實戰

下面是一個使用示例。app

/**
 * 微信公衆號: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,每次謙讓後就會立刻獲取到調度權繼續執行。this

運行以上程序,能夠有如下兩種結果。spa

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

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

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

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

而若是咱們把兩個線程加上線程優先級,那輸出的結果又不同。code

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),關注公衆號在後臺回覆 "多線程" 可獲取更多,轉載請原樣保留本信息。

相關文章
相關標籤/搜索