[External] : Re: Virtual thread memory leak because TrackingRootContainer keeps threads

Alan Bateman Alan.Bateman at oracle.com
Fri Jul 26 07:54:33 UTC 2024


On 25/07/2024 14:49, Matthew Swift wrote:
> :
>
> That may be so, but it's not very flexible, unless I'm missing 
> something, because it's not possible to derive the thread name from 
> the task itself. Said otherwise, a ThreadFactory lets you create very 
> generic thread names like "HTTP Connection #1" ahead of time, but 
> doesn't let you derive a rich name from the Runnable itself, like 
> "HTTP Connection from 1.2.3.4 to 5.6.7.8 on port 8080". I tried 
> cheating a bit by relying on the toString() of the Runnable, but sadly 
> this doesn't work because the executor wraps the provided Runnable 
> before calling the thread factory (see 
> ThreadPerTaskExecutor#start(java.lang.Runnable)).

Okay, so this isn't really anything specific to virtual threads, and not 
specific to TPTE either, instead I think you are asking for an overload 
of ES.submit that allows you to set the thread name for the duration of 
the task.  Maybe you've tried this already but a thread can change its 
own name so you can set it at the start of the task or just wrap the 
TPTE to set the name. With a new virtual thread per task then you don't 
have to restore it as the thread terminates immediately after the task 
completes.

-Alan


More information about the loom-dev mailing list