java something

          不要以為......很遙遠
          隨筆 - 23, 文章 - 1, 評論 - 2, 引用 - 0
          數(shù)據(jù)加載中……

          控制3個線程運行順序的Demo

          本程序可以控制3個線程按順序執(zhí)行, 代碼如下:

          public class Test3 {

           public static void main(String[] args) throws IOException {
            final Test obj = new Test();
            
            new Thread()
            {
             public void run()
             {
              obj.m1();
             }
            }.start();
            new Thread()
            {
             public void run()
             {
              obj.m2();
             }
            }.start();
            new Thread()
            {
             public void run()
             {
              obj.m3();
             }
            }.start();
            
           }

          }

          class Test
          {
           static int count;
           volatile int target = 1;
           synchronized void m1()
           { 
             for (int i = 0; i < 10; i++)
             {
              while (target == 2 || target == 3)
              {
               try {
                wait();
               } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }
              System.out.println("m1() =" + i);
              target = 2;
              notifyAll();
             }
           }
           
           synchronized void m2()
           {
            for (int i = 0; i < 10; i++)
            {
             while (target == 1 || target == 3)
             {
              try {
               wait();
              } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
              }
             }
             System.out.println("m2() =" + i);
             target = 3;
             notifyAll();
            }
           }
           
           synchronized void m3()
           {
            for (int i = 0; i < 10; i++)
            {
             while (target == 1 || target == 2)
             {
              try {
               wait();
              } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
              }
             }
             System.out.println("m3() =" + i);
             target = 1;
             notifyAll();
            }
           }
          }

          posted on 2011-09-02 02:27 Jamie 閱讀(1778) 評論(2)  編輯  收藏 所屬分類: 多線程

          評論

          # re: 控制3個線程運行順序的Demo  回復  更多評論   

          類似于信號量.
          2012-08-10 23:24 | zxogj

          # re: 控制3個線程運行順序的Demo  回復  更多評論   

          用retrantLock 也可以把。
          2012-08-10 23:24 | zxogj

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 会东县| 长岭县| 乌兰察布市| 宁远县| 辰溪县| 盈江县| 米易县| 阿拉善右旗| 周至县| 牙克石市| 卢湾区| 金坛市| 昔阳县| 昌乐县| 神农架林区| 福贡县| 双桥区| 阿合奇县| 江都市| 昌黎县| 巴楚县| 裕民县| 曲水县| 东明县| 通化县| 台南市| 乐亭县| 冷水江市| 安达市| 湾仔区| 安义县| 墨脱县| 河津市| 金溪县| 临洮县| 辽阳县| 奉新县| 来凤县| 屏山县| 乐山市| 上林县|