Thread vs Tasks

Ron Pressler ron.pressler at oracle.com
Thu Nov 18 20:50:39 UTC 2021


It’s up to you what kind of threads you’d like to use, but the default is, indeed virtual, as that makes more sense in more situations, and yes, StructuredExecutor will spawn a new thread for every task (the unstructured analogous mechanism is Executors.newThreadPerTaskExecutor [1]). This is because this mechanism is meant to assist not only in code organisation, but also in observability, and tasks are not observable entities. The hierarchical structure created by StructuredExecutor is, therefore, reified in the threads, and you can observe it with the new thread dump mechanism mentioned in the JEP.

— Ron

[1]: https://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/Executors.html#newThreadPerTaskExecutor(java.util.concurrent.ThreadFactory)



> On 18 Nov 2021, at 17:32, Eric Kolotyluk <eric at kolotyluk.net> wrote:
> 
> Sorry if this is repeated, but for some reason, posting from Microsoft
> Windows 10 Mail does not seem to get to the list, but Gmail does?
> 
> From
> StructuredExecutor.html#fork(java.util.concurrent.Callable,java.util.function.BiConsumer)
> <Fromhttps://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/StructuredExecutor.html#fork(java.util.concurrent.Callable,java.util.function.BiConsumer)>
> 
> 
> 
> "Starts a new thread in this executor to run the given task and an
> operation to run when the task completes."
> 
> 
> 
> In my mind, I am trying to separate the concept of Thread vs Task. Is this
> saying that there is always a 1:1 correspondence between Thread and Task,
> that StructuredExecutor only spawns Threads?
> 
> 
> 
> I was under the impression with Executor operations, such as ForkJoinPool,
> that there was a 1:many relationship between Worker Threads and Tasks,
> where each worker could handle many Tasks. Am I misunderstanding this?
> 
> 
> 
> Indeed, it may be that for Virtual Threads it makes sense to implement a
> 1:1 relationship, but StructuredExecutor also supports Platform Threads,
> and such a 1:1 relationship might be horrible.
> 
> 
> 
> Cheers, Eric



More information about the loom-dev mailing list