激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|

服務器之家 - 編程語言 - JAVA教程 - java ThreadPoolExecutor 并發調用實例詳解

java ThreadPoolExecutor 并發調用實例詳解

2020-11-01 22:58Java之家 JAVA教程

這篇文章主要介紹了java ThreadPoolExecutor 并發調用實例詳解的相關資料,需要的朋友可以參考下

java ThreadPoolExecutor 并發調用實例詳解

概述

通常為了提供任務的處理速度,會使用一些并發模型,ThreadPoolExecutor中的invokeAll便是一種。

代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
  }
}

可以把需要執行的任務創建一個Callable task,利用線程池中的線程并發的執行這些task,從而提高任務的執行效率。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/linsongbin1/article/details/71244376

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品二区高清在线 | 中文字幕在线视频日本 | 黄色av免费网站 | 国产亚洲黑人性受xxxx精品 | 午夜生活理论片 | 视频一区二区久久 | 性欧美暴力猛交69hd | 免费的性生活视频 | 久久久免费观看完整版 | 视频一区二区三区免费观看 | 91视频观看 | 羞羞视频入口 | 亚洲男人的天堂在线视频 | 免费一级肉体全黄毛片 | 啪啪激情 | 美女扒开腿让男生桶爽网站 | 欧美一级高潮片免费的 | 午夜精品久久久久久久96蜜桃 | 毛片在线免费观看完整版 | 成码无人av片在线观看网站 | 九九热在线视频观看 | 91在线视频观看 | 国产毛片毛片毛片 | 在线观看国产一区二区 | 久久久精品视频国产 | 精品国产一区二区三区成人影院 | 国产18视频| 中韩毛片| www.xxx视频 | 日韩毛片一区二区三区 | 91短视频网址| 国产免费乱淫av | 久久影院在线观看 | 久久99精品国产99久久6男男 | 一区国产精品 | 91avsese| 一分钟免费观看完整版电影 | 99综合视频 | 成人午夜免费在线观看 | 欧美a级在线免费观看 | 国产精品高潮99久久久久久久 |