魚躍于淵

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

          all method synchronized

          Posted on 2008-12-06 12:36 魚躍于淵 閱讀(111) 評論(0)  編輯  收藏 所屬分類: j2se
           1 
           2 public class TT1 implements Runnable{
           3     int b = 10 ;
           4     
           5     public synchronized void m1(){
           6         b = 1000 ;
           7         try{
           8             Thread.sleep(5000) ;
           9         }catch(InterruptedException ex){
          10             ex.printStackTrace() ;
          11         }
          12         System.out.println("m1 : b = " + b) ;
          13     }
          14     
          15     public void m2(){
          16         b = 2000 ;
          17         System.out.println("m2 : b = " + b ) ;
          18     }
          19     
          20     public void run(){
          21             m1() ;
          22     }
          23     
          24     public static void main(String[] args){
          25         TT1 tt = new TT1() ;
          26         Thread t= new Thread(tt) ;
          27         t.start() ;
          28         try{
          29             Thread.sleep(1000) ;/*que din m1 method was running !*/
          30         }catch(InterruptedException ex){
          31             ex.printStackTrace() ;
          32         }
          33         tt.m2() ;
          34     }
          35 }
          result :
          m2 : b = 2000
          m1 : b = 2000

           1 
           2 public class TT1 implements Runnable{
           3     int b = 10 ;
           4     
           5     public synchronized void m1(){
           6         b = 1000 ;
           7         try{
           8             Thread.sleep(5000) ;
           9         }catch(InterruptedException ex){
          10             ex.printStackTrace() ;
          11         }
          12         System.out.println("m1 : b = " + b) ;
          13     }
          14     
          15     public synchronized void m2(){
          16         b = 2000 ;
          17         System.out.println("m2 : b = " + b ) ;
          18     }
          19     
          20     public void run(){
          21             m1() ;
          22     }
          23     
          24     public static void main(String[] args){
          25         TT1 tt = new TT1() ;
          26         Thread t= new Thread(tt) ;
          27         t.start() ;
          28         try{
          29             Thread.sleep(1000) ;/*que din m1 method was running !*/
          30         }catch(InterruptedException ex){
          31             ex.printStackTrace() ;
          32         }
          33         tt.m2() ;
          34     }
          35 }
          result:
          m1 : b = 1000
          m2 : b = 2000

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


          網站導航:
           
          主站蜘蛛池模板: 温州市| 四平市| 远安县| 根河市| 迭部县| 南漳县| 台湾省| 肇州县| 孟州市| 嵩明县| 浑源县| 拜泉县| 金堂县| 淳化县| 盐池县| 吉木乃县| 汉沽区| 河北省| 西昌市| 浦县| 巨野县| 昔阳县| 乌兰县| 儋州市| 寿光市| 历史| 都江堰市| 彰武县| 华亭县| 白河县| 宜春市| 越西县| 陵川县| 新泰市| 东方市| 紫金县| 河北省| 深州市| 松江区| 永春县| 商河县|