<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    cuiyi's blog(崔毅 crazycy)

    記錄點(diǎn)滴 鑒往事之得失 以資于發(fā)展
    數(shù)據(jù)加載中……

    線程中的current thread not owner異常錯(cuò)誤

    非常感謝你的閱讀,如果你覺得好或者對(duì)你有幫助,請(qǐng)積極給一個(gè)留言反饋以示鼓勵(lì)。?

    多線程常用的一些方法: wait(),wait(long),notify(),notifyAll()等

    這些方法是當(dāng)前類的實(shí)例方法,

    wait()????? 是使持有對(duì)象鎖的線程釋放鎖;
    wait(long)是使持有對(duì)象鎖的線程釋放鎖時(shí)間為long(毫秒)后,再次獲得鎖,wait()和wait(0)等價(jià);
    notify()??? 是喚醒一個(gè)正在等待該對(duì)象鎖的線程,如果等待的線程不止一個(gè),那么被喚醒的線程由jvm確定;
    notifyAll? 是喚醒所有正在等待該對(duì)象鎖的線程.

    并且應(yīng)該優(yōu)先使用notifyAll()方法,因?yàn)閱拘阉芯€程比喚醒一個(gè)線程更容易讓jvm找到最適合被喚醒的線程.

    對(duì)于上述方法,只有在當(dāng)前線程中才能使用,否則報(bào)運(yùn)行時(shí)錯(cuò)誤java.lang.IllegalMonitorStateException:?current?thread?not?owner.

    從實(shí)現(xiàn)角度來分析:
    在線程調(diào)用wait()方法時(shí),需要把它放到一個(gè)同步段里,即應(yīng)該在調(diào)用前使用
    1synchroed(this){
    2??thread.wait();
    3??
    4}

    5

    否則將會(huì)出現(xiàn)"java.lang.IllegalMonitorStateException: current thread not owner"的異常。


    Thanks very much to visit blog,? welcome your feedback,? your feedback is the Driver && Power to me

    posted on 2006-10-08 19:21 crazycy 閱讀(13833) 評(píng)論(24)  編輯  收藏 所屬分類: JavaSE語言

    評(píng)論

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    您的內(nèi)容對(duì)我有幫助,謝謝!
    2007-05-30 14:22 | shaoxiongwang

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    thak you !
    2007-07-12 15:14 | netiger

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    謝謝^_^
    2007-08-01 17:17 | txd

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    3ks
    2008-01-21 15:59 | ronchie

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    ths
    2008-03-23 20:48 | er

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    謝謝啦
    2008-03-31 16:46 | hxfein

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    Once I was given an interview in IBM, the program is just to write interaction between productor and consumer, I met the problem; and, I solved at that time, but sadly I cannot give the explation about this.
    2008-03-31 22:13 | crazycy

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    非常感謝!受益匪淺!我在用友面試時(shí)就遇到這個(gè)問題!但是沒有答上來!
    2008-04-16 18:05 | zhanggl

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    Thanks! It works!
    2008-08-01 12:14 | Shengkai Zhu

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    謝謝!
    2008-10-08 13:23 | freemelody

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    感謝
    2008-11-11 10:11 | 筋抽的狗狗

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    e感謝,根據(jù)您的幫助,我解決了此異常。
    2008-11-20 13:14 | autumn

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    Anyway, you are best.
    2009-03-23 09:43 | IMGM

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    thanks you!
    it is very helpfull
    2009-06-10 18:03 | duanhw

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    thank you
    2009-06-19 09:49 | mouse

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    只有在當(dāng)前線程中才能使用???
    public class TaskMonitor extends Thread {
    private CreateTask create = null;
    public TaskMonitor(CreateTask create){
    this.create = create;
    }
    //任務(wù)監(jiān)控,每隔2妙,通知CreateTask創(chuàng)造1條任務(wù)放到隊(duì)列中
    public void run() {
    while(true){
    try {
    Thread.sleep(2000);
    System.out.println("TaskMonitor");
    synchronized (create) {
    create.notify();
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    其中的create就是另一個(gè)線程的對(duì)象
    2009-06-22 13:49 | 乞丐

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    Thank you.
    2009-09-16 16:59 | winstone

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    good!!!
    2009-11-22 10:17 | missme

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    謝謝你的分析,但是還不是很詳細(xì),如果能再詳細(xì)一點(diǎn)就好了。
    2010-02-04 23:18 | netant83

    # re: 線程中的current thread not owner異常錯(cuò)誤[未登錄]  回復(fù)  更多評(píng)論   

    謝謝!
    2010-03-20 15:20 | 1

    # re: 線程中的current thread not owner異常錯(cuò)誤[未登錄]  回復(fù)  更多評(píng)論   

    你說的不對(duì),兩個(gè)線程共用一個(gè)鎖,是可以的。
    public A extends Thread{
    public static Object lock = new Object();
    public void run(){
    synchronized(lock){
    try {
    lock.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    public B extends Thread{
    public void run(){
    Object lock = A.lock;
    synchronized(lock){
    lock.notify();
    }
    }
    }
    2010-07-02 12:24 | tony

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    很好的內(nèi)容,有幫助
    2010-07-26 10:52 | wharry

    # re: 線程中的current thread not owner異常錯(cuò)誤[未登錄]  回復(fù)  更多評(píng)論   

    你說的很好,對(duì)我很有用!非常感謝!
    2011-04-02 23:08 | solo

    # re: 線程中的current thread not owner異常錯(cuò)誤  回復(fù)  更多評(píng)論   

    很有用
    2015-09-10 14:32 | 11
    主站蜘蛛池模板: 99re6在线精品免费观看| 免费A级毛片无码久久版| 一个人看的免费视频www在线高清动漫 | av网站免费线看| 亚洲精品国产综合久久久久紧| 亚洲a在线视频视频| 亚洲国产a级视频| 在线观看人成网站深夜免费| 91福利免费体验区观看区| 国产精品福利在线观看免费不卡| 亚洲人成人网站18禁| 亚洲H在线播放在线观看H| 久久精品国产亚洲AV麻豆~| 亚洲女同成人AⅤ人片在线观看| 天天看免费高清影视| 99久久精品日本一区二区免费 | 四虎免费久久影院| 午夜神器成在线人成在线人免费| 182tv免费观看在线视频| 99视频在线免费观看| fc2成年免费共享视频18| 黄色毛片免费在线观看| 亚洲Av永久无码精品一区二区| 丁香婷婷亚洲六月综合色| 亚洲字幕在线观看| 亚洲无成人网77777| 亚洲视频在线观看地址| 亚洲图片一区二区| 久久久久亚洲av无码专区| 久久亚洲国产精品| 亚洲ⅴ国产v天堂a无码二区| 亚洲av永久无码精品古装片| 九月丁香婷婷亚洲综合色| 亚洲第一极品精品无码久久| 国产亚洲免费的视频看| 亚洲国产精品va在线播放| 亚洲成AV人片在| 久久亚洲国产精品成人AV秋霞| 99久久亚洲综合精品成人网| 337p欧洲亚洲大胆艺术| 久久国产亚洲高清观看|