java ThreadPoolExecutor 并發(fā)調(diào)用實例詳解
概述
通常為了提供任務的處理速度,會使用一些并發(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); } }
新聞名稱:javaThreadPoolExecutor并發(fā)調(diào)用實例詳解-創(chuàng)新互聯(lián)
當前網(wǎng)址:http://chinadenli.net/article40/ddiiho.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、軟件開發(fā)、網(wǎng)站制作、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站制作、標簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容