日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java excutorthread_Java中ThreadPoolExecutor的参数理解

發布時間:2023/12/4 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java excutorthread_Java中ThreadPoolExecutor的参数理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、使用Executors創建線程池

之前創建線程的時候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()這三個方法。當然Executors也是用不同的參數去new ThreadPoolExecutor

1.?newFixedThreadPool()

創建線程數固定大小的線程池。 由于使用了LinkedBlockingQueue所以maximumPoolSize 沒用,當corePoolSize滿了之后就加入到LinkedBlockingQueue隊列中。每當某個線程執行完成之后就從LinkedBlockingQueue隊列中取一個。所以這個是創建固定大小的線程池。

public?static?ExecutorService?newFixedThreadPool(int?nThreads)?{

return?new?ThreadPoolExecutor(nThreads,?nThreads,

0L,?TimeUnit.MILLISECONDS,

new?LinkedBlockingQueue());

}

public?ThreadPoolExecutor(int?corePoolSize,

int?maximumPoolSize,

long?keepAliveTime,

TimeUnit?unit,

BlockingQueue?workQueue)?{

this(corePoolSize,?maximumPoolSize,?keepAliveTime,?unit,?workQueue,

Executors.defaultThreadFactory(),?defaultHandler);

}

2.newSingleThreadPool()

創建線程數為1的線程池,由于使用了LinkedBlockingQueue所以maximumPoolSize 沒用,corePoolSize為1表示線程數大小為1,滿了就放入隊列中,執行完了就從隊列取一個。

public?static?ExecutorService?newSingleThreadExecutor()?{

return?new?FinalizableDelegatedExecutorService

(new?ThreadPoolExecutor(1,?1,

0L,?TimeUnit.MILLISECONDS,

new?LinkedBlockingQueue()));

}

3.newCachedThreadPool()

創建可緩沖的線程池。沒有大小限制。由于corePoolSize為0所以任務會放入SynchronousQueue隊列中,SynchronousQueue只能存放大小為1,所以會立刻新起線程,由于maxumumPoolSize為Integer.MAX_VALUE所以可以認為大小為2147483647。受內存大小限制。

public?static?ExecutorService?newCachedThreadPool()?{

return?new?ThreadPoolExecutor(0,?Integer.MAX_VALUE,

60L,?TimeUnit.SECONDS,

new?SynchronousQueue());

}

public?ThreadPoolExecutor(int?corePoolSize,

int?maximumPoolSize,

long?keepAliveTime,

TimeUnit?unit,

BlockingQueue?workQueue)?{

this(corePoolSize,?maximumPoolSize,?keepAliveTime,?unit,?workQueue,

Executors.defaultThreadFactory(),?defaultHandler);

}

二、使用ThreadPoolExecutor創建線程池

ThreadPoolExecutor的構造函數

public?ThreadPoolExecutor(int?corePoolSize,

int?maximumPoolSize,

long?keepAliveTime,

TimeUnit?unit,

BlockingQueue?workQueue,

ThreadFactory?threadFactory,

RejectedExecutionHandler?handler)?{

if?(corePoolSize?

maximumPoolSize?<=?0?||

maximumPoolSize?

keepAliveTime?

throw?new?IllegalArgumentException();

if?(workQueue?==?null?||?threadFactory?==?null?||?handler?==?null)

throw?new?NullPointerException();

this.corePoolSize?=?corePoolSize;

this.maximumPoolSize?=?maximumPoolSize;

this.workQueue?=?workQueue;

this.keepAliveTime?=?unit.toNanos(keepAliveTime);

this.threadFactory?=?threadFactory;

this.handler?=?handler;

}

參數:

1、corePoolSize核心線程數大小,當線程數

2、maximumPoolSize 最大線程數, 當線程數 >= corePoolSize的時候,會把runnable放入workQueue中

3、keepAliveTime ?保持存活時間,當線程數大于corePoolSize的空閑線程能保持的最大時間。

4、unit 時間單位

5、workQueue 保存任務的阻塞隊列

6、threadFactory 創建線程的工廠

7、handler 拒絕策略

任務執行順序:

1、當線程數小于corePoolSize時,創建線程執行任務。

2、當線程數大于等于corePoolSize并且workQueue沒有滿時,放入workQueue中

3、線程數大于等于corePoolSize并且當workQueue滿時,新任務新建線程運行,線程總數要小于maximumPoolSize

4、當線程總數等于maximumPoolSize并且workQueue滿了的時候執行handler的rejectedExecution。也就是拒絕策略。

ThreadPoolExecutor默認有四個拒絕策略:

1、ThreadPoolExecutor.AbortPolicy() ? 直接拋出異常RejectedExecutionException

2、ThreadPoolExecutor.CallerRunsPolicy()????直接調用run方法并且阻塞執行

3、ThreadPoolExecutor.DiscardPolicy() ? 直接丟棄后來的任務

4、ThreadPoolExecutor.DiscardOldestPolicy() ?丟棄在隊列中隊首的任務

當然可以自己繼承RejectedExecutionHandler來寫拒絕策略.

int?corePoolSize?=?1;

int?maximumPoolSize?=?2;

int?keepAliveTime?=?10;

//BlockingQueue?workQueue?=?new?LinkedBlockingQueue();

BlockingQueue?workQueue?=?new?ArrayBlockingQueue(5);

ThreadFactory?threadFactory?=?Executors.defaultThreadFactory();

//線程池和隊列滿了之后的處理方式

//1.跑出異常

RejectedExecutionHandler?handler?=?new?ThreadPoolExecutor.AbortPolicy();

RejectedExecutionHandler?handler2?=?new?ThreadPoolExecutor.CallerRunsPolicy();

RejectedExecutionHandler?handler3?=?new?ThreadPoolExecutor.DiscardPolicy();

RejectedExecutionHandler?handler4?=?new?ThreadPoolExecutor.DiscardOldestPolicy();

ThreadPoolExecutor?threadPoolExecutor?=?new?ThreadPoolExecutor(corePoolSize,?maximumPoolSize,?keepAliveTime,?TimeUnit.SECONDS,?workQueue,?threadFactory,?handler2);

for?(int?j?=?1;?j?

threadPoolExecutor.execute(new?Runnable()?{

public?void?run()?{

try?{

System.out.println(Thread.currentThread().getName());

TimeUnit.SECONDS.sleep(1);

}?catch?(InterruptedException?e)?{

e.printStackTrace();

}

}

});

}

System.out.println(threadPoolExecutor);

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java excutorthread_Java中ThreadPoolExecutor的参数理解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。