概要
本章,我們學習“常用的實現多線程的2種方式”:Thread 和 Runnable。
之所以說是常用的,是因為通過還可以通過java.util.concurrent包中的線程池來實現多線程。關於線程池的內容,我們以后會詳細介紹;現在,先對的Thread和Runnable進行了解。本章內容包括:
Thread和Runnable的簡介
Thread和Runnable的異同點
Thread和Runnable的多線程的示例
轉載請注明出處:http://www.cnblogs.com/skywang12345/p/3479063.html
Thread和Runnable簡介
Runnable 是一個接口,該接口中只包含了一個run()方法。它的定義如下:
public interface Runnable { public abstract void run(); }
Runnable的作用,實現多線程。我們可以定義一個類A實現Runnable接口;然后,通過new Thread(new A())等方式新建線程。
Thread 是一個類。Thread本身就實現了Runnable接口。它的聲明如下:
public class Thread implements Runnable {}
Thread的作用,實現多線程。
Thread和Runnable的異同點
Thread 和 Runnable 的相同點:都是“多線程的實現方式”。
Thread 和 Runnable 的不同點:
Thread 是類,而Runnable是接口;Thread本身是實現了Runnable接口的類。我們知道“一個類只能有一個父類,但是卻能實現多個接口”,因此Runnable具有更好的擴展性。
此外,Runnable還可以用於“資源的共享”。即,多個線程都是基於某一個Runnable對象建立的,它們會共享Runnable對象上的資源。
通常,建議通過“Runnable”實現多線程!
Thread和Runnable的多線程示例
1. Thread的多線程示例
下面通過示例更好的理解Thread和Runnable,借鑒網上一個例子比較具有說服性的例子。
1 // ThreadTest.java 源碼 2 class MyThread extends Thread{ 3 private int ticket=10; 4 public void run(){ 5 for(int i=0;i<20;i++){ 6 if(this.ticket>0){ 7 System.out.println(this.getName()+" 賣票:ticket"+this.ticket--); 8 } 9 } 10 } 11 }; 12 13 public class ThreadTest { 14 public static void main(String[] args) { 15 // 啟動3個線程t1,t2,t3;每個線程各賣10張票! 16 MyThread t1=new MyThread(); 17 MyThread t2=new MyThread(); 18 MyThread t3=new MyThread(); 19 t1.start(); 20 t2.start(); 21 t3.start(); 22 } 23 }
運行結果:
Thread-0 賣票:ticket10 Thread-1 賣票:ticket10 Thread-2 賣票:ticket10 Thread-1 賣票:ticket9 Thread-0 賣票:ticket9 Thread-1 賣票:ticket8 Thread-2 賣票:ticket9 Thread-1 賣票:ticket7 Thread-0 賣票:ticket8 Thread-1 賣票:ticket6 Thread-2 賣票:ticket8 Thread-1 賣票:ticket5 Thread-0 賣票:ticket7 Thread-1 賣票:ticket4 Thread-2 賣票:ticket7 Thread-1 賣票:ticket3 Thread-0 賣票:ticket6 Thread-1 賣票:ticket2 Thread-2 賣票:ticket6 Thread-2 賣票:ticket5 Thread-2 賣票:ticket4 Thread-1 賣票:ticket1 Thread-0 賣票:ticket5 Thread-2 賣票:ticket3 Thread-0 賣票:ticket4 Thread-2 賣票:ticket2 Thread-0 賣票:ticket3 Thread-2 賣票:ticket1 Thread-0 賣票:ticket2 Thread-0 賣票:ticket1
結果說明:
(01) MyThread繼承於Thread,它是自定義個線程。每個MyThread都會賣出10張票。
(02) 主線程main創建並啟動3個MyThread子線程。每個子線程都各自賣出了10張票。
2. Runnable的多線程示例
下面,我們對上面的程序進行修改。通過Runnable實現一個接口,從而實現多線程。
1 // RunnableTest.java 源碼 2 class MyThread implements Runnable{ 3 private int ticket=10; 4 public void run(){ 5 for(int i=0;i<20;i++){ 6 if(this.ticket>0){ 7 System.out.println(Thread.currentThread().getName()+" 賣票:ticket"+this.ticket--); 8 } 9 } 10 } 11 }; 12 13 public class RunnableTest { 14 public static void main(String[] args) { 15 MyThread mt=new MyThread(); 16 17 // 啟動3個線程t1,t2,t3(它們共用一個Runnable對象),這3個線程一共賣10張票! 18 Thread t1=new Thread(mt); 19 Thread t2=new Thread(mt); 20 Thread t3=new Thread(mt); 21 t1.start(); 22 t2.start(); 23 t3.start(); 24 } 25 }
運行結果:
Thread-0 賣票:ticket10 Thread-2 賣票:ticket8 Thread-1 賣票:ticket9 Thread-2 賣票:ticket6 Thread-0 賣票:ticket7 Thread-2 賣票:ticket4 Thread-1 賣票:ticket5 Thread-2 賣票:ticket2 Thread-0 賣票:ticket3 Thread-1 賣票:ticket1
結果說明:
(01) 和上面“MyThread繼承於Thread”不同;這里的MyThread實現了Thread接口。
(02) 主線程main創建並啟動3個子線程,而且這3個子線程都是基於“mt這個Runnable對象”而創建的。運行結果是這3個子線程一共賣出了10張票。這說明它們是共享了MyThread接口的。
更多內容