天天看點

JAVA多線程機制之線程建立ThreadRunnablejoin()

在java中建立線程有兩種方式,一種是繼承thread,另一種是實作runnable接口,thread實際上也實作了runnable接口。

方法名

說明

thread()

配置設定新的 thread 對象

thread(runnable target)

thread(runnable target, string name)

thread(string name)

thread(threadgroup group, runnable target)

thread(threadgroup group, runnable target, string name)

配置設定新的 thread 對象,以便将 target 作為其運作對象,将指定的 name 作為其名稱,并作為 group 所引用的線程組的一員

thread(threadgroup group, runnable target, string name, long stacksize)

配置設定新的 thread 對象,以便将 target 作為其運作對象,将指定的 name 作為其名稱,作為 group 所引用的線程組的一員,并具有指定的堆棧大小

thread(threadgroup group, string name)

傳回值

static int

activecount()

傳回目前線程的線程組中活動線程的數目

void

checkaccess()

判定目前運作的線程是否有權修改該線程

static thread

currentthread()

傳回對目前正在執行的線程對象的引用

static void

dumpstack()

将目前線程的堆棧跟蹤列印至标準錯誤流

enumerate(thread[] tarray)

将目前線程的線程組及其子組中的每一個活動線程複制到指定的數組中

static map<thread,stacktraceelement[]>

getallstacktraces()

傳回所有活動線程的堆棧跟蹤的一個映射

classloader

getcontextclassloader()

傳回該線程的上下文 classloader

static thread.uncaughtexceptionhandler

getdefaultuncaughtexceptionhandler()

傳回線程由于未捕獲到異常而突然終止時調用的預設處理程式

long

getid()

傳回該線程的辨別符

string

getname()

傳回該線程的名稱

int

getpriority()

傳回線程的優先級

stacktraceelement[]

getstacktrace()

傳回一個表示該線程堆棧轉儲的堆棧跟蹤元素數組

thread.state

getstate()

傳回該線程的狀态

threadgroup

getthreadgroup()

傳回該線程所屬的線程組

thread.uncaughtexceptionhandler

getuncaughtexceptionhandler()

傳回該線程由于未捕獲到異常而突然終止時調用的處理程式

static boolean

holdslock(object obj)

當且僅當目前線程在指定的對象上保持螢幕鎖時,才傳回 true

interrupt()

中斷線程

interrupted()

測試目前線程是否已經中斷

boolean

isalive()

測試線程是否處于活動狀态

isdaemon()

測試該線程是否為守護線程

isinterrupted()

測試線程是否已經中斷

join()

等待該線程終止

join(long millis)

等待該線程終止的時間最長為 millis 毫秒

join(long millis, int nanos)

等待該線程終止的時間最長為 millis 毫秒 + nanos 納秒

run()

如果該線程是使用獨立的 runnable 運作對象構造的,則調用該 runnable 對象的 run 方法;否則,該方法不執行任何操作并傳回

setcontextclassloader(classloader cl)

設定該線程的上下文 classloader

setdaemon(boolean on)

将該線程标記為守護線程或使用者線程

setdefaultuncaughtexceptionhandler(thread.uncaughtexceptionhandler eh)

設定當線程由于未捕獲到異常而突然終止,并且沒有為該線程定義其他處理程式時所調用的預設處理程式

setname(string name)

改變線程名稱,使之與參數 name 相同

setpriority(int newpriority)

更改線程的優先級

setuncaughtexceptionhandler(thread.uncaughtexceptionhandler eh)

設定該線程由于未捕獲到異常而突然終止時調用的處理程式

sleep(long millis)

在指定的毫秒數内讓目前正在執行的線程休眠(暫停執行),此操作受到系統計時器和排程程式精度和準确性的影響

sleep(long millis, int nanos)

在指定的毫秒數加指定的納秒數内讓目前正在執行的線程休眠(暫停執行),此操作受到系統計時器和排程程式精度和準确性的影響

start()

使該線程開始執行;java 虛拟機調用該線程的 run 方法

tostring()

傳回該線程的字元串表示形式,包括線程名稱、優先級和線程組

yield()

暫停目前正在執行的線程對象,并執行其他線程

運作結果如下:

thread-1:0

thread-0:0

thread-0:1

thread-1:1

thread-0:2

thread-1:2

thread-0:3

thread-1:3

thread-0:4

thread-1:4

thread-1:5

thread-0:5

thread-1:6

thread-0:6

thread-0:7

thread-1:7

thread-0:8

thread-1:8

thread-1:9

thread-0:9

在上面的示例中,我們使用了繼承thread的方式建立了線程對象并通過start()方法啟動了線程,在例子中,我們啟動了兩個線程,線程隻是簡單的輸出一句話,通過檢視運作結果,我們可以發現兩個線程是交錯執行的。

使用實作接口 runnable 的對象建立一個線程時,啟動該線程将導緻在獨立執行的線程中調用對象的 run 方法

運作結果:

有的時候,我們希望當一個線程執行完後,在繼續執行其他線程,比如一些初始化工作。這個時候我們可以使用<code>join()</code>方法,join()方法可以等待該線程終止。

使用示例

通過運作結果我們可以看到,當我們使用join方法後,會等待線程執行結束再繼續執行另一個線程。