亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

java 多線程 wait,notify

selfimpr / 3084人閱讀

摘要:因?yàn)樯婕暗綄?duì)象鎖,一定要在里面進(jìn)行使用。必須暫定當(dāng)前正在執(zhí)行的線程并釋放資源鎖讓其他線程可以有機(jī)會(huì)運(yùn)行喚醒線程共享變量線程通訊標(biāo)識(shí)線程生產(chǎn)者線程消費(fèi)者請(qǐng)求結(jié)果

因?yàn)樯婕暗綄?duì)象鎖,Wait、Notify一定要在synchronized里面進(jìn)行使用。

Wait必須暫定當(dāng)前正在執(zhí)行的線程,并釋放資源鎖,讓其他線程可以有機(jī)會(huì)運(yùn)行

notify/notifyall: 喚醒線程

共享變量

public class ShareEntity {
    private String name;
    // 線程通訊標(biāo)識(shí)
    private Boolean flag = false;

    public ShareEntity() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }
}

線程1(生產(chǎn)者)

public class CommunicationThread1 extends Thread{

    private ShareEntity shareEntity;
    public CommunicationThread1(ShareEntity shareEntity) {
        this.shareEntity = shareEntity;
    }

    @Override
    public void run() {
        int num = 0;
        while (true) {
            synchronized (shareEntity) {
                if (shareEntity.getFlag()) {
                    try {
                        shareEntity.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (num % 2 == 0)
                    shareEntity.setName("thread1-set-name-0");
                else
                    shareEntity.setName("thread1-set-name-1");
                num++;
                shareEntity.setFlag(true);
                shareEntity.notify();
            }
        }
    }
}

線程2(消費(fèi)者)

public class CommunicationThread2 extends Thread{
    private ShareEntity shareEntity;
    public CommunicationThread2(ShareEntity shareEntity) {
        this.shareEntity = shareEntity;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (shareEntity) {
                if (!shareEntity.getFlag()) {
                    try {
                        shareEntity.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(shareEntity.getName());
                shareEntity.setFlag(false);
                shareEntity.notify();
            }
        }
    }
}

請(qǐng)求

@RequestMapping("test-communication")
    public void testCommunication() {
        ShareEntity shareEntity = new ShareEntity();
        CommunicationThread1 thread1 = new CommunicationThread1(shareEntity);
        CommunicationThread2 thread2 = new CommunicationThread2(shareEntity);
        thread1.start();
        thread2.start();
    }

結(jié)果

thread1-set-name-0
thread1-set-name-1
thread1-set-name-0
thread1-set-name-1
thread1-set-name-0
thread1-set-name-1
thread1-set-name-0

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/74700.html

相關(guān)文章

  • Java線程學(xué)習(xí)(四)等待/通知(wait/notify)機(jī)制

    摘要:運(yùn)行可運(yùn)行狀態(tài)的線程獲得了時(shí)間片,執(zhí)行程序代碼。阻塞的情況分三種一等待阻塞運(yùn)行的線程執(zhí)行方法,會(huì)把該線程放入等待隊(duì)列中。死亡線程方法執(zhí)行結(jié)束,或者因異常退出了方法,則該線程結(jié)束生命周期。死亡的線程不可再次復(fù)生。 系列文章傳送門(mén): Java多線程學(xué)習(xí)(一)Java多線程入門(mén) Java多線程學(xué)習(xí)(二)synchronized關(guān)鍵字(1) java多線程學(xué)習(xí)(二)synchronized關(guān)鍵...

    PiscesYE 評(píng)論0 收藏0
  • Java 線程編程之:notifywait 用法

    摘要:和簡(jiǎn)介和均為的方法暫停一個(gè)線程喚醒一個(gè)線程從以上的定義中,我們可以了解到以下事實(shí)想要使用這兩個(gè)方法,我們需要先有一個(gè)對(duì)象。在中任何一個(gè)時(shí)刻,對(duì)象的控制權(quán)只能被一個(gè)線程擁有。若有多個(gè)線程處于此控制權(quán)下的狀態(tài),只有一個(gè)會(huì)被喚醒。 最近看帖子,發(fā)現(xiàn)一道面試題: 啟動(dòng)兩個(gè)線程, 一個(gè)輸出 1,3,5,7…99, 另一個(gè)輸出 2,4,6,8…100 最后 STDOUT 中按序輸出 1,2,3,4...

    eccozhou 評(píng)論0 收藏0
  • Java線程基礎(chǔ)(一)——線程與鎖

    摘要:一線程的基本概念單線程簡(jiǎn)單的說(shuō),單線程就是進(jìn)程中只有一個(gè)線程。多線程由一個(gè)以上線程組成的程序稱(chēng)為多線程程序。當(dāng)線程調(diào)用完方法進(jìn)入后會(huì)自動(dòng)釋放鎖,線程獲得鎖。 一、線程的基本概念 1.1 單線程 簡(jiǎn)單的說(shuō),單線程就是進(jìn)程中只有一個(gè)線程。單線程在程序執(zhí)行時(shí),所走的程序路徑按照連續(xù)順序排下來(lái),前面的必須處理好,后面的才會(huì)執(zhí)行。 Java示例: public class SingleThrea...

    WelliJhon 評(píng)論0 收藏0
  • Java 線程核心技術(shù)梳理(附源碼)

    摘要:本文對(duì)多線程基礎(chǔ)知識(shí)進(jìn)行梳理,主要包括多線程的基本使用,對(duì)象及變量的并發(fā)訪問(wèn),線程間通信,的使用,定時(shí)器,單例模式,以及線程狀態(tài)與線程組。源碼采用構(gòu)建,多線程這部分源碼位于模塊中。通知可能等待該對(duì)象的對(duì)象鎖的其他線程。 本文對(duì)多線程基礎(chǔ)知識(shí)進(jìn)行梳理,主要包括多線程的基本使用,對(duì)象及變量的并發(fā)訪問(wèn),線程間通信,lock的使用,定時(shí)器,單例模式,以及線程狀態(tài)與線程組。 寫(xiě)在前面 花了一周時(shí)...

    Winer 評(píng)論0 收藏0
  • java 線程編程核心技術(shù) 3—線程間通信

    摘要:在從返回前,線程與其他線程競(jìng)爭(zhēng)重新獲得鎖。就緒隊(duì)列存儲(chǔ)了將要獲得鎖的線程,阻塞隊(duì)列存儲(chǔ)了被阻塞的線程。當(dāng)線程呈狀態(tài),調(diào)用線程對(duì)象的方法會(huì)出現(xiàn)異常。在執(zhí)行同步代碼塊過(guò)程中,遇到異常而導(dǎo)致線程終止,鎖也會(huì)被釋放。 方法wait()的作用是使當(dāng)前執(zhí)行代碼的線程進(jìn)行等待,wait()方法是Object類(lèi)的方法,該方法用來(lái)將當(dāng)前線程置入預(yù)執(zhí)行隊(duì)列中,并且在wait()所在的代碼行處停止執(zhí)行,直...

    Dogee 評(píng)論0 收藏0
  • java線程(7)wait()、notify()和notityALL()

    摘要:已經(jīng)在上面有提到過(guò),和的作用是喚醒正在的線程,是隨機(jī)喚醒線程中的一個(gè),則是喚醒全部。釋放和不釋放鎖在多線程的操作中,鎖的釋放與否是必須要清楚的,是會(huì)釋放鎖,而則不會(huì)。 wait wait方法是Object中的方法,這個(gè)方法的功能特性:1).執(zhí)行wait方法的前提是當(dāng)前線程已經(jīng)獲取到對(duì)象的鎖,也就是wait方法必須在synchronized修飾的代碼塊或者方法中使用。2).執(zhí)行wait之...

    LiuRhoRamen 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<