java并行的执行两个任务

java并行的执行两个任务 ,每个任务又包含多个小任务,下面是一种代码实践的成果

 ExecutorService executor1 = Executors.newSingleThreadExecutor();
        Map<String, Integer> tenantIdNumMap = new HashMap<>();
        FutureTask<Map<String, Integer>> future1 =
                new FutureTask<>(() -> {
                    long time1 = System.currentTimeMillis();
                    ConcurrentHashMap<String, Integer> finalTenantIdNumMap = new ConcurrentHashMap<>();
                    tenantIdList.parallelStream().forEach(tenantId -> {
                        HashMap<String, Object> reqOtherColumn = new HashMap<>();
                        reqOtherColumn.put("beginTime", beginTime);
                        reqOtherColumn.put("endTime", endTime);
                        reqOtherColumn.put("tenantIdList", Arrays.asList(tenantId));
                        //真正的任务在这里执行,这里的返回值类型为String,可以为任意类型
                        List<HashMap<String, String>> tenantNumByIdDistinctInfo = volumeMapper.getTenantNumById(reqOtherColumn);
                        tenantNumByIdDistinctInfo.parallelStream().forEach(map -> {
                            String tenant_id = map.get("tenant_id");
                            Integer idMun = finalTenantIdNumMap.get(tenant_id);
                            if (idMun == null) {
                                finalTenantIdNumMap.put(tenant_id, 1);
                            } else {
                                finalTenantIdNumMap.put(tenant_id, ++idMun);
                            }
                        });
                    });
                    long time2 = System.currentTimeMillis();
                    logger.warn("------33333----2222222--: " + (time2 - time1));
                    return finalTenantIdNumMap;
                });
        executor1.execute(future1);

        ExecutorService executor2 = Executors.newSingleThreadExecutor();
        Map<String, Integer> tenantIdSubjectNumMap = new HashMap<>();
        FutureTask<Map<String, Integer>> future2 =
                new FutureTask<>(() -> {
                    ConcurrentHashMap<String, Integer> finalTenantIdSubjectNumMap = new ConcurrentHashMap<>();
                    long time1 = System.currentTimeMillis();
                    tenantIdList.parallelStream().forEach(tenantId -> {
                        HashMap<String, Object> reqOtherColumn = new HashMap<>();
                        reqOtherColumn.put("beginTime", beginTime);
                        reqOtherColumn.put("endTime", endTime);
                        reqOtherColumn.put("tenantIdList", Arrays.asList(tenantId));
                        List<HashMap<String, String>> tenantSubjectNumByIdDistinctInfo = volumeMapper.getTenantSubjectNumById(reqOtherColumn);
                        tenantSubjectNumByIdDistinctInfo.parallelStream().forEach(map -> {
                            String tenant_id = map.get("tenant_id");
                            Integer idMun = finalTenantIdSubjectNumMap.get(tenant_id);
                            if (idMun == null) {
                                finalTenantIdSubjectNumMap.put(tenant_id, 1);
                            } else {
                                finalTenantIdSubjectNumMap.put(tenant_id, ++idMun);
                            }
                        });
                    });
                    long time2 = System.currentTimeMillis();
                    logger.warn("------4444----2222222--: " + (time2 - time1));
                    return finalTenantIdSubjectNumMap;
                });
        executor2.execute(future2);


        try {
            tenantIdNumMap = future1.get(30000, TimeUnit.MILLISECONDS);//取得结果,同时设置超时执行时间为5秒。同样可以用future.get(),不设置执行超时时间取得结果
        } catch (InterruptedException e) {
            future1.cancel(true);
        } catch (ExecutionException e) {
            future1.cancel(true);
        } catch (TimeoutException e) {
            future1.cancel(true);
        } finally {
            executor1.shutdown();
        }

        try {
            tenantIdSubjectNumMap = future2.get(30000, TimeUnit.MILLISECONDS);//取得结果,同时设置超时执行时间为5秒。同样可以用future.get(),不设置执行超时时间取得结果
        } catch (InterruptedException e) {
            future2.cancel(true);
        } catch (ExecutionException e) {
            future2.cancel(true);
        } catch (TimeoutException e) {
            future2.cancel(true);
        } finally {
            executor2.shutdown();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Java 8 提供了一系列的工具和 API 来帮助我们进行多任务并行处理。其中最常用的是 `java.util.concurrent` 包中的类,比如 `Executor`、`ExecutorService`、`ThreadPoolExecutor` 等。 举个例子,我们可以使用 `ExecutorService` 来创建一个线程池,然后将多个任务封装成 `Callable` 或 `Runnable` 接口的实现类,然后使用 `ExecutorService.submit()` 方法任务提交到线程池中进行执行: ```java ExecutorService executorService = Executors.newFixedThreadPool(4); Future<String> result1 = executorService.submit(() -> "Task 1"); Future<String> result2 = executorService.submit(() -> "Task 2"); String result = result1.get() + " " + result2.get(); System.out.println(result); executorService.shutdown(); ``` 在这个例子中,我们创建了一个可以同时执行 4 个任务的线程池,然后提交了两个任务到线程池中执行。最后使用 `Future.get()` 方法获取任务执行结果,并将两个结果拼接起来。 除了使用线程池以外,Java 8 还提供了一些其他的工具和 API 来帮助我们进行多任务并行处理,比如 `CompletableFuture` 和 `Stream API` 等。 ### 回答2: Java 8引入了新的特性来支持多任务并行处理。其中最常用的特性是流(Stream)和并行流(Parallel Stream)。 流(Stream)是一种功能强大的数据处理工具,它可以让我们以声明式的方式来处理集合或数组。通过使用流,我们可以通过一系列的中间操作过滤、映射或排序数据,并最终进行终端操作,例如求和或聚合。流的特点是可以自动进行数据分块处理,这样可以提高数据处理的效率。但是,普通的流只能在一个线程中进行操作,无法利用多核心处理器的优势。 为了解决这个问题,Java 8引入了并行流(Parallel Stream)这个概念。并行流可以将一个流中的元素自动分块处理,并在多个线程中同时进行处理。这样,我们就可以充分利用多核心处理器的能力,加快数据处理的速度。 使用并行流非常简单,只需要将普通的流调用parallel()方法即可将其转换为并行流。然后,在进行终端操作时,例如使用forEach()或reduce()方法,流会自动将元素分块处理分发给多个线程进行并行处理。在使用并行流时,我们需要注意线程安全的问题,确保共享的数据是可变的。 除了并行流,Java 8还引入了新的线程池框架来支持多任务并行处理。通过使用Fork/Join框架,我们可以将一个任务分解成多个子任务,并通过递归方式并行处理子任务,最后将它们的结果合并起来。这样可以更容易地在多核心处理器上实现任务并行处理。 总之,Java 8提供了多种方式来支持并行处理,包括并行流和Fork/Join框架。通过合理地使用这些特性,我们可以充分利用多核心处理器的能力,提高程序的执行效率。 ### 回答3: Java 8引入了新的并行处理能力,使得多任务处理变得更加简单和高效。Java 8使用了Stream API来实现并行处理,这是一种能够处理大量数据的流式处理方式。 在Java 8中,我们可以使用Stream API来处理集合、数组等数据源。通过调用`parallelStream`方法,我们可以将Stream转换为并行流,从而实现并行处理。 并行处理的好处在于它可以将大量数据分成多个小块,并使用多个线程同时处理这些小块。这样可以充分利用计算资源,提高处理速度。 在并行处理中,我们可以使用`forEach`方法来对每个元素进行处理,也可以使用`map`方法来对元素进行映射。此外,我们还可以在并行处理中使用`filter`方法来过滤元素,或者使用`reduce`方法来将元素进行归约。 值得注意的是,并行处理可能会引发线程安全问题。在使用并行处理时,我们要确保共享对象的线程安全。另外,我们还要避免在并行流中执行有副作用的操作,因为这可能导致意外结果。 总之,Java 8的并行处理能力为我们提供了一种高效处理大量数据的方式。通过使用Stream API,我们可以轻松实现多任务并行处理,并充分利用计算资源,加快程序的执行速度。然而,在使用并行处理时,我们需要注意线程安全和副作用等问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值