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

          sleep、join、yield舉例

          Posted on 2007-08-01 20:03 尚愛軍 閱讀(353) 評論(0)  編輯  收藏

          1.sleep
          static void sleep(long millis)
           
          sleep方法是靜態方法,說明類Thread可以調用。
          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可并某個線程
          public class TestJoin {
            public static void main(String[] args) {
              MyThread2 t1 = new MyThread2("abcde");
              t1.start();
              try {
               t1.join();                                       //本來運行完t1.start之后,就會出現主線程和t1線程并行的運行。
              } catch (InterruptedException e) {}//但是join后,t1線程合并到主線程,主線程等t1運行完后再運行。
               
              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,給其他線程運行的機會。

          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();
                }
              }
            }
          }

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


          網站導航:
           
          主站蜘蛛池模板: 开平市| 依兰县| 东乌珠穆沁旗| 百色市| 曲靖市| 贵德县| 库尔勒市| 孟村| 芜湖县| 五寨县| 原平市| 弥勒县| 雅江县| 呼玛县| 来凤县| 临城县| 克拉玛依市| 喀喇沁旗| 邮箱| 瑞金市| 安康市| 眉山市| 沙田区| 邵阳县| 太谷县| 定安县| 都兰县| 崇礼县| 云梦县| 合作市| 博客| 启东市| 和龙市| 凭祥市| 金川县| 新龙县| 宁化县| 五原县| 无极县| 合水县| 周至县|