多任务并行处理相关面试题

我自己面试时被问过两次多任务并行相关的问题:

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

这个面试题的难点是:

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

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

FutureTask

/*** @author mx*/
public class FutureTaskDemo {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 = ThreadLocalRandom.current().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 mx*/
public class FutureTaskDemo {private static AtomicInteger count = new AtomicInteger(10);public static void main(String[] args) throws ExecutionException, InterruptedException {// 准备10个线程ExecutorService executorService = Executors.newFixedThreadPool(10);// 收集任务List<Callable<Integer>> taskList = new ArrayList<>();// 收集结果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 = ThreadLocalRandom.current().nextInt(5);TimeUnit.SECONDS.sleep(seconds);System.out.println("task is completed! cost:" + seconds + "s left: " + count.decrementAndGet());// 模拟返回结果return 1;};// 收集任务taskList.add(task);}// 10个任务并行执行for (int i = 0; i < 10; i++) {// 从任务列表取出任务,丢到线程池执行Future<Integer> partResult = executorService.submit(taskList.get(i));// 收集异步结果resultList.add(partResult);}// 最终结果,用于累加int result = 0;// 是否全部结束,否则一直循环等待while (notFinished(resultList)) {// wait for all task to be completed...}// 主线程执行到这,肯定全部任务已经结束,所以get()会立即返回结果,不会再阻塞等待for (Future<Integer> future : resultList) {result += future.get();}// 最终全部任务完成,总耗时取决于最耗时的任务时长System.out.println("all task is completed! result=" + result + " cost: " + (System.currentTimeMillis() - start) + "ms");}/*** 是否全部完成** @param list* @return*/private static boolean notFinished(List<Future<Integer>> list) {for (Future<Integer> future : list) {if (!future.isDone()) {return true;}}return false;}
}

结果展示

task is completed! cost:0s left: 9

task is completed! cost:0s left: 8

task is completed! cost:2s left: 7

task is completed! cost:3s left: 6

task is completed! cost:3s left: 3

task is completed! cost:3s left: 4

task is completed! cost:3s left: 5

task is completed! cost:4s left: 2

task is completed! cost:4s left: 1

task is completed! cost:4s left: 0

all task is completed! result=10 cost: 4051ms

在当前场景下,其实没必要。

有些人可能觉得第一个版本会出现以下问题:

假设总共就2个任务,但是第一个任务耗时3秒,第二个任务耗时1秒。第二个任务的1秒是建立在第一个任务的3秒后,所以总耗时就变成了4秒。

实际上并不会。当future1#get()阻塞获取第一个任务结果的过程中,第二个任务已经完成,所以future2.get()不会再阻塞,而是直接返回结果。

CountDownLatch

CountDownLatch是什么?学名叫闭锁,俗名叫门栓。

/*** @author mx*/
public class CountDownLatchDemo {public static void main(String[] args) throws InterruptedException {// 1.先看简单demo,了解下CountDownLatchmainThreadAndAsyncThread();// 2.尝试使用CountDownLatch解决任务并行问题(不处理结果)
//        multiThreadTask();}/*** CountDownLatch简单demo** @throws InterruptedException*/private static void mainThreadAndAsyncThread() throws InterruptedException {// 准备一个countDownLatch,设置10,相当于给门加了10把锁CountDownLatch countDownLatch = new CountDownLatch(10);long start = System.currentTimeMillis();// 副线程去处理任务,每个任务耗时1秒,每处理完1个任务就解开一把锁new Thread(() -> {for (int i = 0; i < 10; i++) {try {Thread.sleep(1000L);} catch (InterruptedException e) {e.printStackTrace();}countDownAndPrint(countDownLatch, 1);}}).start();// 一夫当关,万夫莫开。只要门上10把锁没有全部解开,任何线程都别想想往下走countDownLatch.await();System.out.println("all task is completed! cost: " + (System.currentTimeMillis() - start) + "ms");}/*** CountDownLatch应用:演示10个任务并行执行,全部完成后返回结果** @throws InterruptedException*/private static void multiThreadTask() throws InterruptedException {// 准备一个countDownLatch,设置10,相当于给门加了10把锁CountDownLatch countDownLatch = new CountDownLatch(10);long start = System.currentTimeMillis();// 启动10个线程执行任务for (int i = 0; i < 10; i++) {new Thread(() -> {try {// 线程进来执行任务,随机睡0~4秒int seconds = ThreadLocalRandom.current().nextInt(5);TimeUnit.SECONDS.sleep(seconds);// 每完成一个任务,解开一把锁countDownAndPrint(countDownLatch, seconds);} catch (InterruptedException e) {e.printStackTrace();}}).start();}// 一夫当关,万夫莫开。只要门上10把锁没有全部解开,任何线程都别想想往下走countDownLatch.await();System.out.println("all task is completed! cost: " + (System.currentTimeMillis() - start) + "ms");}/*** countDown()并且打印,其实是不需要加synchronized的,这里只是为了多线程环境下正确打印** @param countDownLatch* @param seconds*/private static synchronized void countDownAndPrint(CountDownLatch countDownLatch, int seconds) {countDownLatch.countDown();System.out.println("task completed, cost: " + seconds + "s left: " + countDownLatch.getCount());}
}

结果展示

task is completed! cost:0s left: 9

task is completed! cost:0s left: 8

task is completed! cost:0s left: 7

task is completed! cost:2s left: 6

task is completed! cost:2s left: 5

task is completed! cost:2s left: 3

task is completed! cost:2s left: 4

task is completed! cost:3s left: 2

task is completed! cost:4s left: 1

task is completed! cost:4s left: 0

all task is completed! result=10 cost: 4049ms

CompletableFuture

顺便说一句,上面的两个Demo都是闹着玩呢,实际开发别傻不拉几地自己写哈...会被打,而且是背身单打颜扣的那种。

/*** @author mx*/
public class CompletableFutureDemo {private static AtomicInteger count = new AtomicInteger(10);public static void main(String[] args) throws InterruptedException, ExecutionException {// 准备10个线程ExecutorService executorService = Executors.newFixedThreadPool(10);// 收集结果List<CompletableFuture<Integer>> resultList = new ArrayList<>();long start = System.currentTimeMillis();// 任务并行for (int i = 0; i < 10; i++) {CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {// 模拟任务耗时 0~4秒try {int seconds = ThreadLocalRandom.current().nextInt(5);TimeUnit.SECONDS.sleep(seconds);System.out.println("task is completed! cost:" + seconds + "s left: " + count.decrementAndGet());} catch (InterruptedException e) {e.printStackTrace();}// 模拟返回结果return 1;}, executorService);resultList.add(completableFuture);}// 处理结果int result = 0;for (CompletableFuture<Integer> completableFuture : resultList) {result += completableFuture.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:0s left: 8

task is completed! cost:0s left: 7

task is completed! cost:0s left: 6

task is completed! cost:2s left: 5

task is completed! cost:3s left: 4

task is completed! cost:3s left: 3

task is completed! cost:3s left: 2

task is completed! cost:4s left: 1

task is completed! cost:4s left: 0

all task is completed! result=10 cost: 4051ms

实际开发案例

public class CompletableFutureDemo {private static AtomicInteger count = new AtomicInteger(2);public static void main(String[] args) throws InterruptedException, ExecutionException {// 准备10个线程ExecutorService executorService = Executors.newFixedThreadPool(2);long start = System.currentTimeMillis();// 模拟处理订单CompletableFuture<Void> dealOrder = CompletableFuture.runAsync(() -> {// 模拟任务耗时 0~4秒try {int seconds = ThreadLocalRandom.current().nextInt(5);TimeUnit.SECONDS.sleep(seconds);System.out.println("task is completed! cost:" + seconds + "s left: " + count.decrementAndGet());} catch (InterruptedException e) {e.printStackTrace();}}, executorService);// 模拟处理库存CompletableFuture<Void> dealStock = CompletableFuture.runAsync(() -> {// 模拟任务耗时 0~4秒try {int seconds = ThreadLocalRandom.current().nextInt(5);TimeUnit.SECONDS.sleep(seconds);System.out.println("task is completed! cost:" + seconds + "s left: " + count.decrementAndGet());} catch (InterruptedException e) {e.printStackTrace();}}, executorService);// 可变参数,可以传任意个CompletableFuture,阻塞等待所有任务完成CompletableFuture.allOf(dealOrder, dealStock).get();// 最终全部任务完成,总耗时取决于最耗时的任务时长System.out.println("all task is completed! cost: " + (System.currentTimeMillis() - start) + "ms");}
}

结果展示

task is completed! cost:2s left: 1

task is completed! cost:3s left: 0

all task is completed! cost: 3058ms

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/317193.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pyqt6 + pycharm 搭建+使用入门

首先安装PyQt6和PyQt6-tools。使用如下命令&#xff1a; pip install PyQt6 PyQt6-tools 但是运行后会报如下错误&#xff1a; 这个时候按照提示执行命令升级pip即可 python.exe -m pip install --upgrade pip 配置pycharm&#xff1a; 打开pycharm&#xff0c;进入setting&am…

mysql查询表里的重复数据方法:

1 2 3 4 INSERT INTO hk_test(username, passwd) VALUES (qmf1, qmf1),(qmf2, qmf11) delete from hk_test where usernameqmf1 and passwdqmf1 MySQL里查询表里的重复数据记录&#xff1a; 先查看重复的原始数据&#xff1a; 场景一&#xff1a;列出username字段有重读的数…

工具变量-ESG基金持股数据集(2008-2022年)

一、数据介绍 数据名称&#xff1a;工具变量-ESG基金持股数据 数据范围&#xff1a;A股上市公司 数据年份&#xff1a;2008-2022年 样本数量&#xff1a;41621条 数据来源&#xff1a;中国责任投资年度报告、上市公司年报 数据整理&#xff1a;自主整理 二、参考文献 […

微服务-@FeignClient 与 Feign 隔离

FeignClient 扫描 FeignClientsRegistrar#registerBeanDefinitions public void registerBeanDefinitions(AnnotationMetadata metadata,BeanDefinitionRegistry registry) { // 注册默认配置 registerDefaultConfiguration(metadata, registry); registerFeignClients(metada…

什么是差值表达式

在Vue.js中&#xff0c;差值表达式是一种基本的数据绑定形式&#xff0c;用于将数据绑定到文档对象模型&#xff08;DOM&#xff09;上。差值表达式通常使用双大括号 {{ }} 来表示&#xff0c;这种语法非常直观。当Vue实例的数据发生变化时&#xff0c;差值表达式的内容也会相应…

八大算法排序@希尔排序(C语言版本)

目录 希尔排序概念算法思想示例分析结论算法步骤选择增量序列按增量分组逐步缩小增量 算法优势 代码实现核心算法希尔排序代码实现&#xff1a; 时间复杂度空间复杂度特性总结 该排序会关联到直接插入排序的知识点&#xff0c;如果对于直接插入排序还有所疑惑&#xff0c;可以跳…

数据结构学习 jz42连续子数组最大和

关键词&#xff1a;动态规划 滚动数组 最长上升子序列 这道题比较简单&#xff0c;类似最长上升子序列&#xff0c;比最长上升子序列简单。 和最长上升子序列的区别&#xff1a;这道题因为是连续的&#xff0c;所以只用记录max就好了。最长上升子序列是不连续的&#xff0c;所…

FA发放云桌面并与FC对接

&#xff08;7&#xff09;分配桌面&#xff08;该组为刚刚创建的域名用户和组&#xff09;&#xff0c;确认无误&#xff0c;直接发放 &#xff08;8&#xff09;可在任务中心查看发放的进度 3、FA的登录流程 &#xff08;1&#xff09;登录WI&#xff1a;客户端访问VLB&…

网络安全B模块(笔记详解)- 数字取证

数据分析数字取证-attack 1.使用Wireshark查看并分析Windows 7桌面下的attack.pcapng数据包文件,通过分析数据包attack.pcapng找出恶意用户的IP地址,并将恶意用户的IP地址作为Flag(形式:[IP地址])提交; 解析:http.request.method==POST ​ Flag:[172.16.1.102] 2.继续…

国图公考:公务员政审阶段,这四类家庭不予通过!

公务员考试需要经过笔试、面试、体检、政审等步骤&#xff0c;全部合格之后才能成为一名公务员&#xff0c;其中政审的要求是非常严格的&#xff0c;如果你是以下四类家庭的孩子&#xff0c;政审是不通过的。 1. 配偶、直系亲属和三代以内旁系亲属因参与民族、宗教、非法宗教、…

vue3项目创建

安装node.js vue --version &#xff08;4.5.0以上&#xff09; npm install -g vue/cli vue create 项目名称 npm run dev 启动 npm run build 打包 ———————— vite 创建工程 npm create vuelatest npm i npm run dev 启动 npm run build 打包 项目结构…

wordpress在界面将站点地址直接修改为https导致上不去问题的解决办法

wordpress在界面将站点地址直接修改为https导致上不去问题的解决办法 #修改数据库yz_options