简体   繁体   English

是否不鼓励在Java EE容器中使用Java 8并行流?

[英]Is it discouraged using Java 8 parallel streams inside a Java EE container?

Given that spawning threads in Java EE containers are discouraged . 鉴于不鼓励在Java EE容器中生成线程 Would using the Java 8 parallel streams , which may spawn threads, inside Java EE be discouraged too? 是否也不鼓励在Java EE中使用可能产生线程的Java 8并行流

EDIT See alternate answer from andrepnh . 编辑请参阅andrepnh替代答案。 The below may have been the plan, but it doesn't appear to have played out that way in practice. 以下可能是计划,但在实践中似乎没有这样做。


The way I read it from the lambda-dev mailing list discussion mentioned in the comments: it's not discouraged the way spawning threads is - but won't do anything much for you in a Java EE context. 我从评论中提到的lambda-dev邮件列表讨论中读取它的方式:它并不劝阻产生线程的方式 - 但在Java EE上下文中不会对你做任何事情。

From the linked discussion: 来自链接的讨论:

the Java EE concurrency folks had been already talked through this, and the current outcome is that FJP will gracefully degrade to single-threaded (even caller-context) execution when running from within the EE container Java EE并发人员已经讨论过这个问题,目前的结果是当从EE容器内部运行时,FJP将优雅地降级为单线程(甚至是调用者上下文)执行

So you're able to safely use parallel streams in a procedure or library that runs in both contexts. 因此,您可以在两个上下文中运行的过程或库中安全地使用并行流。 When it's run in a SE environment, it will make with the magical parallel shenanigans - but when it's run in an EE environment it will gracefully degrade to serial execution. 当它在SE环境中运行时,它将使用神奇的并行恶作剧 - 但是当它在EE环境中运行时,它将优雅地降级为串行执行。

Note: the phrase quoted above is future tense - does anyone have a citation for some definitive documentation? 注意:上面引用的短语是将来时态 - 是否有人对某些权威文档有引用?

A heads up, the graceful degradation to single thread is not available. 抬头,单线程的优雅降级不可用。 I also thought it was because of Shorn's answer and that mailing list discussion, but I found out it wasn't while researching for this question . 我也认为这是因为Shorn的答案和邮件列表的讨论,但我发现它不是在研究这个问题时 The mechanism is not in the Java EE 7 spec and it's not in glassfish 4.1. 该机制不在Java EE 7规范中,并且不在glassfish 4.1中。 Even if another container does it, it won't be portable. 即使是另一个容器,它也不会是便携式的。

You can test this by calling the following method: 您可以通过调用以下方法来测试它:

@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);
    }
}

And you'll get something like: 你会得到类似的东西:

Info:   Task 20 on thread http-listener-1(4)
Info:   Task 10 on thread ForkJoinPool.commonPool-worker-3
Info:   Task 28 on thread ForkJoinPool.commonPool-worker-0
...

I've also checked glassfish 4.1.1 source code, and there isn't a single use of ForkJoinPool , ForkJoinWorkerThreadFactory or ForkJoinWorkerThread . 我还检查了glassfish 4.1.1源代码,并没有使用ForkJoinPoolForkJoinWorkerThreadFactoryForkJoinWorkerThread

The mechanism could be added to EE 8, since many frameworks will leverage jdk8 features, but I don't know if it's part of the spec. 该机制可以添加到EE 8,因为许多框架将利用jdk8功能,但我不知道它是否是规范的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM