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 Listlist = 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 Listlist = 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(); } }}