Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36966
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: How do I automatically cleanly terminate a thread? |
| Date | 2017-07-17 16:05 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <okj52o$s2f$1@dont-email.me> (permalink) |
| References | (2 earlier) <23395a95-7741-4c12-bb16-bb50341df864@googlegroups.com> <okiprf$k64$1@dont-email.me> <446909b0-32bf-4c2e-b81c-9e55832a2c8f@googlegroups.com> <okiut4$44e$1@dont-email.me> <5a5e2da1-ab01-4d17-a71d-2770578d9fff@googlegroups.com> |
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.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How do I automatically cleanly terminate a thread? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-17 08:06 -0700
Re: How do I automatically cleanly terminate a thread? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-17 11:27 -0400
Re: How do I automatically cleanly terminate a thread? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-17 09:03 -0700
Re: How do I automatically cleanly terminate a thread? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-07-17 11:53 -0500
Re: How do I automatically cleanly terminate a thread? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-17 10:05 -0700
Re: How do I automatically cleanly terminate a thread? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-17 18:16 +0000
Re: How do I automatically cleanly terminate a thread? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-17 12:18 -0700
Re: How do I automatically cleanly terminate a thread? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-17 16:05 -0400
Re: How do I automatically cleanly terminate a thread? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-07-17 13:29 -0500
Re: How do I automatically cleanly terminate a thread? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-17 13:05 -0400
csiph-web