多線程--基礎篇1--簡單實現

//多線程操作類 繼承Thread類
public class TestThread extends Thread {
    private String name;//共享變量
    public TestThread(String name){
            this.name=name;
    }
    public void run(){
            for(int i = 0 ;i<100;i++){
                     System.out.println(this.name);
            }
    }
}
//主線程
public class MainThread {

      /**
       * @param args
      */
      public static void main(String[] args) {
             new TestThread("shixin").start();
             new TestThread("axue").start();

      }

}
//多線程操作類 繼承Runnable接口
public class TestThread implements Runnable{
      private String name;//共享變量
      public TestThread(String name){
            this.name=name;
      }
      public void run(){
            for(int i = 0 ;i<100;i++){
            System.out.println(this.name);
            }
      }
}
//主線程

public class MainThread {

    /**
    * @param args
    */
    public static void main(String[] args) {
        new Thread(new TestThread("shixin")).start();
        new Thread(new TestThread("axue")).start();

    }

}

爲什麼要用start()方法啓動多線程呢?

在JDK的安裝目錄下,src.zip是全部的java源程序,通過此代碼找到Thread類的Thread方法的定義:

public synchronized void start(){
      if(started)//判斷多線程是否已啓動
             throw new IllegalThreadStateException;
     started = true;
     start0();//調用start0()方法
 }
    private native void start0();//使用native關鍵字聲明的方法,沒有方法體

操作系統有很多種,Windows、Linux、Unix,既然多線程操作中要進行CPU資源的強佔,也就是說要等待CPU調度,那麼這些調度的操作時由各個操作系統的底層實現的,所以在Java程序中根本就沒法實現,那麼此時Java設計者定義了native關鍵字,使用此關鍵字表示可以調用操作系統的底層函數,那麼這樣的技術又稱爲JNI技術(Java Native Interface)。

兩種實現方式的區別於聯繫:

    在程序的開發中只要是多線程則肯定永遠以實現Runnable藉口爲正統操作,因爲實現Runnable接口相比繼承Thread類有如下好處:

(1)避免單繼承的侷限,一個類可以同時實現多個接口。

(2)適合於資源共享。

以賣票爲例,通過Thread類實現:

public class TestThread extends Thread{
     private int j=100;
     public void run(){
          for(int i = 0 ;i<100;i++){
               if(j>0){
               System.out.println("賣第"+j+"張票!");
                j--;   
               }
           }
    }
}

//new TestThread一次,共享變量會初始化一次,可理解爲一個機器;start一次,啓動一個線程

 public static void main(String[] args) {

        /*一共100張票,可一共被賣出去了200張票
         new TestThread().start();//一個機器的一個線程
         new TestThread().start();//另一個機器的一個線程
         /*這裏也可以如下實現,TestThread被new一次,即可理解爲一個機器啓動2個線程(start2次)賣100張票,不過這樣寫程序看起來彆扭

           TestThread m=new TestThread();
            new Thread(m).start();
            new Thread(m).start();

        */

         /*如下寫法不行的,會報IllegalThreadStateException異常

          TestThread m=new TestThread();
           m.start();
           m.start();

         */
 }

以賣票爲例,通過Runnable接口實現:

public class TestThread implements Runnable{
     private int j=100;
     public void run(){
          for(int i = 0 ;i<100;i++){
               if(j>0){
               System.out.println("賣第"+j+"張票!");
                j--;   
               }
           }
    }
}

/*一個機器2個線程共賣100張票*/

public static void main(String[] args) {
      TestThread m=new TestThread();
      new Thread(m).start();//一個線程
      new Thread(m).start();//一個線程
 }

實際上Runnable接口和Thread類之間還是存在聯繫的:

public class Thread extendsObject implements Runnable

發現Thread類也是Runnable接口的子類。

 

發佈了32 篇原創文章 · 獲贊 16 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章