掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流
Java notify喚醒在此對(duì)象監(jiān)視器上等待的單個(gè)線(xiàn)程。相關(guān)的問(wèn)題需要我們不斷的學(xué)習(xí),下面我們就看看如何才能更好的使用。如果所有線(xiàn)程都在此對(duì)象上等待,則會(huì)選擇喚醒其中一個(gè)線(xiàn)程。

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的埇橋區(qū)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
直到當(dāng)前的線(xiàn)程放棄此對(duì)象上的鎖定,才能繼續(xù)執(zhí)行被喚醒的線(xiàn)程。此方法只應(yīng)由作為此對(duì)象監(jiān)視器的所有者的線(xiàn)程來(lái)調(diào)用.
"當(dāng)前的線(xiàn)程必須擁有此對(duì)象監(jiān)視器"與"此方法只應(yīng)由作為此對(duì)象監(jiān)視器的所有者的線(xiàn)程來(lái)調(diào)用"說(shuō)明wait方法與notify方法必須在同步塊內(nèi)執(zhí)行,即synchronized(obj之內(nèi)).
調(diào)用對(duì)像wait方法后,當(dāng)前線(xiàn)程釋放對(duì)像鎖,進(jìn)入等待狀態(tài).直到其他線(xiàn)程(也只能是其他線(xiàn)程)通過(guò)Java notify喚醒方法,或 notifyAll.該線(xiàn)程重新獲得對(duì)像鎖.
繼續(xù)執(zhí)行,記得線(xiàn)程必須重新獲得對(duì)像鎖才能繼續(xù)執(zhí)行.因?yàn)閟ynchronized代碼塊內(nèi)沒(méi)有鎖是寸步不能走的.看一個(gè)很經(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)+"個(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;
- 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;
- }
以上就是對(duì)Java notify喚醒相關(guān)代碼的介紹。希望大家有所幫助。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流