一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲第一二三四区 | 欧美日韩精品在线观看 | 视频在线91 | 国产免费小视频在线观看 | 午夜A级理论片左线播放 | 青青青手机在线观看 | 青视频在线 | 5x社区发源地最新地址 | 亚洲天堂视频在线播放 | 欧美日韩高清完整版在线观看免费 | 激情婷婷综合久久久久 | 免费高清特黄a 大片 | 啊皇上你好大要知画 | 国产大片51精品免费观看 | 亚洲AV无码乱码在线观看浪潮 | 国产欧美一区二区三区免费 | 精品国产一区二区 | 国产综合久久 | 国产日韩欧美在线观看不卡 | 国产精品主播在线 | 久久免费特黄毛片 | 亚洲风情无码免费视频 | 四虎影院2022 | 亚洲精品动漫免费二区 | 天天做天天玩天天爽天天 | 91av免费 | 日本全黄三级在线观看 | 1986葫芦兄弟全集免费观看第十集 | 欧美日韩在线观看一区二区 | 国产亚洲精品福利在线 | 跪趴好紧h | 男人猛戳女人下部30分钟 | heyzo在线观看 | 白丝校花被扒开双腿喷水小说 | 国产高清在线播放免费观看 | 王晶三级作品 | 四虎影视永久在线精品免费 | 欧美猛男同志同性video | 欧美老妪 | 国内精品哆啪啪 | 国产高清在线播放免费观看 |