<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-49  文章-56  trackbacks-0

    java.lang.Thread

    【創建新線程的方法有兩種】

    1 定義線程類實現Runnable接口
    public class MyThred implements Runnable{
         @overwrite
         public void run(){}
    }

    起用線程:
    MyThred myThred=new MyThred();
    Thread t=new Thread(myThred);
    t.start();


    2 定義線程類繼承Thread類并重寫其run方法
    public class MyThred extends Thread{
         @overwrite
         public void run(){}
    }

    起用線程:
    MyThred myThred=new MyThred();
    myThred.start();

    說明:一般使用接口來實現
    -------------------------------------------------
    【線程狀態轉換】

    【線程控制基本方法】:
    isAlive() 判斷線程是否還“活”著
    getPriority() 獲得線程的優先級
    setPriority() 設置線程的優先級
    Thread.sleep() 將當前線程睡眠指定的毫秒數
    join()  調用某線程的該方法,將當前線程與該線程“合并”,即等待
            該線程線束,再恢復當前線程的運行。
    yield() 讓出CPU,當前線程進入就緒隊列等待調度
    wait() 當膠線程進入對象的wait pool。
    notify()/notifyAll() 喚醒對象的wait pool中的一個/所有等待線程。
    Thread.currentThread() 得到當前線程
    ----------------------------------------------------------------
    【sleep/join/yield 方法】

    sleep示例:
    public class TestInterrupt {
      public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        //主線程睡眠
        try {Thread.sleep(10000);}
        catch (InterruptedException e) {}
        //停下當前線程,這個方法一般不建議使用
        thread.interrupt();
        //使用這個結束線程
        shutDown();
      }
    }

    class MyThread extends Thread {
     boolean flag = true;
      public void run(){
        while(flag){
          System.out.println("==="+new Date()+"===");
          try {
     //當前子線程睡眠
            sleep(1000);
     //sleep時被打斷會拋InterruptedException
          } catch (InterruptedException e) {
            return;
          }
        }
      }
      //結束線程的方法
      public void shutDown(){
          flag = false;
      }
    }

    jion示例:
    public class TestJoin {
      public static void main(String[] args) {
        MyThread2 t1 = new MyThread2("abcde");
        t1.start();
        try {
            //當前線程合并到主線程,相當于方法調用
         t1.join();
        } catch (InterruptedException e) {}
         
        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;
          }
        }
      }
    }

    yield示例:
    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();
          }
        }
      }
    }

    --------------------------------------------------------------
    【線程的優先級別】getPriority()/setProority(int newPriority)

    線程的優先級用數字表示,范圍從1到10,默認為5.
    Thread.MIN_PRIORITY=1
    Thread.MAX_PRIORITY=10
    Thread.NORM_PRIORITY=5

    示例:
    public class TestPriority {
     public static void main(String[] args) {
      Thread t1 = new Thread(new T1());
      Thread t2 = new Thread(new T2());
      //將t1的優先級提高3級
      t1.setPriority(Thread.NORM_PRIORITY + 3);
      t1.start();
      t2.start();
     }
    }

    class T1 implements Runnable {
     public void run() {
      for(int i=0; i<1000; i++) {
       System.out.println("T1: " + i);
      }
     }
    }

    class T2 implements Runnable {
     public void run() {
      for(int i=0; i<1000; i++) {
       System.out.println("------T2: " + i);
      }
     }
    }

    --------------------------------------------------------------------
    【線程的同步】未看完

    posted on 2009-11-29 21:30 junly 閱讀(199) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 亚洲综合久久1区2区3区| 国产AV无码专区亚洲AVJULIA| 久久伊人久久亚洲综合| 激情吃奶吻胸免费视频xxxx| 91热久久免费精品99| 国产l精品国产亚洲区在线观看| 免费国产在线精品一区| 成人永久免费高清| 黄色免费网站在线看| 国产网站免费观看| 免费人妻精品一区二区三区| 成人免费午夜无码视频| 亚洲av无码一区二区三区天堂古代| 中国人xxxxx69免费视频| 亚洲成a人片在线观| 在线观看免费人成视频色| 日本亚洲免费无线码| 毛片基地免费观看| 美景之屋4在线未删减免费 | 222www免费视频| 亚洲国产亚洲综合在线尤物| 久久精品无码专区免费青青| 亚洲AV永久无码精品一百度影院 | 人妻仑乱A级毛片免费看| 最新国产AV无码专区亚洲| APP在线免费观看视频| 久久久久亚洲av无码专区导航| 久久综合给合久久国产免费| 婷婷久久久亚洲欧洲日产国码AV| 日韩中文字幕免费视频| 国产精品久久亚洲不卡动漫 | 日韩高清在线免费观看| 高h视频在线免费观看| 亚洲情XO亚洲色XO无码| 在线观看免费视频资源| 亚洲人成色4444在线观看| 国产成人高清亚洲| 日本成年免费网站| 72pao国产成视频永久免费| 亚洲丁香色婷婷综合欲色啪| 99re这里有免费视频精品 |