Java執行緒池幾種簡單使用方式

2021-09-23 14:13:17 字數 827 閱讀 8075

1.提供公共的執行緒池工具類,示例:

private static threadpooltaskexecutor taskexecutor = null;

private static integer corepoolsize = 10;

private static integer maxpoolsize = 20;

private static integer queuecapacity = 1000;

static

public static void run(runnable runnable)

呼叫示例:

threadtaskutils.run(new runnable() 

});

2.使用spring註解,自主配置執行緒池資訊,在專案中加入下面**:

@bean(name = "threadpooltask")

public threadpooltaskexecutor getasyncthreadpooltaskexecutor()

在需要使用非同步的位置,加上註解:@async("threadpooltask");

在啟動類加上註解:@enableasync

注意事項:在呼叫本類子方法時,方法上加上註解無效,示例:

public void autocheck() 

@async("scheduledpooltaskexecutor")

public void checkorder()

使用上面的寫法,非同步不生效。

執行緒池的幾種使用方式介紹

一 執行緒池的優點 關於執行緒池的使用優點網路上介紹的有很多,可以歸結為以下幾點 1.減少在建立和銷毀執行緒上所花的時間及系統資源的開銷。2.提高執行緒的可管理性,對執行緒進行統一的分配 調優和監控,從而也提高相應速度。二 執行緒池的uml圖和使用方式 執行緒池的uml圖如下,使用執行緒池的方式總共...

java執行緒池使用

newcachedthreadpool newfixedthreadpool newscheduledthreadpool newsinglethreadexecutor 單例物件中的執行緒池使用 建立乙個可快取執行緒池,如果執行緒池長度超過處理需要,可靈活 空閒執行緒,若無可 則新建執行緒 exe...

java執行緒池場景使用

當伺服器介面之間通訊中,如果a伺服器請求b伺服器,只返回請求通訊成功且不需要實時返回業務成功或失敗時,處理成功或者失敗用非同步通知方式,那麼可以採用執行緒池或者訊息佇列 業務場景 金融業務中的強風控,還款等等 重點 如果需要同步通知業務成功或失敗,讓a伺服器提供通知介面,在伺服器b執行緒中可以同步通...