十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
如何進(jìn)行Java多線程語句具體分類的分析,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),安陸網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:安陸等地區(qū)。安陸做網(wǎng)站價(jià)格咨詢:18980820575
在Java多線程語句中有很多的小的語句需要我們特殊的注意。wait(),notify(),notifyAll()不屬于Thread類,下面我們就來詳細(xì)的看看如何使用這幾個(gè)分類代碼。希望大家有所收獲。
而是屬于Object基礎(chǔ)類,也就是說每個(gè)對(duì)像都有wait(),notify(),notifyAll()的功能.因?yàn)槎紓€(gè)對(duì)像都有鎖,鎖是每個(gè)對(duì)像的基礎(chǔ),當(dāng)然操作鎖的方法也是最基礎(chǔ)了.先看java doc怎么說:
Java多線程語句中,wait導(dǎo)致當(dāng)前的線程等待,直到其他線程調(diào)用此對(duì)象的 notify() 方法或 notifyAll() 方法。當(dāng)前的線程必須擁有此對(duì)象監(jiān)視器。該線程發(fā)布對(duì)此監(jiān)視器的所有權(quán)并等待,直到其他線程通過調(diào)用 notify 方法,或 notifyAll 方法通知在此對(duì)象的監(jiān)視器上等待的線程醒來。然后該線程將等到重新獲得對(duì)監(jiān)視器的所有權(quán)后才能繼續(xù)執(zhí)行.
notify喚醒在此對(duì)象監(jiān)視器上等待的單個(gè)線程。如果所有線程都在此對(duì)象上等待,則會(huì)選擇喚醒其中一個(gè)線程。直到當(dāng)前的線程放棄此對(duì)象上的鎖定,才能繼續(xù)執(zhí)行被喚醒的線程。此方法只應(yīng)由作為此對(duì)象監(jiān)視器的所有者的線程來調(diào)用.
"當(dāng)前的線程必須擁有此對(duì)象監(jiān)視器"與"此方法只應(yīng)由作為此對(duì)象監(jiān)視器的所有者的線程來調(diào)用"說明wait方法與notify方法必須在同步塊內(nèi)執(zhí)行,即synchronized(obj之內(nèi)).
調(diào)用對(duì)像wait方法后,當(dāng)前線程釋放對(duì)像鎖,進(jìn)入等待狀態(tài).直到其他線程(也只能是其他線程)通過notify 方法,或 notifyAll.該線程重新獲得對(duì)像鎖。繼續(xù)執(zhí)行,記得線程必須重新獲得對(duì)像鎖才能繼續(xù)執(zhí)行.因?yàn)閟ynchronized代碼塊內(nèi)沒有鎖是寸步不能走的.看一個(gè)很經(jīng)典的例子:
Code 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)+"個(gè)"); } } } } 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)+"個(gè)"); } } } } package ProductAndConsume; imort 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; }關(guān)于如何進(jìn)行Java多線程語句具體分類的分析問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。