Parallel and Asynchronous Programming with Streams and CompletableFuture with Venkat Subramaniam
Why Stack Overflow recommends it
Stack Overflow users cite it as a reference, often with timestamps, when explaining how parallel streams and CompletableFuture use the ForkJoinPool common pool, thread counts, and stream behaviour like findAny versus findFirst.
Recommended by 0 people across 0 different months, 2018 to 2018.
What it is
A 3 hour 14 minute Devoxx deep dive session by Venkat Subramaniam on Java 8 parallel streams and CompletableFuture. It starts with a quick introduction to both facilities, then spends most of its time on the gotchas, when to use them, and when to avoid them.
What people said
Cites Markus Mitterauer · 2018-06 · Stack Overflow · on “core-count tasks CompletableFuture slower than parallelStream”
Following is how I understood it from Venkat Subramaniam s talk on Parallel and Asynchronous Programming with Streams and CompletableFuture [this video] : As CompleteableFuture also utilizes ForkJoinPool.commonPool() it may as well use the main thread, and it does under certain circumstances. Given the following example public static void main(String[] args) { CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> numberSupplier()); future.thenAccept(i -> System.out.println("f: " + i + " - " + Thread.currentThread())); sleep(100); //wait for async operations to finish before exiting } private static Integer numberSupplier() { Integer n = 2; System.out.println("c: " + n + " - " +
Cites Azbesciak · 2018-07 · Stack Overflow · on “kotlin coroutines - use main thread in run blocking”
I am trying to execute following code: val jobs = listOf(...) return runBlocking(CommonPool) { val executed = jobs.map { async { it.execute() } }.toTypedArray() awaitAll(*executed) } where jobs is the list of some Supplier s - in synchronus world this should just create, for example, list of ints. Everything works fine, but the problem is the main thread is not utilized. Bellow screenshot from YourKit: [LINK] So, the question is - how can I utilize main thread also? I suppose runBlocking is the problem here, but is there other way to receive the same result? With Java parallel stream it looks far more better, but the main thread is still not utilized entirely (tasks are totally independent).
Cites emon · 2018-10 · Stack Overflow · on “How to get an ideal number of threads in parallel programs in Java?”
General Equation: Number of Threads <= (Number of cores) / (1 - blocking factor) Where 0 <= blocking factor < 1 Number of Core of a machine : Runtime.getRuntime().availableProcessors() Number of Thread you can parallelism, you will get by printing out this code : ForkJoinPool.commonPool() And the number parallelism is Number of Core of your machine - 1. Because that one is for main thread. Source link [this video] Time : 1:09:00
Cites emon · 2018-10 · Stack Overflow · on “Difference between findAny() and findFirst() in Java 8”
In stream findFirst and findAny return first element and do not execute the rest but in parallelStream, it is not passible to say the order and parallelStream executes the rest of the collection. Reference [this video] Time 1:25:00