<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### Java并發編程:線程間協作的兩種方式:wait、notify、notifyAll和Condition * * * * * 轉載自: http://www.cnblogs.com/dolphin0520/p/3920385.html 在現實中,需要線程之間的協作。比如說最經典的生產者-消費者模型:當隊列滿時,生產者需要等待隊列有空間才能繼續往里面放入商品,而在等待的期間內,生產者必須釋放對臨界資源(即隊列)的占用權。因為生產者如果不釋放對臨界資源的占用權,那么消費者就無法消費隊列中的商品,就不會讓隊列有空間,那么生產者就會一直無限等待下去。因此,一般情況下,當隊列滿時,會讓生產者交出對臨界資源的占用權,并進入掛起狀態。然后等待消費者消費了商品,然后消費者通知生產者隊列有空間了。同樣地,當隊列空時,消費者也必須等待,等待生產者通知它隊列中有商品了。這種互相通信的過程就是線程間的協作。   今天我們就來探討一下Java中線程協作的最常見的兩種方式:利用Object.wait()、Object.notify()和使用Condition   以下是本文目錄大綱:   一.wait()、notify()和notifyAll()   二.Condition   三.生產者-消費者模型的實現   若有不正之處請多多諒解,并歡迎批評指正。    **一.wait()、notify()和notifyAll()** wait()、notify()和notifyAll()是Object類中的方法: ~~~ /** * Wakes up a single thread that is waiting on this object's * monitor. If any threads are waiting on this object, one of them * is chosen to be awakened. The choice is arbitrary and occurs at * the discretion of the implementation. A thread waits on an object's * monitor by calling one of the wait methods */ public final native void notify(); /** * Wakes up all threads that are waiting on this object's monitor. A * thread waits on an object's monitor by calling one of the * wait methods. */ public final native void notifyAll(); /** * Causes the current thread to wait until either another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or a * specified amount of time has elapsed. * <p> * The current thread must own this object's monitor. */ public final native void wait(long timeout) throws InterruptedException; ~~~  從這三個方法的文字描述可以知道以下幾點信息:   1)wait()、notify()和notifyAll()方法是本地方法,并且為final方法,無法被重寫。   2)調用某個對象的wait()方法能讓當前線程阻塞,并且當前線程必須擁有此對象的monitor(即鎖)   3)調用某個對象的notify()方法能夠喚醒一個正在等待這個對象的monitor的線程,如果有多個線程都在等待這個對象的monitor,則只能喚醒其中一個線程;   4)調用notifyAll()方法能夠喚醒所有正在等待這個對象的monitor的線程;   有朋友可能會有疑問:為何這三個不是Thread類聲明中的方法,而是Object類中聲明的方法(當然由于Thread類繼承了Object類,所以Thread也可以調用者三個方法)?其實這個問題很簡單,由于每個對象都擁有monitor(即鎖),所以讓當前線程等待某個對象的鎖,當然應該通過這個對象來操作了。而不是用當前線程來操作,因為當前線程可能會等待多個線程的鎖,如果通過線程來操作,就非常復雜了。   上面已經提到,如果調用某個對象的wait()方法,當前線程必須擁有這個對象的monitor(即鎖),因此調用wait()方法必須在同步塊或者同步方法中進行(synchronized塊或者synchronized方法)。   調用某個對象的wait()方法,相當于讓當前線程交出此對象的monitor,然后進入等待狀態,等待后續再次獲得此對象的鎖(Thread類中的sleep方法使當前線程暫停執行一段時間,從而讓其他線程有機會繼續執行,但它并不釋放對象鎖);   notify()方法能夠喚醒一個正在等待該對象的monitor的線程,當有多個線程都在等待該對象的monitor的話,則只能喚醒其中一個線程,具體喚醒哪個線程則不得而知。   同樣地,調用某個對象的notify()方法,當前線程也必須擁有這個對象的monitor,因此調用notify()方法必須在同步塊或者同步方法中進行(synchronized塊或者synchronized方法)。   nofityAll()方法能夠喚醒所有正在等待該對象的monitor的線程,這一點與notify()方法是不同的。   這里要注意一點:notify()和notifyAll()方法只是喚醒等待該對象的monitor的線程,并不決定哪個線程能夠獲取到monitor。   舉個簡單的例子:假如有三個線程Thread1、Thread2和Thread3都在等待對象objectA的monitor,此時Thread4擁有對象objectA的monitor,當在Thread4中調用objectA.notify()方法之后,Thread1、Thread2和Thread3只有一個能被喚醒。注意,被喚醒不等于立刻就獲取了objectA的monitor。假若在Thread4中調用objectA.notifyAll()方法,則Thread1、Thread2和Thread3三個線程都會被喚醒,至于哪個線程接下來能夠獲取到objectA的monitor就具體依賴于操作系統的調度了。   上面尤其要注意一點,一個線程被喚醒不代表立即獲取了對象的monitor,只有等調用完notify()或者notifyAll()并退出synchronized塊,釋放對象鎖后,其余線程才可獲得鎖執行。      下面看一個例子就明白了: ~~~ public class Test { public static Object object = new Object(); public static void main(String[] args) { Thread1 thread1 = new Thread1(); Thread2 thread2 = new Thread2(); thread1.start(); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } thread2.start(); } static class Thread1 extends Thread{ @Override public void run() { synchronized (object) { try { object.wait(); } catch (InterruptedException e) { } System.out.println("線程"+Thread.currentThread().getName()+"獲取到了鎖"); } } } static class Thread2 extends Thread{ @Override public void run() { synchronized (object線程Thread-1調用了object.notify() 線程Thread-1釋放了鎖 線程Thread-0獲取到了鎖; System.out.println("線程"+Thread.currentThread().getName()+"調用了object.notify()"); } System.out.println("線程"+Thread.currentThread().getName()+"釋放了鎖"); } } } ~~~  無論運行多少次,運行結果必定是: >  線程Thread-1調用了object.notify() > 線程Thread-1釋放了鎖 > 線程Thread-0獲取到了鎖
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看