<div dir="ltr"><div>I see, so you would start two VTs. One waiting on the `ReferenceQueue` (and immediately stopping the queue), and another doing the loop. Though I admit that would be correct, but still would be rather inefficient compared to what the JVM could do. Also, it wouldn't necessarily be exactly the same, because for that to work, when you are stopping the queue, you still have to exit with an exception (from the loop), while the JVM can just finish executing the thread. Though I guess one could argue that even in a JVM supported implementation you might want this termination by exception, otherwise there would be an additional possibility of a finally block never executing (which is rather dangerous). That said, if we are relying on the GC to effectively trigger this exception, then that is already an issue, because the GC does not promise that it will clean up everything.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Robert Engels <<a href="mailto:rengels@ix.netcom.com">rengels@ix.netcom.com</a>> ezt írta (időpont: 2023. júl. 3., H, 14:42):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div dir="ltr"></div><div dir="ltr">The code is not correct. Read about reference queues and weak references and how you could have the producer detect when there are no more possible readers and clean itself up. You don’t need to provide a cleaner - it can all be done internal to the “generator” implementation.</div><div dir="ltr"><br></div><div dir="ltr">It is not trivial code but it is straightforward. </div><div dir="ltr"><br></div><div dir="ltr">I’ll give you a clue - you need to have a watcher of the weak references that wakes the blocked producer  </div><div dir="ltr"></div><blockquote type="cite"><div dir="ltr"><br>
</div></blockquote></div></blockquote></div></div>