第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

同時(shí)執(zhí)行Thread.interrupt() Object.notify(),為什么會(huì)有兩個(gè)結(jié)果?

同時(shí)執(zhí)行Thread.interrupt() Object.notify(),為什么會(huì)有兩個(gè)結(jié)果?

UYOU 2022-01-12 16:07:09
public class WaitNotifyAll {    private static volatile Object resourceA = new Object();    public static void main(String[] args) throws Exception {        Thread threadA = new Thread(new Runnable() {            @Override            public void run() {                synchronized (resourceA) {                    try {                        System.out.println("threadA begin wait");                        resourceA.wait();                        System.out.println("threadA end wait");                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        });        Thread threaB = new Thread(new Runnable() {            @Override            public void run() {                synchronized (resourceA) {                    System.out.println("threadC begin notify");                    threadA.interrupt();                    resourceA.notify();                }            }        });        threadA.start();        Thread.sleep(1000);        threaB.start();        System.out.println("main over");    } }這里有兩種可能的結(jié)果:拋出中斷異常正常終止為什么?我不明白。當(dāng)threadA被中斷時(shí),結(jié)果應(yīng)該拋出InterruptedException。但有時(shí)執(zhí)行這個(gè)程序,它可以正常完成。環(huán)境:java8,mac
查看完整描述

2 回答

?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

當(dāng)線程同時(shí)接收到中斷和通知時(shí),行為可能會(huì)有所不同。

請(qǐng)參考https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3

信用 - 并發(fā)興趣郵件列表上的 Alex Otenko


查看完整回答
反對(duì) 回復(fù) 2022-01-12
?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

因?yàn)橹匦屡判?。在正常終止編譯器重新排序指令中斷和通知中,中斷在工作線程上調(diào)用并且沒有中斷異常拋出。嘗試通過讀取 volatile 變量來(lái)禁止重新排序,您總是會(huì)遇到異常中斷。


public class WaitNotifyAll {

private static volatile Object resourceA = new Object();


public static void main(String[] args) throws Exception {

    Thread threadA = new Thread(new Runnable() {

        @Override

        public void run() {

            synchronized (resourceA) {

                try {

                    System.out.println("threadA begin wait");

                    resourceA.wait();

                    System.out.println("threadA end wait");

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    });


    Thread threaB = new Thread(new Runnable() {

        @Override

        public void run() {

            synchronized (resourceA) {

                System.out.println("threadC begin notify");

                threadA.interrupt();

                System.out.print(resourceA);

                resourceA.notify();

            }

        }

    });


    threadA.start();


    Thread.sleep(1000);


    threaB.start();


    System.out.println("main over");

}

}


查看完整回答
反對(duì) 回復(fù) 2022-01-12
  • 2 回答
  • 0 關(guān)注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)