隨筆-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 閱讀(195) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 嘉善县| 蕲春县| 商南县| 依兰县| 宜州市| 江源县| 杂多县| 祁阳县| 辽阳市| 五华县| 盖州市| 怀宁县| 宁都县| 沙河市| 车致| 西丰县| 阜阳市| 东乌珠穆沁旗| 贺州市| 上高县| 京山县| 郧西县| 嘉荫县| 社会| 襄垣县| 泗洪县| 家居| 阳朔县| 乾安县| 南召县| 棋牌| 上饶市| 尼木县| 汾阳市| 长寿区| 巴青县| 南郑县| 敖汉旗| 九龙县| 房产| 深泽县|