魚躍于淵

          First know how, Second know why !
          posts - 0, comments - 1, trackbacks - 0, articles - 49

          Thread DeadLock demo

          Posted on 2008-12-06 11:54 魚躍于淵 閱讀(209) 評論(0)  編輯  收藏 所屬分類: j2se
           1 
           2 public class ThreadDeadLock implements Runnable{
           3     
           4     private int flag ;
           5     static Object o1 = new Object() ;
           6     static Object o2 = new Object() ;
           7     
           8     public void run(){
           9         System.out.println("flag = " + flag) ;
          10         if(flag==1){
          11             synchronized(o1){
          12                 try{
          13                     Thread.sleep(1000) ;
          14                 }catch(InterruptedException ex){
          15                     ex.printStackTrace() ;
          16                 }
          17                 synchronized(o2){
          18                     System.out.println("Hello , I'm Thread t1, and I synchronized o2 !");
          19                 }
          20             }
          21             
          22         }
          23         
          24         if(flag==2){
          25             synchronized(o2){
          26                 try{
          27                     Thread.sleep(1000);
          28                 }catch(InterruptedException ex){
          29                     ex.printStackTrace() ;
          30                 }
          31                 synchronized(o1){
          32                 System.out.println("Hello , I'm Thread t2, and I synchronized o1 !") ;
          33             }
          34             }
          35             
          36         }
          37     }
          38     
          39     public static void main(String[] args){
          40         ThreadDeadLock dl1 = new ThreadDeadLock() ;
          41         ThreadDeadLock dl2 = new ThreadDeadLock() ;
          42         dl1.flag = 1 ;
          43         dl2.flag = 2 ;
          44         Thread t1 = new Thread(dl1) ;
          45         Thread t2 = new Thread(dl2) ;
          46         t1.start() ;
          47         t2.start() ;
          48     }
          49 }

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 万全县| 潼关县| 临澧县| 喜德县| 乃东县| 高邮市| 岫岩| 南漳县| 庄河市| 永州市| 承德市| 五寨县| 南靖县| 惠水县| 西安市| 曲靖市| 彭州市| 丰顺县| 永福县| 南召县| 金乡县| 宣恩县| 东阳市| 无极县| 博湖县| 青川县| 凌海市| 楚雄市| 许昌县| 搜索| 黔江区| 樟树市| 高陵县| 乌拉特中旗| 洮南市| 墨竹工卡县| 福清市| 蕉岭县| 嘉义县| 巍山| 定兴县|