首页 > 编程语言 > 详细

java多线程并行操作

时间:2020-07-09 20:08:12      阅读:56      评论:0      收藏:0      [点我收藏+]
    public Map<String, Object> myTrainListDatasTotalCount(HttpServletRequest request) {
        Map<String, Object> attributes = new HashMap<String, Object>();
        BaseInfoEntity baseInfo = this.getBaseInfo(request);

        //通过多个线程并查询
        ExecutorService service = Executors.newFixedThreadPool(3); //创建线程池
        BlockingQueue<Future<String[]>> queue = new LinkedBlockingQueue<Future<String[]>>(); //创建队列
        
        Future<String[]> future = service.submit(this.getWholeMyTrainListCount(baseInfo,null,"all"));
        queue.add(future);
        
        Future<String[]> future1 = service.submit(this.getWholeMyTrainListCount(baseInfo,null,"wait"));
        queue.add(future1);
        
        Future<String[]> future2 = service.submit(this.getWholeMyTrainListCount(baseInfo,null,"complete"));
        queue.add(future2);
        
        int queueSize = queue.size();
        for (int i = 0; i < queueSize; i++) {
            try {
                String[] str = queue.take().get();
                
                //结果处理过程
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        service.shutdown();
        
        return attributes;
    }
    
    public Callable<String[]> getWholeMyTrainListCount(final BaseInfoEntity baseInfo,final WeixPageBean weixPageBean,final String type) { //传递的参数都需要使用final修饰 
         Callable<String[]> callable = new Callable<String[]>() {
                public String[] call() throws Exception {
                    String[] str = new String[2];
                        
                        //处理过程
                        
                    return str;
                }
            };
            return callable;
    }

 

java多线程并行操作

原文:https://www.cnblogs.com/aiyaya/p/13275565.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!