關於多線程中sleep、join、yield的區別

好了、說了多線程,那就不得不說說多線程的sleep()、join()和yield()三個方法的區別啦java

   一、sleep()方法多線程

 /**
     * Causes the currently executing thread to sleep (temporarily cease
     * execution) for the specified number of milliseconds, subject to
     * the precision and accuracy of system timers and schedulers. The thread
     * does not lose ownership of any monitors.
     *  意思是說:當前正在執行的線程休眠(暫時中止執行)指定的毫秒數,具體取決於系統計時器和調度程序的精度和準確性。 該線程不會失去任何監視器的全部權。
     * @param  millis
     *         the length of time to sleep in milliseconds  
     *         毫秒爲單位
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public static native void sleep(long millis) throws InterruptedException;

其實主要的就是他是讓其餘線程走,本身進行休眠,可是本身卻不會釋放對象鎖,也就是說,若是有同步鎖的時候,其餘線程不能訪問共享數據。併發

注意該方法要捕獲異常 好比有兩個線程同時執行(沒有Synchronized),一個線程優先級爲MAX_PRIORITY,另外一 個爲MIN_PRIORITY,若是沒有Sleep()方法,只有高優先級的線程執行完成後,低優先級 的線程才能執行;但當高優先級的線程sleep(5000)後,低優先級就有機會執行了。 總之,sleep()可使低優先級的線程獲得執行的機會,固然也可讓同優先級、高優先級的 線程有執行的機會。app

  二、yield() 方法oop

    /**
     * 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.
     * 它是一種啓發式嘗試,用於改善線程之間的相對進展,不然會過分利用CPU。 它的使用應與詳細的分析和基準測試相結合,以確保它實際上具備所需的效果。
     * <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() 這個方法從以上註釋能夠看出,也是一個休眠自身線程的方法,一樣不會釋放自身鎖的標識,區別在於它是沒有參數的,即yield()方 法只是使當前線程從新回到可執行狀態,測試

因此執行yield()的線程有可能在進入到可執行狀態 後立刻又被執行,另外yield()方法只能使同優先級或者高優先級的線程獲得執行機會,這也 和sleep()方法不一樣。this

 

三、join() 方法spa

這個方法比較有意思,Thread的非靜態方法join()讓一個線程B「加入」到另一個線程A的尾部。在A執行完畢以前, B不能工做。線程

/**
     * Waits for this thread to die.  
     *  等待線程死亡
     * <p> An invocation of this method behaves in exactly the same
     * way as the invocation
     *
     * <blockquote>
     * {@linkplain #join(long) join}{@code (0)}
     * </blockquote>
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public final void join() throws InterruptedException {
        join(0);  // 調用了有參方法
    }
  /**
     * Waits at most {@code millis} milliseconds for this thread to
     * die. A timeout of {@code 0} means to wait forever.
     *  這個線程最多等多少毫秒,若是超時了,就會進行線程死鎖
     * <p> This implementation uses a loop of {@code this.wait} calls
     * conditioned on {@code this.isAlive}. As a thread terminates the
     * {@code this.notifyAll} method is invoked. It is recommended that
     * applications not use {@code wait}, {@code notify}, or
     * {@code notifyAll} on {@code Thread} instances.
     *
     * @param  millis
     *         the time to wait in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public final synchronized void join(long millis)
    throws InterruptedException {

保證當前線程中止執行,直到該線程所加入的線程完成爲止。然而,若是它加入的線程沒有存活,則當前線程不須要中止。debug

相關文章
相關標籤/搜索