python2处理耗时任务_多任务并行处理

面试偶尔会问多任务并行相关的问题:

假设现在有10个任务,要求同时处理,并且必须所有任务全部完成才返回结果

这个面试题的难点是:

  • 既然要同时处理,那么肯定要用多线程。怎么设计多线程同时处理任务呢?
  • 要求返回结果,那么就不能用简单的Thread+Runnable了,这个是无返回结果的
  • 最难的是,这些任务彼此间还有关系:任务全部结束才算完成

下面3个Demo,CountDownLatch的结果处理交给大家自行完成。

FutureTask

/**
 * @author qiyu
 * @date 2020-09-07
 */
public class FutureTaskDemo {
    

    private static final Random random = new Random();
    private static AtomicInteger count = new AtomicInteger(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
    
        // 准备10个线程
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        // 收集每个任务的结果
        List<Future<Integer>> resultList = new ArrayList<>();

        long start = System.currentTimeMillis();

        // 并行处理10个任务
        for (int i = 0; i < 10; i++) {
    
            // 准备任务
            Callable<Integer> task = () -> {
    
                // 模拟任务耗时 0~4秒
                int seconds = random.nextInt(5);
                TimeUnit.SECONDS.sleep(seconds);
                System.out.println("task is completed! cost:" + seconds + "s left: " + count.decrementAndGet());
                // 模拟返回结果
                return 1;
            };
            // 提交任务
            Future<Integer> partResult = executorService.submit(task);
            // 收集结果
            resultList.add(partResult);
        }

        int result = 0;

        // 阻塞获取并累加结果
        for (Future<Integer> future : resultList) {
    
            result += future.get();
        }

        // 最终全部任务完成,总耗时取决于最耗时的任务时长
        System.out.println("all task is completed! result=" + result + " cost: " + (System.currentTimeMillis() - start) + "ms");
    }
    
}

结果展示

task is completed! cost:0s left: 9
task is completed! cost:1s left: 8
task is completed! cost:1s left: 7
task is completed! cost:2s left: 6
task is completed! cost:3s left: 4
task is completed! cost:3s left: 5
task is completed! cost:3s left: 3
task is completed! cost:3s left: 1
task is completed! cost:3s left: 2
task is completed! cost:4s left: 0
all task is completed! result=10 cost: 4110ms

我原先还写过另一个复杂版本:

/**
 * @author qiyu
 * @date 2020-09-07
 */
public class FutureTaskDemo {
    

    private static final Random random = new Random();
    private static AtomicInteger count = new AtomicInteger(10);

    public static void main(String[] a
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值