舉例講解Java中的多線程編程
Java創(chuàng)建線程(Runnable接口和Thread類)
大多數情況,通過實例化一個Thread對象來創(chuàng)建一個線程。Java定義了兩種方式:
- 實現Runnable 接口;
- 可以繼承Thread類。
下面的依次介紹了每一種方式。
實現Runnable接口
創(chuàng)建線程的最簡單的方法就是創(chuàng)建一個實現Runnable 接口的類。Runnable抽象了一個執(zhí)行代碼單元。你可以通過實現Runnable接口的方法創(chuàng)建每一個對象的線程。為實現Runnable 接口,一個類僅需實現一個run()的簡單方法,該方法聲明如下:
public void run( )
在run()中可以定義代碼來構建新的線程。理解下面內容是至關重要的:run()方法能夠像主線程那樣調用其他方法,引用其他類,聲明變量。僅有的不同是run()在程序中確立另一個并發(fā)的線程執(zhí)行入口。當run()返回時,該線程結束。
在你已經創(chuàng)建了實現Runnable接口的類以后,你要在類內部實例化一個Thread類的對象。Thread 類定義了好幾種構造函數。我們會用到的如下:
Thread(Runnable threadOb, String threadName)
該構造函數中,threadOb是一個實現Runnable接口類的實例。這定義了線程執(zhí)行的起點。新線程的名稱由threadName定義。
建立新的線程后,它并不運行直到調用了它的start()方法,該方法在Thread 類中定義。本質上,start() 執(zhí)行的是一個對run()的調用。 Start()方法聲明如下:
void start( )
下面的例子是創(chuàng)建一個新的線程并啟動它運行:
// 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對象由下面的語句創(chuàng)建::
t = new Thread(this, "Demo Thread");
通過前面的語句this 表明在this對象中你想要新的線程調用run()方法。然后,start() 被調用,以run()方法為開始啟動了線程的執(zhí)行。這使子線程for 循環(huán)開始執(zhí)行。調用start()之后,NewThread 的構造函數返回到main()。當主線程被恢復,它到達for 循環(huán)。兩個線程繼續(xù)運行,共享CPU,直到它們的循環(huán)結束。該程序的輸出如下:
Child thread: Thread[Demo Thread,5,main] Main Thread: 5 Child Thread: 5 Child Thread: 4 Main Thread: 4 Child Thread: 3 Child Thread: 2 Main Thread: 3 Child Thread: 1 Exiting child thread. Main Thread: 2 Main Thread: 1 Main thread exiting.
如前面提到的,在多線程程序中,通常主線程必須是結束運行的最后一個線程。實際上,一些老的JVM,如果主線程先于子線程結束,Java的運行時間系統(tǒng)就可能“掛起”。前述程序保證了主線程最后結束,因為主線程沉睡周期1000毫秒,而子線程僅為500毫秒。這就使子線程在主線程結束之前先結束。簡而言之,你將看到等待線程結束的更好途徑。
擴展Thread
創(chuàng)建線程的另一個途徑是創(chuàng)建一個新類來擴展Thread類,然后創(chuàng)建該類的實例。當一個類繼承Thread時,它必須重載run()方法,這個run()方法是新線程的入口。它也必須調用start()方法去啟動新線程執(zhí)行。下面用擴展thread類重寫前面的程序:
// Create a second thread by extending Thread
class 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有兩種創(chuàng)建子線程的方法,哪一種更好呢。所有的問題都歸于一點。Thread類定義了多種方法可以被派生類重載。對于所有的方法,惟一的必須被重載的是run()方法。這當然是實現Runnable接口所需的同樣的方法。很多Java程序員認為類僅在它們被加強或修改時應該被擴展。因此,如果你不重載Thread的其他方法時,最好只實現Runnable 接口。這當然由你決定。然而,在本章的其他部分,我們應用實現runnable接口的類來創(chuàng)建線程。
相關文章
基于Nacos實現Spring Cloud Gateway實現動態(tài)路由的方法
這篇文章主要介紹了基于Nacos實現Spring Cloud Gateway實現動態(tài)路由的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06

