close

共用同一個變數

 

thread A   處理完object a 後 要處理object b

thread B  處理完object b 後 要處理object a

 

發生時刻:當thead a 處理完a而thread B剛好處理完b,兩個就會鎖住

  • synchronized嵌套nested時
        synchronized (lefthand) {
            System.out.println(name + " takes up " + lefthand + " (left).");
            //兩個人都在等對方的資源
            synchronized (righthand) {
                System.out.println(name + " takes up " + righthand + " (right).");
                System.out.println(name + " is eating now, yam yam!");
                System.out.println(name + " puts down " + righthand + " (right).");
            }
            System.out.println(name + " puts down " + lefthand + " (left).");
        }
        }

http://twmht.github.io/blog/posts/multithread/deadlock.html

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 學習程式 的頭像
    學習程式

    程式學習日記,如果我幫助了你請讓我知道

    學習程式 發表在 痞客邦 留言(0) 人氣()