Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36958
| 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 11:27 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <okikph$20d$1@dont-email.me> (permalink) |
| References | <bca988ab-7993-4b11-866a-bc3ab62e8988@googlegroups.com> |
On 7/17/2017 11:06 AM, Eric Douglas wrote:
> I invoke my main test thread.
> This test thread creates a new instance of TestClassOne.
> TestClassOne creates a new instance of WatchService.
> TestClassOne starts a new thread to sit in the background and wait indefinitely.
> (https://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html#take())
> Main test thread loses reference to it's instance of TestClassOne (variable set to null). Background thread keeps running.
> Instance of TestClassOne should get garbage collected? Can we automatically interrupt the background thread?
The JVM holds references to all running threads: They won't become
garbage simply because your code forgets about them. Also, a running
thread wouldn't stop even if it did somehow become garbage; that would
be subject to all the problems that cause Thread::stop and kin to be
deprecated.
The only safe way to stop a thread is with its cooperation. The
stopper arranges some mutually agreed-upon "Please stop soon" signal,
like a `volatile boolean' that the stoppee tests now and then. Maybe
the stopper also arranges to awaken the stoppee if it might be in a
lengthy wait of some kind: It might interrupt the stoppee, or close
the socket the stoppee is using, or something of that kind -- whatever
will break the waiting thread out of its wait.
If all you need is that the thread goes away in order to allow the
JVM to terminate, it may be simplest to make it a daemon: Call its
setDaemon(true) method before calling start(). The JVM assumes that
it's all right to clobber daemon threads when shutting down, the idea
being that if the asynchronous shutdown causes corruption it won't
matter because the whole Java environment is about to disappear.
--
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