1. 程式人生 > >Java - Thread 和 Runnable實現多線程

Java - Thread 和 Runnable實現多線程

abs java jdk1 public adt system 轉載 作用 final

Java多線程系列--“基礎篇”02之 常用的實現多線程的兩種方式

概要

本章,我們學習“常用的實現多線程的2種方式”:ThreadRunnable
之所以說是常用的,是因為通過還可以通過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接口的。

start() 和 run()的區別說明

start() : 它的作用是啟動一個新線程,新線程會執行相應的run()方法。start()不能被重復調用。
run() : run()就和普通的成員方法一樣,可以被重復調用。單獨調用run()的話,會在當前線程中執行run(),而並不會啟動新線程!

下面以代碼來進行說明。

class MyThread extends Thread{  
    public void run(){
        ...
    } 
};
MyThread mythread = new MyThread();

mythread.start()會啟動一個新線程,並在新線程中運行run()方法。
而mythread.run()則會直接在當前線程中運行run()方法,並不會啟動一個新線程來運行run()。

start() 和 run()的區別示例

下面,通過一個簡單示例演示它們之間的區別。源碼如下:

技術分享
 1 // Demo.java 的源碼
 2 class MyThread extends Thread{  
 3     public MyThread(String name) {
 4         super(name);
 5     }
 6 
 7     public void run(){
 8         System.out.println(Thread.currentThread().getName()+" is running");
 9     } 
10 }; 
11 
12 public class Demo {  
13     public static void main(String[] args) {  
14         Thread mythread=new MyThread("mythread");
15 
16         System.out.println(Thread.currentThread().getName()+" call mythread.run()");
17         mythread.run();
18 
19         System.out.println(Thread.currentThread().getName()+" call mythread.start()");
20         mythread.start();
21     }  
22 }
技術分享

運行結果

main call mythread.run()
main is running
main call mythread.start()
mythread is running

結果說明
(01) Thread.currentThread().getName()是用於獲取“當前線程”的名字。當前線程是指正在cpu中調度執行的線程。
(02) mythread.run()是在“主線程main”中調用的,該run()方法直接運行在“主線程main”上。
(03) mythread.start()會啟動“線程mythread”,“線程mythread”啟動之後,會調用run()方法;此時的run()方法是運行在“線程mythread”上。

start() 和 run()相關源碼(基於JDK1.7.0_40)

Thread.java中start()方法的源碼如下:

技術分享
public synchronized void start() {
    // 如果線程不是"就緒狀態",則拋出異常!
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    // 將線程添加到ThreadGroup中
    group.add(this);

    boolean started = false;
    try {
        // 通過start0()啟動線程
        start0();
        // 設置started標記
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
        }
    }
}
技術分享

說明:start()實際上是通過本地方法start0()啟動線程的。而start0()會新運行一個線程,新線程會調用run()方法。

private native void start0();

Thread.java中run()的代碼如下:

public void run() {
    if (target != null) {
        target.run();
    }
}

說明:target是一個Runnable對象。run()就是直接調用Thread線程的Runnable成員的run()方法,並不會新建一個線程。

Java - Thread 和 Runnable實現多線程