在JavaEE的应用服务器CompletableFuture / parallelStream [英] CompletableFuture/parallelStream in JavaEE app server

查看:356
本文介绍了在JavaEE的应用服务器CompletableFuture / parallelStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于新的Java8我们得到非常好的功能异步任务,例如

CompletableFuture和.paralellStream()。如果您在Java SE运行这个,因为我已经理解它,你将利用ForkJoinPool,但如果我运行如下面的例子中会发生什么Wildfly或TomcatEE?

  //这里我开始comp.Future没有给一个Executor
测试= CompletableFuture.supplyAsync(() - > timeConsumingMethod());
//这里我开始一个并行流
mList.paralell()。过滤器(......)。收集(Collectors.toList())

会发生什么事,我会在这里我借用从资源如果


  1. 的例子在@Stateful豆跑

  2. 的例子在@Stateless豆跑

  3. 的例子是在CDI豆跑


解决方案

您不应该在Java EE中使用ForkJoinPool。只有应用服务器应该提供的并行结构(如的中的Java EE 7)ManagedExecutorService ,因为线程必须由容器进行管理。

奇怪的是,有在提到声称,ForkJoinPool将适度降低单这个答案邮件列表的消息-threaded在EE容器。我已经在GlassFish 4.1测试这一点,并在创建线程照常。运行此code:

  @Singleton
公共类SomeSingleton {
    公共无效的FireStream(){
        IntStream.range(0,32)
            。平行()
            .mapToObj(I - >的String.format(关于线程%的任务%D,
                我,Thread.currentThread()。的getName()))
            .forEach(的System.out ::的println);
    }
}

我得到以下的输出:

 信息:任务20对HTTP线程监听-1(4)
信息:任务10线程ForkJoinPool.commonPool工人-3
信息:任务21线程HTTP监听-1(4)
信息:任务11线程ForkJoinPool.commonPool工人-3
信息:任务22线程HTTP监听-1(4)
信息:任务8线程ForkJoinPool.commonPool工人-3
信息:任务23线程HTTP监听-1(4)
信息:任务9线程ForkJoinPool.commonPool工人-3
信息:任务18线程HTTP监听-1(4)
信息:任务14线程ForkJoinPool.commonPool工人-3
信息:任务19线程HTTP监听-1(4)
信息:任务15线程ForkJoinPool.commonPool工人-3
信息:任务16线程HTTP监听-1(4)
信息:任务17线程HTTP监听-1(4)
信息:任务4线程HTTP监听-1(4)
信息:任务5线程HTTP监听-1(4)
信息:任务6线程HTTP监听-1(4)
信息:任务7线程HTTP监听-1(4)
信息:任务2线程HTTP监听-1(4)
信息:任务3线程HTTP监听-1(4)
信息:任务0线程HTTP监听-1(4)
信息:任务1线程HTTP监听-1(4)
信息:任务26线程HTTP监听-1(4)
信息:任务27线程HTTP监听-1(4)
信息:任务24对HTTP线程监听-1(4)
信息:任务25线程HTTP监听-1(4)
信息:任务12线程HTTP监听-1(4)
信息:任务13线程HTTP监听-1(4)
信息:任务30日线的HTTP监听-1(4)
信息:任务31线程HTTP监听-1(4)
信息:任务28线程ForkJoinPool.commonPool工人-0
信息:任务29线程ForkJoinPool.commonPool工人-0

也许降解将成为可用的Java EE 8,当大多数个别规格将利用SE 8的功能。


修改

我检查的GlassFish 4.1.1源$ C ​​$ C,并没有一个单一的使用 ForkJoinPool ForkJoinWorkerThreadFactory ForkJoinWorkerThread 。因此,应用程序服务器管理的并行资源不是基于任何的构造。不幸的是,真的没有降解机理提供。

Given the new Java8 we are getting really nice features for asynchronous tasks, e.g. CompletableFuture and .paralellStream(). If you run this in Java SE as I've understood it you will utilize the ForkJoinPool, but what happens if I run the following examples in e.g. Wildfly or TomcatEE?

//Here I start a comp.Future without giving an Executor
test = CompletableFuture.supplyAsync(() -> timeConsumingMethod());
//Here I start a parallel stream 
mList.paralell().filter(...).collect(Collectors.toList())

What happens and where will I borrow my resources from if

  1. The examples are ran in a @Stateful bean
  2. The examples are ran in a @Stateless bean
  3. The examples are ran in a CDI bean

解决方案

You should not use ForkJoinPool in Java EE. Only the app server is supposed to provide constructs for parallelism (like the ManagedExecutorService in Java EE 7), because threads have to be managed by the container.

Curiously, there's a message in the mailing list mentioned in this answer that claims that a ForkJoinPool will gracefully degrade to single-threaded in an EE container. I've tested this with glassfish 4.1 and the usual threads are created. Running this code:

@Singleton
public class SomeSingleton {
    public void fireStream() {
        IntStream.range(0, 32)
            .parallel()
            .mapToObj(i -> String.format("Task %d on thread %s", 
                i, Thread.currentThread().getName()))
            .forEach(System.out::println);
    }
}

I get the following output:

Info:   Task 20 on thread http-listener-1(4)
Info:   Task 10 on thread ForkJoinPool.commonPool-worker-3
Info:   Task 21 on thread http-listener-1(4)
Info:   Task 11 on thread ForkJoinPool.commonPool-worker-3
Info:   Task 22 on thread http-listener-1(4)
Info:   Task 8 on thread ForkJoinPool.commonPool-worker-3
Info:   Task 23 on thread http-listener-1(4)
Info:   Task 9 on thread ForkJoinPool.commonPool-worker-3
Info:   Task 18 on thread http-listener-1(4)
Info:   Task 14 on thread ForkJoinPool.commonPool-worker-3
Info:   Task 19 on thread http-listener-1(4)
Info:   Task 15 on thread ForkJoinPool.commonPool-worker-3
Info:   Task 16 on thread http-listener-1(4)
Info:   Task 17 on thread http-listener-1(4)
Info:   Task 4 on thread http-listener-1(4)
Info:   Task 5 on thread http-listener-1(4)
Info:   Task 6 on thread http-listener-1(4)
Info:   Task 7 on thread http-listener-1(4)
Info:   Task 2 on thread http-listener-1(4)
Info:   Task 3 on thread http-listener-1(4)
Info:   Task 0 on thread http-listener-1(4)
Info:   Task 1 on thread http-listener-1(4)
Info:   Task 26 on thread http-listener-1(4)
Info:   Task 27 on thread http-listener-1(4)
Info:   Task 24 on thread http-listener-1(4)
Info:   Task 25 on thread http-listener-1(4)
Info:   Task 12 on thread http-listener-1(4)
Info:   Task 13 on thread http-listener-1(4)
Info:   Task 30 on thread http-listener-1(4)
Info:   Task 31 on thread http-listener-1(4)
Info:   Task 28 on thread ForkJoinPool.commonPool-worker-0
Info:   Task 29 on thread ForkJoinPool.commonPool-worker-0

Perhaps the degradation will become available on Java EE 8, when most individual specifications will leverage SE 8 features.


Edit

I've checked glassfish 4.1.1 source code, and there isn't a single use of ForkJoinPool, ForkJoinWorkerThreadFactory or ForkJoinWorkerThread. So the app server managed parallelism resources are not based in any of those constructs. Unfortunately, there's really no degradation mechanism available.

这篇关于在JavaEE的应用服务器CompletableFuture / parallelStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆