Dev@Free

          zJun's Tech Weblog

          Java 多線程中的死鎖問題

          Java中的多線程使用 synchronized關鍵字實現同步.為了避免線程中使用共享資源的沖突,當線程進入 synchronized的共享對象時,將為共享對象加上鎖,阻止其他的線程進入該共享對象.但是,正因為這樣,當多線程訪問多個共享對象時,如果線程鎖定對象的順序處理不當話就有可能線程間相互等待的情況,即常說的: 死鎖現象.

          比如下面演示了一個典型的死鎖的情況:

          線程:MyThread
          /**
          ?*?共享線程
          ?*
          */

          Class?MyThread?
          extends ?Thread {
          ??
          private
          ?String?threadName;
          ??
          private
          ?String?shareObj1,shareObj2;
          ??
          public ?MyThread(String?name,String?shareObj1,String?shareObj2)
          {
          ????
          this .threadName? =
          ?name;
          ????
          this .shareObj1? =
          ?shareObj1;
          ????
          this .shareObj2? =
          ?shareObj2;
          ????start();
          ??}

          ??
          public ?run() {
          ????
          synchronized (shareObj1)
          {
          ??????System.out.println(threadName?
          + ? " locked?for?shareObj1 "
          );
          ????}

          ????Thread.sleep(
          1000 );
          ????
          synchronized (shareObj1)
          {
          ??????System.out.println(threadName?
          + ? " locked?for?shareObj2 "
          );
          ????}


          ??}

          }

          主程序:Main
          public ? class ?Main {
          ??
          private ?String?shareObj1? = ? " share?object?1 "
          ;
          ??
          private ?String?shareObj2? = ? " share?object?2 "
          ;
          ??
          public ? static ?main(String[]?args)
          {
          ????MyThread?thread1?
          = ? new ?MyThread( " thread1 "
          ,shareObj1,shareObj2);
          ????Thread.sleep(
          1000
          );
          ????MyThread?thread2?
          = ? new ?MyThread( " thread2 "
          ,shareObj1,shareObj2);
          ??}

          }

          運行結果:

          posted on 2007-07-24 19:02 zJun's帛羅閣 閱讀(3237) 評論(1)  編輯  收藏 所屬分類: Java基礎

          評論

          # re: Java 多線程中的死鎖問題 2011-06-27 17:00 vistor

          synchronized (shareObj1) {
          System.out.println(threadName + " locked for shareObj1 " );
          }
          Thread.sleep( 1000 );
          synchronized (shareObj1) {
          System.out.println(threadName + " locked for shareObj2 " );
          }
          代碼錯了吧!這會死鎖嗎?
          第二個是2并且new 2時順序顛倒  回復  更多評論   

          導航

          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          統計

          常用鏈接

          留言簿(15)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          博客

          文檔

          站點

          論壇

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 怀仁县| 宜兰县| 沐川县| 涿鹿县| 依安县| 肃南| 武平县| 红原县| 姚安县| 黑河市| 张家界市| 玉屏| 建平县| 广饶县| 武穴市| 石渠县| 通江县| 太康县| 衡阳县| 广德县| 鸡东县| 台南市| 金秀| 南川市| 利辛县| 连山| 巩留县| 沾益县| 嘉禾县| 莆田市| 乌兰浩特市| 辰溪县| 左权县| 呈贡县| 克山县| 浦北县| 同德县| 明光市| 罗田县| 平远县| 息烽县|