天天看點

executorService invokeAll 方法調用Callable

package org.herry.thread.four;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

public class TestInvoke {

public static void main(String[] args) {

ExecutorService executorService = Executors.newFixedThreadPool(10);

List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();

Callable<Integer> task = null;

for (int i = 0; i < 10; i++) {

task = new Callable<Integer>() {

@Override

public Integer call() throws Exception {

int ran = new Random().nextInt(1000);

Thread.sleep(ran);

System.out.println(Thread.currentThread().getName()

+ "休息了:" + ran);

return ran;

}

};

tasks.add(task);

}

long s = System.currentTimeMillis();

try {

List<Future<Integer>> results = executorService.invokeAll(tasks);

System.out.println("完成任務一共消耗了:" + (System.currentTimeMillis() - s)+"毫秒");

for (int i = 0; i < results.size(); i++) {

System.out.println(results.get(i).get());

}

} catch (Exception e) {

e.printStackTrace();

}

executorService.shutdown();

}

}

//通過線程池調用   exec.invokeAll(taskList)方法,taskList實作了Callable 接口,

//多線程阻塞等待結果傳回示例

private void execTodoTask(Map<String, Object> paraMap,Map<String, Object> resultMap,String Ecp) {

TodoTask ecpTask2=null;

TodoTask ecpTask=null;

TodoTask bamsTask=null;

if(EipMainController.SYSTEM_ID_ECP_TWO.equals(Ecp)){

TodoServiceECP2 ecpService2 = new TodoServiceECP2();

ecpService2.setProcessService(processRunService);

ecpTask2 = new TodoTask(paraMap, ecpService2);

}else{

TodoServiceECP ecpService = new TodoServiceECP();

ecpService.setProcessService(processRunService);

ecpTask = new TodoTask(paraMap, ecpService);

TodoServiceBAMS bamsService = new TodoServiceBAMS();

bamsTask = new TodoTask(paraMap, bamsService);

}

//任務清單

List<TodoTask> taskList = new ArrayList<TodoTask>();

if(EipMainController.SYSTEM_ID_ECP_TWO.equals(Ecp)){

taskList.add(ecpTask2);

}else{

taskList.add(ecpTask);

taskList.add(bamsTask);

}

//得到線程池

ExecutorService exec = ThreadPoolUtil.getExecutorService();

try {

List<Future<Map<String, Object>>> futureList = exec.invokeAll(taskList);

for (Future<Map<String, Object>> future : futureList) {

try {

Map<String, Object> tempMap = future.get();

resultMap.putAll(tempMap);

}catch (ExecutionException e) {

e.printStackTrace();

//轉換成運作時異常重新抛出

throw new RuntimeException(e);

}

}

} catch (InterruptedException e) {

//恢複線程中斷狀态

Thread.currentThread().interrupt();

}

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

public class ThreadPoolUtil {

private static final ExecutorService executors;

private static final ScheduledExecutorService sheduledThreadPool;

static{

executors = Executors.newFixedThreadPool(6);

sheduledThreadPool = Executors.newScheduledThreadPool(3);

}

public static ExecutorService getExecutorService(){

return executors;

}

public static ScheduledExecutorService getScheduledExecutorService(){

return sheduledThreadPool;

}

}

//實作Callable接口

public class TodoTask implements Callable<Map<String, Object>>{

private Map<String, Object> para;

private TodoService<Map<String, Object>> service;

public TodoTask(Map<String, Object> para,TodoService<Map<String, Object>> service){

this.para = para;

this.service = service;

}

@Override

public Map<String, Object> call() throws Exception {

return service.getTodoData(para);

}

}

public interface TodoService<T> {

public T getTodoData(Map<String, Object> para) throws TodoException;

}

//實作服務接口

public class TodoServiceECP implements TodoService<Map<String, Object>>{

private ProcessRunService processService;

public void setProcessService(ProcessRunService processService){

this.processService = processService;

}

@Override

public Map<String, Object> getTodoData(Map<String, Object> para) {

QueryFilter filter = new QueryFilter();

PageBean page = new PageBean();

filter.setFilters(para);

filter.setPageBean(page);

page.setPagesize((Integer) para.get("pageSize"));

page.setCurrentPage(Integer.parseInt((String) para.get("currentPage")));

List<ProcessRun> processRunList = processService.getMyTodoTaskPor4Eip(filter);

Map<String, Object> result = new HashMap<String, Object>();

result.put("totalProcess", page.getTotalCount());

String systemId = (String) para.get("systemId");

if(EipMainController.SYSTEM_ID_ECP.equals(systemId)){

result.put("totalPage", page.getTotalPage());

result.put("processRunList", processRunList);

}

return result;

}

}