性格決定命運(yùn),氣度影響格局
          posts - 20, comments - 18, trackbacks - 0, articles - 1
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          sleep、join、yield舉例

          Posted on 2007-08-01 20:03 尚愛(ài)軍 閱讀(352) 評(píng)論(0)  編輯  收藏

          1.sleep
          static void sleep(long millis)
           
          sleep方法是靜態(tài)方法,說(shuō)明類(lèi)Thread可以調(diào)用。
          sleep舉例:
          import java.util.*;
          public class TestInterrupt {
            public static void main(String[] args) {
              MyThread thread = new MyThread();
              thread.start();                         
              try {Thread.sleep(10000);}    //主線程睡10秒。
              catch (InterruptedException e) {}
              thread.interrupt();
            }
          }

          class MyThread extends Thread {
           boolean flag = true;
            public void run(){
              while(flag){
                System.out.println("==="+new Date()+"===");
                try {
                  sleep(1000);
                } catch (InterruptedException e) {
                  return;
                }
              }
            }
          }

          2.join可并某個(gè)線程
          public class TestJoin {
            public static void main(String[] args) {
              MyThread2 t1 = new MyThread2("abcde");
              t1.start();
              try {
               t1.join();                                       //本來(lái)運(yùn)行完t1.start之后,就會(huì)出現(xiàn)主線程和t1線程并行的運(yùn)行。
              } catch (InterruptedException e) {}//但是join后,t1線程合并到主線程,主線程等t1運(yùn)行完后再運(yùn)行。
               
              for(int i=1;i<=10;i++){
                System.out.println("i am main thread");
              }
            }
          }
          class MyThread2 extends Thread {
            MyThread2(String s){
             super(s);
            }
           
            public void run(){
              for(int i =1;i<=10;i++){
                System.out.println("i am "+getName());
                try {
                 sleep(1000);
                } catch (InterruptedException e) {
                 return;
                }
              }
            }
          }

          3.yield方法
          讓出CPU,給其他線程運(yùn)行的機(jī)會(huì)。

          public class TestYield {
            public static void main(String[] args) {
              MyThread3 t1 = new MyThread3("t1");
              MyThread3 t2 = new MyThread3("t2");
              t1.start(); t2.start();
            }
          }
          class MyThread3 extends Thread {
            MyThread3(String s){super(s);}
            public void run(){
              for(int i =1;i<=100;i++){
                System.out.println(getName()+": "+i);
                if(i%10==0){
                  yield();
                }
              }
            }
          }

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 金坛市| 分宜县| 隆昌县| 庆云县| 唐山市| 茌平县| 皋兰县| 亚东县| 松桃| 泸定县| 罗山县| 雷山县| 松潘县| 凌云县| 万安县| 河东区| 鄯善县| 桂平市| 化德县| 南郑县| 讷河市| 喜德县| 房山区| 错那县| 平湖市| 广元市| 苍山县| 绥江县| 阿荣旗| 福清市| 屯门区| 军事| 沾化县| 东明县| 沁阳市| 商城县| 菏泽市| 阿城市| 通榆县| 阿合奇县| 泽州县|