Skip to content

Sharing a ForkJoinPool with ParallelStreams

johnmcclean-aol edited this page Jan 24, 2015 · 1 revision

When SimpleReact is using a ForkJoinPool as a TaskExecutor, that can be used to replace the Common ForkJoinPool for the JDK ParallelStreams api.

Use collectResults().block().submit(result -> result.parallelStream()) to reuse the SimpleReact ForkJoinPool for ParallelStreams.

e.g.

Set<String> threadGroup = Collections.synchronizedSet(new TreeSet());

	Integer result = new SimpleReact()
			.<Integer> react(() -> 1, () -> 2, () -> 3)
			.then((it) -> { threadGroup.add(Thread.currentThread().getThreadGroup().getName()); return it * 200;})
			.collectResults().block().<Integer>submit( 
					it -> it.parallelStream()
							.filter(f -> f > 300)
							.map(m ->{ threadGroup.add(Thread.currentThread().getThreadGroup().getName());return m - 5; })
							.reduce(0, (acc, next) -> acc + next));

	

	assertThat(result, is(990));
	assertThat(threadGroup.size(), is(1));
Clone this wiki locally