<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

    【創(chuàng)建新線程的方法有兩種】

    1 定義線程類實現(xiàn)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();

    說明:一般使用接口來實現(xiàn)
    -------------------------------------------------
    【線程狀態(tài)轉(zhuǎn)換】

    【線程控制基本方法】:
    isAlive() 判斷線程是否還“活”著
    getPriority() 獲得線程的優(yōu)先級
    setPriority() 設(shè)置線程的優(yōu)先級
    Thread.sleep() 將當(dāng)前線程睡眠指定的毫秒數(shù)
    join()  調(diào)用某線程的該方法,將當(dāng)前線程與該線程“合并”,即等待
            該線程線束,再恢復(fù)當(dāng)前線程的運行。
    yield() 讓出CPU,當(dāng)前線程進入就緒隊列等待調(diào)度
    wait() 當(dāng)膠線程進入對象的wait pool。
    notify()/notifyAll() 喚醒對象的wait pool中的一個/所有等待線程。
    Thread.currentThread() 得到當(dāng)前線程
    ----------------------------------------------------------------
    【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) {}
        //停下當(dāng)前線程,這個方法一般不建議使用
        thread.interrupt();
        //使用這個結(jié)束線程
        shutDown();
      }
    }

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

    jion示例:
    public class TestJoin {
      public static void main(String[] args) {
        MyThread2 t1 = new MyThread2("abcde");
        t1.start();
        try {
            //當(dāng)前線程合并到主線程,相當(dāng)于方法調(diào)用
         t1.join();
        } catch (InterruptedException e) {}
         
        for(int i=1;i<=10;i++){
          System.out.println("i am main thread");
        }
      }
    }
    class MyThread2 extends Thread {
      //給當(dāng)前線程起個別名
      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) {
        //可以用同一個線程類創(chuàng)建不同的線程對象,分別執(zhí)行
        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){
     //讓出時間給其它線程執(zhí)行
            yield();
          }
        }
      }
    }

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

    線程的優(yōu)先級用數(shù)字表示,范圍從1到10,默認(rèn)為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的優(yōu)先級提高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 閱讀(194) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 亚洲国产高清精品线久久| 69影院毛片免费观看视频在线| 福利免费观看午夜体检区| 亚洲电影在线播放| 免费人成在线观看网站品爱网| 亚洲色爱图小说专区| 中文字幕不卡高清免费| 在线观看亚洲天天一三视| 一级特黄录像视频免费| 精品国产亚洲男女在线线电影| 一级黄色免费大片| 日韩一卡2卡3卡4卡新区亚洲 | 免费视频精品一区二区三区 | 亚洲国产精品免费观看| 亚洲日韩中文字幕| 国国内清清草原免费视频99| 激情亚洲一区国产精品| 性感美女视频免费网站午夜| 亚洲欧美成人综合久久久| 免费毛片在线播放| 中美日韩在线网免费毛片视频| 亚洲熟妇av一区二区三区| 久久久久免费精品国产 | 亚洲美女自拍视频| 免费视频专区一国产盗摄| 亚洲Av无码国产一区二区| 亚洲AV无码不卡在线观看下载| 中国极品美軳免费观看| 18gay台湾男同亚洲男同| 久久精品免费一区二区喷潮| 在线亚洲精品视频| 亚洲av午夜福利精品一区| 黄在线观看www免费看| 国产精品国产亚洲区艳妇糸列短篇| 亚洲A∨精品一区二区三区| 水蜜桃视频在线观看免费播放高清| 亚洲精品国产福利在线观看| 永久免费AV无码网站在线观看| 亚洲免费一区二区| 亚洲国产成人综合| 亚洲精品视频在线看|