Dr.Knowitall
Dr.Knowitall

Reputation: 10468

Destroying Threads in Java

I saw some posts on stackoverflow and read through the tutorials that oracle posted on destroying threads. From what I understand, once you start() a thread, you cannot use stop() to remove that thread. Instead of actually removing the thread from the scheduler, it is recommended to make the thread sleep() indefinitely. Is this thinking right?

Also this brings me to my next question, is this why people use thread pools?. Instead of "sleeping" a thread, it is more resourceful to use that thread to work on other jobs rather than creating new threads. Please let me know if my understanding of multi-thread management is right. It doesn't make sense that Java doesn't allow for a thread to be completely removed in a safe manner.

Upvotes: 2

Views: 183

Answers (2)

Alex DiCarlo
Alex DiCarlo

Reputation: 4891

As templatetypedef mentioned, you shouldn't be forcibly stopping a thread, you should signal to a thread to stop. For example, whenever your thread is blocking, it should be done in a while loop that tests the condition it is blocking on, as well as the condition to quit:

while (!condition && !stop) {
    try {
        someBlockingFunction(); // A lock, take on a BlockingQueue, etc.
    } catch (InterruptedException e) { //ignored }
}

Upon exiting the while loop, check to see if we have been signaled to stop (stop = true) by another thread, if so return from the run() function to allow the thread to clean itself up.

Upvotes: 1

templatetypedef
templatetypedef

Reputation: 372674

It is generally considered a very bad idea to forcibly stop a thread once it has started (or to make it sleep indefinitely) because the thread will not be able to clean up any of the resources it has acquired. For example, if a thread acquired a lock and is forcibly killed or slept indefinitely, then the lock will not be released and deadlock can ensue. Similarly, if the thread was making changes to a data structure and killed early, the data structure might be in a corrupted state, causing serious problems later on.

The best way to stop a thread in Java is to interrupt the thread and tell it that it needs to try to shut down as soon as possible. That way, the thread can try to stop what it's doing and release any resources before shutting down. In other words, you request that the thread shut down, rather than forcibly killing it.

This is not related to why thread pools exist. Thread pools are useful because there is usually some overhead associated with creating or destroying threads, due to the internal JVM or OS-level bookkeeping required to track the thread's state and progress. Thread pools make it possible to recycle threads and have them perform different tasks by having the threads sleep until a task is ready, then wake up and perform the task. This can be much faster than spawning off a new thread, performing the task yourself, then tearing down the thread.

Hope this helps!

Upvotes: 3

Related Questions