多线程执行同一任务,所有线程返回结果后执行下一步

多线程执行同一任务,所有线程返回结果后执行下一步

执行查询时,在一个方法中查询多种数据,只能等前面数据查询结束后后面的查询才能进行,效率低下.为了提高效率使用多线程并行查询,但是需要每个线程查询的结果,得到所有的结果后在进行下一步操作,因正常的线程方式Thread、Runnable方式因不能获取返回值,所以采用callable实现该功能。

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


/**
 * @Description:多线程执行同一任务,所有线程返回值后执行下一步操作测试(可用于多线程查询)
 * @Auther: 
 * @Date: 
 */
public class ThreadTest {
    private static ExecutorService threadPool = null;

    static {
        threadPool = Executors.newFixedThreadPool(20);
    }

    public static void main(String[] args) {
        try {
//            getNumArg(1);
//            getNumArg(2);
            getForNum();
            System.out.println("方法执行完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
    * @Description: 简单多线程执行(参数利用)
    * @Param: []
    * @return: void
    * @Author: MaoHQ
    * @Date: 2021/3/25
    */
    public static void getNumArg(int num) throws InterruptedException, ExecutionException {
        List<String> tmplist = new ArrayList<String>();
        CountDownLatch latch = new CountDownLatch(2);
        Future<Integer> lnt = threadPool.submit(new TestThread1(1, tmplist,latch));
        Future<Integer> ykt = threadPool.submit(new TestThread2(2, tmplist,latch));
        //等待线程均完成
        latch.await();
        //获取线程返回值
        Integer i1 = lnt.get();
        Integer i2 = ykt.get();
        //线程均执行完后,继续执行
        System.out.println(i1 + i2 + "数据:" +num);
        for (String string : tmplist) {
            System.out.println(string+ "数据:" +num);
        }

    }

    /**
    * @Description: 多线程执行
    * @Param: []
    * @return: void
    * @Author: MaoHQ
    * @Date: 2021/3/25
    */
    public static void getNum() throws InterruptedException, ExecutionException {
        Future<Integer> t1 = threadPool.submit(new TestThread1(1));
        Future<Integer> t2 = threadPool.submit(new TestThread2(2));
        //获取线程返回值
        Integer i1 = t1.get();
        Integer i2 = t2.get();
        //线程均执行完后,继续执行
        System.out.println(i1 + i2);
    }

    /**
    * @Description: 多线程执行(循环)
    * @Param: []
    * @return: void
    * @Author: MaoHQ
    * @Date: 2021/3/29
    */
    public static void getForNum() throws InterruptedException, ExecutionException {
        int total = 5;
        List< Future<Integer>> allFuture = new ArrayList();
        CountDownLatch latch = new CountDownLatch(total);
        for (int i = 0; i <total ; i++) {
            Future<Integer> tmpData = threadPool.submit(new TestThread1(1,latch));
            allFuture.add(tmpData);
        }
        latch.await();
        for (Future future : allFuture) {
            Integer num = (Integer) future.get();
            System.out.println(num);
        }

    }

    /***
    * @Description:  线程2(执行中有睡眠)
    * @Param:
    * @return:
    * @Author: MaoHQ
    * @Date: 2021/3/25
    */
    private static class TestThread2 implements Callable<Integer> {
        private int num;
        private List<String> tmplist;
        private CountDownLatch latch;

        public TestThread2(int num) {
            this.num = num;
        }

        public TestThread2(int num, List<String> tmplist) {
            this.num = num;
            this.tmplist = tmplist;
        }
        public TestThread2(int num, List<String> tmplist, CountDownLatch latch) {
            this.num = num;
            this.tmplist = tmplist;
            this.latch = latch;
        }

        @Override
        public Integer call() throws Exception {
            try {
                System.out.println("正在运行2");
                Thread.sleep(5000);
                if (tmplist != null)
                    tmplist.add("a");
                return num;
            }finally {
                latch.countDown();
            }
        }

    }

    /***
    * @Description: 线程1(执行中无睡眠)
    * @Param:
    * @return:
    * @Author: MaoHQ
    * @Date: 2021/3/25
    */
    private static class TestThread1 implements Callable<Integer> {
        private int num;
        private List<String> tmplist;
        private CountDownLatch latch;

        public TestThread1(int num, List<String> tmplist) {
            this.num = num;
            this.tmplist = tmplist;
        }

        public TestThread1(int num) {
            this.num = num;
        }

        public TestThread1(int num, List<String> tmplist, CountDownLatch latch) {
            this.num = num;
            this.tmplist = tmplist;
            this.latch = latch;
        }
        public TestThread1(int num, CountDownLatch latch) {
            this.num = num;
            this.latch = latch;
        }

        @Override
        public Integer call() throws Exception {
            try {
                if (tmplist != null)
                    tmplist.add("b");
                System.out.println("正在运行");
                return num;
            }finally {
                //线程执行数-1
                latch.countDown();
            }
        }

    }

}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值