多執行緒之執行執行緒異常捕獲

2021-10-01 16:27:14 字數 1219 閱讀 6244

/**

* @description: 執行緒異常捕捉

* @author: administrator

* @create: 2019-12-23 22:22

**/public

class

threadexception

static

void

thread1()

catch

(interruptedexception e)})

; thread.

start()

;}static

void

thread2()

catch

(interruptedexception e)})

;//輸出異常資訊

thread.

setuncaughtexceptionhandler

((t, e)

->);

thread.

start();}}

thread1()方法執行結果

捕獲執行緒異常使用 setuncaughtexceptionhandler(thread t,exception e)方法捕捉異常資訊返回,然後再進行處理

thread1()方法執行結果

}異常處理器

class

myuncaughtexceptionhandler

implements

thread.uncaughtexceptionhandler

}

自定義丟擲異常的執行緒

class

threadtask

implements

runnable

}

異常捕獲

執行緒池中捕獲執行緒執行異常

在專案中使用執行緒池時,使用了如下方式 executorservice datauploadpool executors.newfixedthreadpool writethreadnum datauploadpool.execute new thread task 但是這種方式下,主線程無法捕獲子...

多執行緒之執行緒同步

pulse lockobj 表示釋放當前被lock的lockobj,容許其他執行緒呼叫。相當於暫時掛起當前執行緒 wait lockobj 表示等待當前被其他執行緒占用的lockobj。下面的 將會交替執行兩個執行緒 class ticktock console.write tick monitor...

多執行緒之執行緒池

執行緒框架關係 executor 介面 executorservice 介面 繼承 executor abstractexecutorservice 抽象類 實現 executorservice threadpoolexecutor 繼承abstractexecutorservice 過載一系列方法...