I have a significant data set and you want to call a slow but clean method and quickly call a method with side effects from the result of the first. I am not interested in intermediate results, so I would not want to collect them.
The obvious solution is to create a parallel thread, make a slow call, make the thread sequential again and make the call quickly. The problem is that ALL code is executed in one thread, there is no actual parallelism.
Code example:
@Test public void testParallelStream() throws ExecutionException, InterruptedException { ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors() * 2); Set<String> threads = forkJoinPool.submit(()-> new Random().ints(100).boxed() .parallel() .map(this::slowOperation) .sequential() .map(Function.identity())//some fast operation, but must be in single thread .collect(Collectors.toSet()) ).get(); System.out.println(threads); Assert.assertEquals(Runtime.getRuntime().availableProcessors() * 2, threads.size()); } private String slowOperation(int value) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return Thread.currentThread().getName(); }
If I remove sequential
, the code will execute as expected, but obviously the non-parallel operation will be called by multiple threads.
Could you recommend some references to this behavior, or perhaps somehow avoid temporary collections?
source share