スレッドを使用してデータベースを同時にクエリーして効率を向上させることを記録します.

13848 ワード

       //    :3
       CountDownLatch latch = new CountDownLatch(3);
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        List<Future<List<Map<String,Object>>>> futureTaskList = new ArrayList<Future<List<Map<String,Object>>>>(3);

        futureTaskList.add(threadPool.submit(new Callable<List<Map<String,Object>>>() {
            @Override
                public List<Map<String,Object>> call() throws Exception {
                    //    
                    List<Map<String,Object>> list =  inTrainMapper.countDK(params);
                    latch.countDown();
                    return list;
                }
        }));

        futureTaskList.add(threadPool.submit(new Callable<List<Map<String,Object>>>() {
            @Override
            public List<Map<String,Object>> call() throws Exception {
                //      
                List<Map<String,Object>> list = inTrainMapper.countLY(params);
                latch.countDown();
                return list;
            }
        }));

        futureTaskList.add(threadPool.submit(new Callable<List<Map<String,Object>>>() {
            @Override
            public List<Map<String,Object>> call() throws Exception {
                //     
                List<Map<String,Object>> list = unTrain(params);
                latch.countDown();
                return list;
            }
        }));

//         listMap
        List<Map<String, Object>> list1 = new ArrayList<>();
        List<Map<String, Object>> list2 = new ArrayList<>();
        List<Map<String, Object>> list3 = new ArrayList<>();
        try {
            list1 =  futureTaskList.get(0).get();
            list2 = futureTaskList.get(1).get();
            list3 = futureTaskList.get(2).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }