多執行緒程式設計實現Callable
阿新 • • 發佈:2018-12-15
方法描述:
1、Callable最重要的兩個優點:可以獲取返回值
、丟擲異常
2、執行緒實現
2.1、定義執行緒實現Callable方法,並設定返回的資料型別,如:Callable<List<Map<String, Object>>>
2.2、可實現多個構造方法,提供其他方法呼叫,如:
public MyThread2(CountDownLatch countDownLatch){... }
public MyThread2(String type, String username) {.... }
public MyThread2(CountDownLatch countDownLatch, String type, String username) {... }
2.3、重寫call方法,可以再該方法中直接返回資料,可丟擲異常.
public List<Map<String, Object>> call() throws Exception {
//doing something
return list;
}
2.3、呼叫執行緒
2.3.1、例項化ExecutorService物件、定義future物件接受結果
2.3.2、ExecutorService物件呼叫執行緒
2.3.3、future物件阻塞等待資料
// 這個執行緒池可以根據需要例項化不同的執行緒池
ExecutorService exec = Executors.newCachedThreadPool();
List<Future<List<Map<String, Object>>>> results = new ArrayList<>();//Future 相當於是用來存放Executor執行的結果的一種容器
String[] threadNames = new String[]{"other", "tgbms_dy", "sxxm", "htsq", "newOa", "ECN", "common", "BhtXtpt" , "JdgcjXtp", "WddXtpt", "XjbXtp"};
for (String item : threadNames) {
System.out.println("執行執行緒:" + item);
results.add(exec.submit(new MyThread2(item, username)));
}
for (Future<List<Map<String, Object>>> fs : results) {
//阻塞阻塞等待
List<Map<String, Object>> list = fs.get();
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
HashMap<String, Object> map = new HashMap<>();
map.put("system", list.get(i).get("system"));
map.put("todoTaskCount", list.get(i).get("todoTaskCount"));
map.put("systemName", list.get(i).get("systemName"));
data.add(map);
}
}
}
exec.shutdown();
具體實現方式:
呼叫主方法類
/**
*多執行緒獲取待辦資料
*
* @param username
* @return
*/
public static void main(String[] args) throws Exception {
String username = "li_xibang";
List<Map<String, Object>> data = new ArrayList<>();
long time = System.currentTimeMillis();
// 這個執行緒池可以根據需要例項化不同的執行緒池
ExecutorService exec = Executors.newCachedThreadPool();
List<Future<List<Map<String, Object>>>> results = new ArrayList<>();//Future 相當於是用來存放Executor執行的結果的一種容器
String[] threadNames = new String[]{"other", "tgbms_dy", "sxxm", "htsq", "newOa", "ECN", "common", "BhtXtpt", "JdgcjXtp", "WddXtpt", "XjbXtp"};
//通過標記迴圈調多執行緒
for (String item : threadNames) {
System.out.println("執行執行緒:" + item);
results.add(exec.submit(new MyThread2(item, username)));
}
for (Future<List<Map<String, Object>>> fs : results) {
//阻塞阻塞等待(再次回一致等待資料的返回)
List<Map<String, Object>> list = fs.get();
HashMap<String, Object> map = new HashMap<>();
map.put("system", list.get(0).get("system"));
map.put("todoTaskCount", list.get(0).get("todoTaskCount"));
map.put("systemName", list.get(0).get("systemName"));
data.add(map);
System.out.println("還未完成");
}
exec.shutdown();
long time2 = System.currentTimeMillis();
System.out.println("所花費時間:" + (time2 - time));
System.out.println(Thread.currentThread().getName() + "結束");
}
執行緒實現類
package com.sanxia.gaoke.thread;
import com.sanxia.gaoke.service.impl.TodoServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
/**
* Created by Administrator on 2018/6/5.
*/
public class MyThread2 implements Callable<List<Map<String, Object>>> {
private String taskType;
private String username;
private CountDownLatch countDownLatch;
public MyThread2(CountDownLatch countDownLatch){
this.countDownLatch = countDownLatch;
}
public MyThread2(String type, String username) {
this.taskType = type;
this.username = username;
}
public MyThread2(CountDownLatch countDownLatch, String type, String username) {
this.countDownLatch = countDownLatch;
this.taskType = type;
this.username = username;
}
@Override
public List<Map<String, Object>> call() throws Exception {
List<Map<String, Object>> list = new ArrayList<>();
try {
if (taskType.equals("other")) {
//其它待辦數
list = new TodoServiceImpl().getOtherNum(list, username);
} else if (taskType.equals("tgbms_dy")) {
//xx待辦數
list = new TodoServiceImpl().getTgbmsNum(list, username);
} else if (taskType.equals("sxxm")) {
//xx系統待辦數
list = new TodoServiceImpl().getSxxmNum(list, username);
} else if (taskType.equals("htsq")) {
//xx系統待辦數獲取
list = new TodoServiceImpl().getQdpNum(list, username);
} else if (taskType.equals("newOa")) {
//新OA系統待辦數
list = new TodoServiceImpl().getNewOaNum(list, username);
} else if (taskType.equals("ECN")) {
//ECN待辦數
list = new TodoServiceImpl().getEcnNum(list, username);
} else if (taskType.equals("common")) {
//xx系統待辦數
list = new TodoServiceImpl().getOaWithNum(list, username);
} else {
System.out.println("引數taskType為空,請確認...");
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}