博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java_ExecutorService, CompletionService - 有返回值并行工作方式
阅读量:6046 次
发布时间:2019-06-20

本文共 3926 字,大约阅读时间需要 13 分钟。

package com.demo.test3;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;/** * @author QQ: 1236897 * *///工作顺序(0000)和(aaaa,bbbb,cccc,dddd)并行, (aaaa),(bbbb),(cccc),(dddd) 为串行出现public class DemoTEST {    public static void main(String[] args) {        final ThreadService service = new ThreadService();        service.renderPage();    }}class ThreadService {    private final ExecutorService executor = Executors.newCachedThreadPool();    public void renderPage() {        final List
list = new ArrayList
(); list.add("aaaa"); list.add("bbbb"); list.add("ccccc"); list.add("dddddd"); Callable
> task = new Callable
>() { @Override public List
call() throws Exception { List
result = new ArrayList
(); for (String str : list) { System.out.println("input to result....."); Random r = new Random(); double d2 = r.nextDouble() * 5; Thread.sleep(1000*(int)d2); result.add(str); } return result; } }; Future
> future = executor.submit(task); System.out.println("000000000 - "+"=======此处和task 同时并行===================="); try { List
res = future.get(); for (String s : res) { System.out.println(s+"--"+Thread.currentThread().getName()); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } executor.shutdown(); }}

//==============================================================================================

package com.demo.test4;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.concurrent.Callable;import java.util.concurrent.CompletionService;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorCompletionService;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;/** * @author QQ: 1236897 * *///工作方式 - (0000)和(aaaa,bbbb,cccc,dddd)并行开始,(aaaaa)(bbbbb)(ccccc)(ddddd) 并行工作public class DemoTest2 {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        ThreadService service = new ThreadService();        service.renderPage();    }}class ThreadService {    private final ExecutorService executor = Executors.newCachedThreadPool();    public void renderPage() {        final List
list = new ArrayList
(); list.add("aaaa"); list.add("bbbb"); list.add("ccccc"); list.add("dddddd"); CompletionService
completionService = new ExecutorCompletionService
( executor); for (final String s : list) { completionService.submit(new Callable
() { @Override public String call() throws Exception { // TODO Auto-generated method stub Random r = new Random(); double d2 = r.nextDouble() * 5; Thread.sleep(1000 * (int) d2); return s; } }); } System.out.println("00000" + "==========并行开始===="); try { for (int i = 0; i < list.size(); i++) { Future
str = completionService.take(); String s = str.get(); System.out.println(s); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

 

转载于:https://www.cnblogs.com/MarchThree/p/4771052.html

你可能感兴趣的文章
闭包的另一种形式
查看>>
python学习-文件读写整理
查看>>
python之路-02 Python基础
查看>>
解决WebView跟ScrollView冲突
查看>>
1.2.3 自定义ActionBar的风格
查看>>
核心动画(基础动画)
查看>>
essential c++ 读书笔记1
查看>>
Print matrix spiral
查看>>
amazon 设计 9 chat server
查看>>
SpringCloud学习笔记:服务注册与发现Eureka(2)
查看>>
WifiDog系统
查看>>
Data augmentation
查看>>
04 | 深入浅出索引(上)
查看>>
无穷之旅:关于无穷大的文化史 (伊莱·马奥尔 著)
查看>>
Weekly 1
查看>>
gdb 调试工具
查看>>
[ NOI 2002 ] 贪吃的九头龙
查看>>
就南通大学教务管理系统微信公众号的个人看法
查看>>
简述sprintf、fprintf和printf函数的区别
查看>>
My97DatePicker
查看>>