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
- The examples are ran in a @Stateful bean
- The examples are ran in a @Stateless bean
- 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:
I get the following output:
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
orForkJoinWorkerThread
. So the app server managed parallelism resources are not based in any of those constructs. Unfortunately, there's really no degradation mechanism available.