這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)Java notify喚醒源代碼的示例分析,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
湘西土家族網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司成立于2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
Java notify喚醒在此對象監(jiān)視器上等待的單個線程。相關(guān)的問題需要我們不斷的學(xué)習(xí),下面我們就看看如何才能更好的使用。如果所有線程都在此對象上等待,則會選擇喚醒其中一個線程。
直到當前的線程放棄此對象上的鎖定,才能繼續(xù)執(zhí)行被喚醒的線程。此方法只應(yīng)由作為此對象監(jiān)視器的所有者的線程來調(diào)用.
"當前的線程必須擁有此對象監(jiān)視器"與"此方法只應(yīng)由作為此對象監(jiān)視器的所有者的線程來調(diào)用"說明wait方法與notify方法必須在同步塊內(nèi)執(zhí)行,即synchronized(obj之內(nèi)).
調(diào)用對像wait方法后,當前線程釋放對像鎖,進入等待狀態(tài).直到其他線程(也只能是其他線程)通過Java notify喚醒方法,或 notifyAll.該線程重新獲得對像鎖.
繼續(xù)執(zhí)行,記得線程必須重新獲得對像鎖才能繼續(xù)執(zhí)行.因為synchronized代碼塊內(nèi)沒有鎖是寸步不能走的.看一個很經(jīng)典的例子:
Java notify喚醒代碼
package ProductAndConsume; import java.util.List; public class Consume implements Runnable{ private List container = null; private int count; public Consume(List lst){ this.container = lst; } public void run() { while(true){ synchronized (container) { if(container.size()== 0){ try { container.wait();//放棄鎖 } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } container.remove(0); container.notify(); System.out.println("我吃了"+(++count)+"個"); } } } } package ProductAndConsume; import java.util.List; public class Product implements Runnable { private List container = null; private int count; public Product(List lst) { this.container = lst; } public void run() { while (true) { synchronized (container) { if (container.size() > MultiThread.MAX) { try { container.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } container.add(new Object()); container.notify(); System.out.println("我生產(chǎn)了"+(++count)+"個"); } } } } package ProductAndConsume; import java.util.ArrayList; import java.util.List; public class MultiThread { private List container = new ArrayList(); public final static int MAX = 5; public static void main(String args[]){ MultiThread m = new MultiThread(); new Thread(new Consume(m.getContainer())).start(); new Thread(new Product(m.getContainer())).start(); new Thread(new Consume(m.getContainer())).start(); new Thread(new Product(m.getContainer())).start(); } public List getContainer() { return container; } public void setContainer(List container) { this.container = container; }
上述就是小編為大家分享的Java notify喚醒源代碼的示例分析了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:Javanotify喚醒源代碼的示例分析
標題來源:http://chinadenli.net/article2/ppcgoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、標簽優(yōu)化、App設(shè)計、網(wǎng)站內(nèi)鏈、服務(wù)器托管、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)