Java創建線程(Runnable接口和Thread類)
大多數情況,通過實例化一個Thread對象來創建一個線程。 Java定義了兩種方式:
下面的依次介紹了每一種方式。
實現Runnable接口
創建線程的最簡單的方法就是創建一個實現Runnable 接口的類。 Runnable抽象了一個執行代碼單元。你可以通過實現Runnable接口的方法創建每一個對象的線程。為實現Runnable 接口,一個類僅需實現一個run()的簡單方法,該方法聲明如下:
public void run( )
在run()中可以定義代碼來構建新的線程。理解下面內容是至關重要的:run()方法能夠像主線程那樣調用其他方法,引用其他類,聲明變量。僅有的不同是run()在程序中確立另一個並發的線程執行入口。當run()返回時,該線程結束。
在你已經創建了實現Runnable接口的類以後,你要在類內部實例化一個Thread類的對象。 Thread 類定義了好幾種構造函數。我們會用到的如下:
Thread(Runnable threadOb, String threadName)
該構造函數中,threadOb是一個實現Runnable接口類的實例。這定義了線程執行的起點。新線程的名稱由threadName定義。
建立新的線程後,它並不運行直到調用了它的start()方法,該方法在Thread 類中定義。本質上,start() 執行的是一個對run()的調用。 Start()方法聲明如下:
void start( )
下面的例子是創建一個新的線程並啟動它運行:
// Create a second thread.class NewThread implements Runnable { Thread t; NewThread() { // Create a new, second thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i- -) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out .println("Exiting child thread."); }}class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0 ; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); }}
在NewThread 構造函數中,新的Thread對象由下面的語句創建::
t = new Thread(this, "Demo Thread");
通過前面的語句this 表明在this對像中你想要新的線程調用run()方法。然後,start() 被調用,以run()方法為開始啟動了線程的執行。這使子線程for 循環開始執行。調用start()之後,NewThread 的構造函數返回到main()。當主線程被恢復,它到達for 循環。兩個線程繼續運行,共享CPU,直到它們的循環結束。該程序的輸出如下:
Child thread: Thread[Demo Thread,5,main]Main Thread: 5Child Thread: 5Child Thread: 4Main Thread: 4Child Thread: 3Child Thread: 2Main Thread: 3Child Thread: 1Exiting child thread.Main Thread: 2Main Thread: 1Main thread exiting.
如前面提到的,在多線程程序中,通常主線程必須是結束運行的最後一個線程。實際上,一些老的JVM,如果主線程先於子線程結束,Java的運行時間系統就可能“掛起”。前述程序保證了主線程最後結束,因為主線程沉睡週期1000毫秒,而子線程僅為500毫秒。這就使子線程在主線程結束之前先結束。簡而言之,你將看到等待線程結束的更好途徑。
擴展Thread
創建線程的另一個途徑是創建一個新類來擴展Thread類,然後創建該類的實例。當一個類繼承Thread時,它必須重載run()方法,這個run()方法是新線程的入口。它也必須調用start()方法去啟動新線程執行。下面用擴展thread類重寫前面的程序:
// Create a second thread by extending Threadclass NewThread extends Thread { NewThread() { // Create a new, second thread super("Demo Thread"); System.out.println("Child thread: " + this); start( ); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println( "Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread." ); }}class ExtendThread { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out .println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); }}
該程序生成和前述版本相同的輸出。子線程是由實例化NewThread對像生成的,該對像從Thread類派生。注意NewThread 中super()的調用。該方法調用了下列形式的Thread構造函數:
public Thread(String threadName)
這裡,threadName指定線程名稱。
選擇合適方法
到這裡,你一定會奇怪為什麼Java有兩種創建子線程的方法,哪一種更好呢。所有的問題都歸於一點。 Thread類定義了多種方法可以被派生類重載。對於所有的方法,惟一的必須被重載的是run()方法。這當然是實現Runnable接口所需的同樣的方法。很多Java程序員認為類僅在它們被加強或修改時應該被擴展。因此,如果你不重載Thread的其他方法時,最好只實現Runnable 接口。這當然由你決定。然而,在本章的其他部分,我們應用實現runnable接口的類來創建線程。