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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

子線程的join方法阻塞主線程,如果在另一個子線程中notify那個子線程,會報監(jiān)視器狀態(tài)異常

子線程的join方法阻塞主線程,如果在另一個子線程中notify那個子線程,會報監(jiān)視器狀態(tài)異常

動漫人物 2019-03-12 09:15:59
一.問題當主線程調用子線程的join方法時,其實還是調用子線程的wait方法來阻塞主線程,那么有兩個問題:a.如果我在另一個子線程中獲得當前子線程對象,并調用線程的notify方法,是不是可以解除子線程的阻塞,經(jīng)測試會報監(jiān)視器狀態(tài)異常。b.子線程是個單獨的對象,為啥會阻塞主線程呢?又不存在共享資源競爭,尤其是Thread中join方法是個普通的synchronized方法二、代碼public class JoinTest {public static void main(String[] args) throws InterruptedException {    MyThread3 thread=new MyThread3();    NotifyThread nt=new NotifyThread(thread);    thread.start();    nt.start();    thread.join();    for(int i=0;i<3;i++){        System.out.println(Thread.currentThread().getName() + "線程第" + i + "次執(zhí)行!");    }}}class NotifyThread extends Thread{Thread myThread ;public NotifyThread(Thread myThread){    this.myThread=myThread;}public void run(){    try {        System.out.println("休眠開始");        Thread.sleep(3000);        System.out.println("休眠結束");    } catch (InterruptedException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    myThread.notify();    System.out.println("已喚醒,讓Join失效");}}class MyThread3 extends Thread {@Overridepublic void run() {        for (int i = 0; i < 10; i++) {        try {            System.out.println(this.getName() + "線程第" + i + "次執(zhí)行!");            Thread.sleep(1000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}三、異常Thread-0線程第0次執(zhí)行!休眠開始Thread-0線程第1次執(zhí)行!Thread-0線程第2次執(zhí)行!休眠結束Exception in thread "Thread-1" java.lang.IllegalMonitorStateExceptionat java.lang.Object.notify(Native Method)at thread.com.simple.NotifyThread.run(JoinTest.java:29)Thread-0線程第3次執(zhí)行!Thread-0線程第4次執(zhí)行!Thread-0線程第5次執(zhí)行!Thread-0線程第6次執(zhí)行!Thread-0線程第7次執(zhí)行!Thread-0線程第8次執(zhí)行!Thread-0線程第9次執(zhí)行!main線程第0次執(zhí)行!main線程第1次執(zhí)行!main線程第2次執(zhí)行!
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經(jīng)驗 獲得超2個贊

先談為什么“喚醒”不了,notify不會“喚醒”MyThread3,因為阻塞不就是join方法的使命么?再說MyThread3也沒有休眠,不是一直在執(zhí)行么,何來“喚醒”之說!


最后來看看join方法的實現(xiàn),或許對你理解有幫助:


public final synchronized void join(long millis) throws InterruptedException {

    long base = System.currentTimeMillis();

    long now = 0;


    if (millis < 0) {

        throw new IllegalArgumentException("timeout value is negative");

    }


    if (millis == 0) {

        while (isAlive()) {

            wait(0);

        }

    } else {

        while (isAlive()) {

            long delay = millis - now;

            if (delay <= 0) {

                break;

            }

            wait(delay);

            now = System.currentTimeMillis() - base;

        }

    }

}

再來談為什么有異常,其實已經(jīng)比較明確了,看注釋:


/**

 * Thrown to indicate that a thread has attempted to wait on an

 * object's monitor or to notify other threads waiting on an object's

 * monitor without owning the specified monitor.

 */

當前線程不是該對象monitor所有者時,試圖調用其wait或者notify方法就會報這個錯,解決異常只要拿到所有者就好了,常見方法:


public class NotifyThread extends Thread{


    Thread myThread ;

    public NotifyThread(Thread myThread){

        this.myThread=myThread;

    }


    public void run(){

        try {

            System.out.println("休眠開始");

            Thread.sleep(3000);

            System.out.println("休眠結束");

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        synchronized(myThread){

            myThread.notify();

        }

        System.out.println("已喚醒,讓Join失效");

    }

}


查看完整回答
反對 回復 2019-04-19
  • 1 回答
  • 0 關注
  • 518 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號