标签:
很多时候,我们使用线程去处理一些业务,并希望得到结果,这时候,我们可以使用Callable。
下面例子,模拟使用线程查询DB得到一个List。
线程,返回一个List数据
package com.nicchagil.study.thread.cnblogs.No02可返回值的线程; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; public class QueryDataThread implements Callable<List<Map<String, Object>>> { private Map<String, Object> param = null; public QueryDataThread(Map<String, Object> param) { super(); this.param = param; } @Override public List<Map<String, Object>> call() throws Exception { /* 模拟查询DB后得到结果集合 */ System.out.println("param -> " + param); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("id", "1001"); map1.put("name", "nick huang"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("id", "1002"); map2.put("name", "darren lin"); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); list.add(map1); list.add(map2); /* 设置短暂的睡眠以便观察效果 */ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return list; } }
启动线程,并等待返回结果
package com.nicchagil.study.thread.cnblogs.No02可返回值的线程; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Call { public static void main(String[] args) { List<Map<String, Object>> list = null; try { list = query(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (list != null && list.size() > 0) { for (Map<String, Object> map : list) { System.out.println("id : " + map.get("id") + ", name : " + map.get("name")); } } } /** * 模拟查询的业务方法 */ public static List<Map<String, Object>> query() throws InterruptedException, ExecutionException { ExecutorService es = Executors.newCachedThreadPool(); /* 模拟查询参数 */ Map<String, Object> param = new HashMap<String, Object>(); param.put("username", "derick"); Future<List<Map<String, Object>>> future = es.submit(new QueryDataThread(param)); List<Map<String, Object>> list = null; try { list = future.get(); } finally { es.shutdown(); } return list; } }
查询用户数据的线程
package com.nicchagil.study.thread.cnblogs.No02可返回值的线程.No002多个查询; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; public class QueryDataThread implements Callable<List<Map<String, Object>>> { private Map<String, Object> param = null; public QueryDataThread(Map<String, Object> param) { super(); this.param = param; } @Override public List<Map<String, Object>> call() throws Exception { /* 模拟查询DB后得到结果集合 */ System.out.println("query user param -> " + param); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("id", "1001"); map1.put("name", "nick huang"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("id", "1002"); map2.put("name", "darren lin"); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); list.add(map1); list.add(map2); /* 设置短暂的睡眠以便观察效果 */ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return list; } }
查询地址数据的线程
package com.nicchagil.study.thread.cnblogs.No02可返回值的线程.No002多个查询; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; public class QueryExtDataThread implements Callable<Map<String, Object>> { private Map<String, Object> param = null; public QueryExtDataThread(Map<String, Object> param) { super(); this.param = param; } @Override public Map<String, Object> call() throws Exception { /* 模拟查询DB后得到结果集合 */ System.out.println("query address param -> " + param); Map<String, Object> map = new HashMap<String, Object>(); map.put("1001", "hz"); map.put("1002", "pl"); /* 设置短暂的睡眠以便观察效果 */ try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return map; } }
并发调用查询用户数据和地址数据的线程,然后合并数据,并返回
package com.nicchagil.study.thread.cnblogs.No02可返回值的线程.No002多个查询; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Call { public static void main(String[] args) { List<Map<String, Object>> list = null; try { list = query(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (list != null && list.size() > 0) { for (Map<String, Object> map : list) { System.out.println(map); } } } /** * 模拟查询的业务方法 */ public static List<Map<String, Object>> query() throws InterruptedException, ExecutionException { ExecutorService es = Executors.newCachedThreadPool(); /* 模拟查询参数 */ Map<String, Object> param = new HashMap<String, Object>(); param.put("username", "derick"); Future<List<Map<String, Object>>> userFuture = es.submit(new QueryDataThread(param)); Future<Map<String, Object>> addressFuture = es.submit(new QueryExtDataThread(param)); List<Map<String, Object>> userList = null; Map<String, Object> addressMap = null; try { userList = userFuture.get(); addressMap = addressFuture.get(); } finally { es.shutdown(); } /* 合并业务数据 */ if (userList != null && userList.size() > 0) { for (Map<String, Object> map : userList) { map.put("address", addressMap.get(map.get("id"))); } } return userList; } }
ExecutorService.submit(java.util.concurrent.Callable)
标签:
原文地址:http://www.cnblogs.com/nick-huang/p/4743705.html