魚躍于淵

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

          導航

          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          留言簿(1)

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          Thread synchronized demo

          Posted on 2008-12-06 11:31 魚躍于淵 閱讀(124) 評論(0)  編輯  收藏 所屬分類: j2se
           1 
           2 
           3 public class ThreadTest3 implements Runnable{
           4     
           5     Timer timer = new Timer();
           6     
           7     public static void main(String[] args){
           8         ThreadTest3 r1 = new ThreadTest3();
           9         Thread t1 = new Thread(r1);
          10         Thread t2 = new Thread(r1);
          11         
          12         t1.setName("t1");
          13         t2.setName("t2");
          14         t1.start();
          15         t2.start();
          16     }
          17     
          18     public void run(){
          19         timer.add(Thread.currentThread().getName());
          20     }
          21 }
          22 
          23 class Timer{
          24     private static int num = 0;
          25     public void add(String name){
          26         synchronized(this){
          27             num++ ;
          28             try{
          29                 Thread.sleep(1000);
          30             }catch(InterruptedException ex){
          31                 
          32             }
          33             System.out.println("===" + name + " shi di " + num + " ge fang wen Timer de Thread !");
          34             }
          35     }
          36 }
          主站蜘蛛池模板: 温宿县| 大悟县| 察哈| 镇雄县| 永顺县| 新乐市| 涟水县| 新田县| 莒南县| 曲靖市| 麻阳| 临泉县| 孝昌县| 岳阳市| 博兴县| 辉县市| 郯城县| 乌海市| 麦盖提县| 岳西县| 西乌珠穆沁旗| 博白县| 巴彦淖尔市| 高平市| 自治县| 林州市| 陇南市| 叙永县| 读书| 新安县| 凯里市| 阿拉善右旗| 岳阳市| 西宁市| 林西县| 丹东市| 准格尔旗| 诏安县| 松溪县| 宁海县| 宜兴市|