多线程编程实现Callable

方法描述:

跳转具体实现方法

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;
    }
}

完。

如果你看到了这里,觉得文章写得不错就给个赞,关注公众号,可订阅更多干货?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢!
公众号:学软件开发就这么简单

发布了19 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/lixibang/article/details/83096833
今日推荐