Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: How do I automatically cleanly terminate a thread? Date: Mon, 17 Jul 2017 16:05:22 -0400 Organization: A noiseless patient Spider Lines: 39 Message-ID: References: <23395a95-7741-4c12-bb16-bb50341df864@googlegroups.com> <446909b0-32bf-4c2e-b81c-9e55832a2c8f@googlegroups.com> <5a5e2da1-ab01-4d17-a71d-2770578d9fff@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 17 Jul 2017 20:01:28 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a3683bc2a147767788aad374f1a1bed9"; logging-data="28751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jZj6yje8vagwEHJ95IZ2X" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: <5a5e2da1-ab01-4d17-a71d-2770578d9fff@googlegroups.com> Content-Language: en-US Cancel-Lock: sha1:P7h4gn2HSU+te1ZcJMjGtCCxzv0= Xref: csiph.com comp.lang.java.programmer:36966 On 7/17/2017 3:18 PM, Eric Douglas wrote: > On Monday, July 17, 2017 at 2:20:21 PM UTC-4, Martin Gregorie wrote: >>> >> IIRC you said that the reference to the thread is lost so it can't be >> shut down. Is there no way this could be preserved, maybe with an >> extended JButton that triggers the thread by calling a method in your >> Model? >> >> This would mean you could keep the thread reference so that your shutdown >> code can terminate it cleanly. >> >> Alternatively, an extended JButton might pass the thread a reference to a >> variable used by shutdown code to tell the thread to terminate. >> > > I did create a close method I can call from the windowClosing method which does send the interrupt to the background thread monitoring for file changes. That's the simple answer. > > I was wondering if I put this in an API and someone else tries to implement it and doesn't call the close method for any reason can I get the background thread to terminate itself. So far our only answer is it should be safe to kill a WatchService sitting on a take() method by running it in a daemon thread. Note that the daemon thread will continue to run until the JVM decides to shut down. That might not be for a l-o-n-g time ... > Ending the GUI terminates the JVM if there are no background (non-daemon) threads running. I didn't see any clean way to trigger this condition to interrupt the thread other than manually calling a method. I had tested setting the reference to null to see if the garbage collector would fire it. I haven't tried this myself, but for belt-AND-suspenders safety you might give the users of your API not a reference to your Thread, but to some kind of proxy object (probably a good idea in any case). You'd make sure to keep no hard references to the proxy, but only a WeakReference registered with a ReferenceQueue. An auxiliary Thread would wait for notification that the proxy object has become garbage, and upon such notice would arrange for the two Threads to terminate, just as if your client had actually remembered to call stopItPlease(). ... but if you're trying to defend against misuse of your API, bear in mind that fools can always outsmart the foolproof. -- esosman@comcast-dot-net.invalid Twelve hundred eighty-three days to go.