閱讀313 返回首頁    go 阿裏雲 go 技術社區[雲棲]


多線程Runnable和Thread產生線程

 

https://dev.yesky.com/186/2547686.shtml

public class Test {

 public static void main(String[] args) throws Exception{
  MyThread mt = new MyThread();
  mt.start();
  mt.join();
  Thread.sleep(3000);
  mt.start();
 }
}

  當線程對象mt運行完成後,我們讓主線程休息一下,然後我們再次在這個線程對象上啟動線程。結果我們看到:

  Exception in thread "main" java.lang.IllegalThreadStateException

  也就是這種線程對象一時運行一次完成後,它就再也不能運行第二次了。我們可以看一下它有具體實現:

public synchronized void start() {
 if (started)
  throw new IllegalThreadStateException();
  started = true;
  group.add(this);
  start0();
 }

  一個Thread的實例一旦調用start()方法,這個實例的started標記就標記為true,事實中不管這個線程後來有沒有執行到底,隻要調用了一次start()就再也沒有機會運行了,這意味著:

  [通過Thread實例的start(),一個Thread的實例隻能產生一個線程]

  那麼如果要在一個實例上產生多個線程(也就是我們常說的線程池),我們應該如何做呢?這就是Runnable接口給我們帶來的偉大的功能。

class R implements Runnable{
 private int x = 0;
 public void run(){
  for(int i=0;i<100;i++){
    try{
     Thread.sleep(10);
    }catch(Exception e){}
    System.out.println(x++);
  }
 }
}

  正如它的名字一樣,Runnable的實例是可運行的,但它自己並不能直接運行,它需要被Thread對象來包裝才行運行:

public class Test {
 public static void main(String[] args) throws Exception{
  new Thread(new R()).start();
 }
}

  當然這個結果和mt.start()沒有什麼區別。但如果我們把一個Runnable實例給Thread對象多次包裝,我們就可以看到它們實際是在同一實例上啟動線程:

public class Test {
 public static void main(String[] args) throws Exception{
  R r = new R();
  for(int i=0;i<10;i++)
    new Thread(r).start();
 }
}

  x是實例對象,但結果是x被加到了999,說明這10個線程是在同一個r對象上運行的。請大家注意,因為這個例子是在單CPU上運行的,所以沒有對多個線程同時操作共同的對象進行同步。這裏是為了說明的方便而簡化了同步,而真正的環境中你無法預知程序會在什麼環境下運行,所以一定要考慮同步。

  到這裏我們做一個完整的例子來說明線程產生的方式不同而生成的線程的區別:

package debug;
import java.io.*;
import java.lang.Thread;
class MyThread extends Thread{
 public int x = 0;
 public void run(){
  System.out.println(++x);
 }
}

class R implements Runnable{
 private int x = 0;
 public void run(){
  System.out.println(++x);
 }
}
public class Test {
 public static void main(String[] args) throws Exception{
  for(int i=0;i<10;i++){
    Thread t = new MyThread();
    t.start();
  }
  Thread.sleep(10000);//讓上麵的線程運行完成
  R r = new R();
  for(int i=0;i<10;i++){
    Thread t = new Thread(r);
    t.start();
  }
 }  
}

  上麵10個線程對象產生的10個線程運行時打印了10次1。下麵10個線程對象產生的10個線程運行時打印了1到10(可能不是按照1-10的順序)。我們把下麵的10個線程稱為同一實例(Runnable實例)的多個線程。

最後更新:2017-04-02 06:51:49

  上一篇:go Android ListView反複調用getView和getCount
  下一篇:go android ListView 數據更新的總結