Execution context
When we create a Future, the code is executed asynchronously on the JVM, but what is used to execute that code? In fact, the only way to execute code in parallel is to use threads. A naive approach would be to suggest: I should just create a new thread each time I would like to execute a new piece of code. However, that is a really bad idea. First, the number of threads is limited by the operating system; you cannot spawn as many threads as you want. Secondly, you could face thread starvation (https://docs.oracle.com/javase/tutorial/essential/concurrency/starvelive.html); this happens when your CPU spends all of its time switching contexts between threads, instead of executing real code.
OK, but how can you manage the thread ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access