java ThreadPoolExecutor 并發(fā)調(diào)用實(shí)例詳解
java ThreadPoolExecutor 并發(fā)調(diào)用實(shí)例詳解
概述
通常為了提供任務(wù)的處理速度,會(huì)使用一些并發(fā)模型,ThreadPoolExecutor中的invokeAll便是一種。
代碼
package test.current;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class TestCallable {
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Callable<List<Long>>> tasks = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Callable<List<Long>> task = new Callable<List<Long>>() {
@Override
public List<Long> call() throws Exception {
return Arrays.asList(1L,2L);
}
};
tasks.add(task);
}
List<Long> finalResults = new ArrayList<>(10);
List<Future<List<Long>>> results = ThreadPool.getThreadPool().invokeAll(tasks);
for(Future<List<Long>> ele : results) {
List<Long> list = ele.get();
finalResults.addAll(list);
}
System.out.println(finalResults);
}
}
package test.current;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPool {
private static final int CORE_SIZE = 8;
private static final int MAX_SIZE = 12;
private static final long KEEP_ALIVE_TIME = 30;
private static final int QUEUE_SIZE = 50000;
private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(CORE_SIZE, MAX_SIZE, KEEP_ALIVE_TIME,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(QUEUE_SIZE), new ThreadPoolExecutor.AbortPolicy());
public static ThreadPoolExecutor getThreadPool() {
return threadPool;
}
}
可以把需要執(zhí)行的任務(wù)創(chuàng)建一個(gè)Callable task,利用線程池中的線程并發(fā)的執(zhí)行這些task,從而提高任務(wù)的執(zhí)行效率。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
使用Spring處理x-www-form-urlencoded方式
這篇文章主要介紹了使用Spring處理x-www-form-urlencoded方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java中將字符串String轉(zhuǎn)換為整數(shù)int的多種方法
在Java中將String類型轉(zhuǎn)換為int類型是一個(gè)常見(jiàn)的操作,下面這篇文章主要給大家介紹了關(guān)于Java中將字符串String轉(zhuǎn)換為整數(shù)int的多種方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
springboot實(shí)現(xiàn)定時(shí)任務(wù)的四種方式小結(jié)
本文主要介紹了springboot實(shí)現(xiàn)定時(shí)任務(wù)的四種方式小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
一篇文章帶你了解JAVA面對(duì)對(duì)象應(yīng)用
Java是一門面向?qū)ο蟮恼Z(yǔ)言。對(duì)象是Java程序中的基本實(shí)體。除了對(duì)象之外Java程序同樣處理基本數(shù)據(jù)。下面這篇文章主要給大家總結(jié)了關(guān)于Java中面向?qū)ο蟮闹R(shí)點(diǎn),需要的朋友可以參考借鑒,下面來(lái)一起看看吧2021-08-08
Java中this和super的區(qū)別及this能否調(diào)用到父類使用
這篇文章主要介紹了Java中this和super的區(qū)別及this能否調(diào)用到父類使用,this和super都是Java中常見(jiàn)的關(guān)鍵字,下文關(guān)于兩者區(qū)別介紹,需要的小伙伴可以參考一下2022-05-05
Java動(dòng)態(tài)規(guī)劃之編輯距離問(wèn)題示例代碼
這篇文章主要介紹了Java動(dòng)態(tài)規(guī)劃之編輯距離問(wèn)題示例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Spark隨機(jī)森林實(shí)現(xiàn)票房預(yù)測(cè)
這篇文章主要為大家詳細(xì)介紹了Spark隨機(jī)森林實(shí)現(xiàn)票房預(yù)測(cè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08

