On Sun, 22 Feb 2026 14:07:49 GMT, Viktor Klang <[email protected]> wrote:

>> Thread.join was historically implemented with a loop of Object.wait 
>> conditioned on isAlive. We couldn't for this virtual threads because it 
>> would pin a virtual thread invoking join to it carrier.  Now that 
>> Object.wait preempts, it means the special implementation of join for 
>> virtual threads can be removed. Where possible, the notifyAll is elided as 
>> it adds to the cost of termination.
>> 
>> The implNote of the join methods is restored (this is implNote text, not 
>> spec, so no CSR required).
>
> src/java.base/share/classes/java/lang/Thread.java line 1909:
> 
>> 1907:         // ensure there is a notifyAll to wake up waiters when this 
>> thread terminates
>> 1908:         if (this instanceof VirtualThread vthread) {
>> 1909:             vthread.beforeJoin();
> 
> If deemed valuable, we might be able to do something more sophisticated as 
> the proposed solution will trigger notifyAll even if all joiners bailed out 
> due to timeout.

I think it's best to keep it as simple as possible. It's not wrong to notifyAll 
if there are no waiters so okay if a joiner bails. My concern with doing 
anything more sophisticated is that it would add counters or other book 
keeping. It's enough to just avoid the notifyAll when the thread terminates 
before anyone calls join.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/29839#discussion_r2845582926

Reply via email to