<div dir="ltr">Rereading what you wrote, I assume you meant that you want to have a `Cleaner` (which will close the queue), but that is quite an issue, because then everyone has to provide their own cleaner thread (since there is no common JDK implementation), and even if there is a common Cleaner that means a considerable overhead (since now there could be contention for registering with it) over what the JVM could do for us.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Attila Kelemen <<a href="mailto:attila.kelemen85@gmail.com">attila.kelemen85@gmail.com</a>> ezt írta (időpont: 2023. júl. 3., H, 13:55):<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="ltr"><div dir="ltr">Robert Engels <<a href="mailto:rengels@ix.netcom.com" target="_blank">rengels@ix.netcom.com</a>> ezt írta (időpont: 2023. júl. 3., H, 13:38):<br></div><div class="gmail_quote"><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">Believe me. Queues are all you need there is no memory leak and no need to “close”. The producer side uses a weak reference to the queue. When there are no more strong references the producer side can terminate. </div><div dir="ltr"><br></div><div dir="ltr">You can’t use a standard blocking queue for this - but the queue implementation is fairly trivial - with a wake-up thread that listens on the weak reference queue. </div><div dir="ltr"></div><blockquote type="cite"><div dir="ltr"><br></div></blockquote></div></blockquote><div><br></div><div>My concern is not that the queue is not getting GCd, but that there will be a forever waiting VT. Correct me, if I'm wrong, but I believe you mean something like this (btw., the below could be implemented standard Java `SynchronousQueue` as well, but I didn't want to bother):</div><div><br></div><div>```</div><div>Iterator<T> toIterator(ForEachable<T> generator) {</div><div>  var queue = ... new SynchronousQueue variant;</div><div>  Thread.startVirtualThread(() -> {</div><div>    try { generator.forEach(e -> queue.put(e)); }</div><div>    finally { queue.finishWriter(); }</div><div>  });</div><div>  return new Iterator<>() { ... obvious implementation based on `queue` ... }</div><div>}</div><div>```</div><div><br></div><div>My problem with this is that, if I stop reading the `Iterator` (before reading all of its elements), then the started VT will never exit, and that looks like a memory leak to me. At least, I can't see how to workaround this issue.</div><div><br></div><div><br></div></div></div>
</blockquote></div>