线程并发查询数据库, 利用CountDownLatch得到三者的数据之后进行处理

 import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 查询三个不同表,查询的结果进行合并
 *
 * @author fdy
 */
public class ThreadOneToMany {
    static ThreadFactory namedThreadFactory = Executors.defaultThreadFactory();
    static ExecutorService pool = new ThreadPoolExecutor(3, 3,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
    static List<String> a = new ArrayList<>();

    static List<String> b = new ArrayList<>();

    static List<String> c = new ArrayList<>();

    static List<String> sumList = new ArrayList<>();

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Map<String, Object> map = new HashMap<>(1);
        map.put("date", "2020-12-27");
        CountDownLatch latch = new CountDownLatch(3);
        pool.submit(() -> {
            a = getADate(map);
            latch.countDown();
            System.out.println("=========================a" + latch.getCount());

        });
        pool.submit(() -> {
            b = getBDate(map);
            latch.countDown();
            System.out.println("=========================b" + latch.getCount());

        });

        pool.submit(() -> {
            c = getCDate(map);
            latch.countDown();
            System.out.println("=========================c" + latch.getCount());

        });

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        System.out.println("=========================" + latch.getCount());

        if (!CollectionUtils.isEmpty(a)
                && !CollectionUtils.isEmpty(b)
                && !CollectionUtils.isEmpty(c)) {
            sumList.addAll(a);
            sumList.addAll(b);
            sumList.addAll(c);
            a.clear();
            b.clear();
            c.clear();

        }
        if (CollectionUtils.isEmpty(a)) {
            System.out.println("a查询结果为空");
        }
        if (CollectionUtils.isEmpty(b)) {
            System.out.println("b查询结果为空");
        }
        if (CollectionUtils.isEmpty(c)) {
            System.out.println("c查询结果为空");
        }
        System.out.println(sumList);
        long end = System.currentTimeMillis();
        System.out.println("===============>>"+(end-start));
        pool.shutdown();
    }

    /**
     * 获取a数据
     *
     * @param map
     * @return
     */
    private static List<String> getCDate(Map<String, Object> map) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            list.add("c" + i);
        }

        return list;
    }

    /**
     * 获取b数据
     *
     * @param map
     * @return
     */
    private static List<String> getBDate(Map<String, Object> map) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            list.add("b" + i);
        }
        return list;
    }

    /**
     * 获取c数据
     *
     * @param map
     * @return
     */
    private static List<String> getADate(Map<String, Object> map) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            list.add("a" + i);
        }
        return list;
    }

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值