How to kill the thread? ..... How to restart them again in multi threading?
-
There is Thread.stop(): http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#stop() , but it is not recommended that you do that: http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html – Travis Gockel Mar 22 '10 at 03:42
-
1The actual page that explains the dangers of thread primitives is http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html – Will Mar 22 '10 at 03:45
-
@chetans, you should rewrite this so it's more specific and makes more sense. – avpx Mar 22 '10 at 03:46
-
2Best way to kill a thread is by drowning. Threads can't swim. – Anthony Forloney Mar 22 '10 at 04:51
-
What do you mean by 'restart' a thread? – Adisesha Mar 22 '10 at 09:31
7 Answers
Since your post is tagged "Java," I have a good idea of what you are saying. Let's say you start a thread by doing:
Thread foo = new Thread(someRunnable);
foo.start();
Now that destroy
and friends are deprecated, you need a way to kill the thread. Luckily for you, there has always been the concept of "interrupts." Simply change your runnable so that, on interrupt, it exits. Then call the thread's interrupt
method.
foo.interrupt();
If you wrote your Runnable to handle this correctly, it will stop whatever it is doing and terminate.

- 1,892
- 15
- 13
Thread.stop()
kills a thread, but you definitely don't want to do this (see the API documentation for an explanation why). Thread.interrupt()
sends an asynchronous notification to a thread, so that it can shut itself gracefully.
For a comprehensive text on Java multithreading, I recommend B. Goetz, Java Concurrency in Practice, Addison-Wesley Professional.

- 5,429
- 4
- 22
- 31
-
5Thread.stop() is kind of like putting a car traveling at 60mph in park to stop it...definitely stops it, but you're going to leave a sad, sad trail of twisted metal along the way and your car won't ever quite be the same ;-) – Chris Thompson Mar 22 '10 at 04:07
-
also, Thread.interrupt wont always interrupt a thread waiting on IO. if youre serious about designing for interruption you need a way to forcibly close any IO streams your target thread might be waiting on – radai Mar 22 '10 at 04:50
The preferred way for a Thread
to die is for the execution of the run
method to go to completion:
Thread t = new Thread(new Runnable() {
public void run() {
// Do something...
// Thread will end gracefully here.
}
}
Once a thread gracefully dies in the example above, the Thread
cannot be restarted. (Trying to call Thread.start
on a thread that has already been started will cause an IllegalThreadStateException
.)
In that case, one can make another instance of the thread and call start
on that.
Probably a good place to get more information on threading would be Lesson: Concurrency from The Java Tutorials.

- 159,216
- 35
- 211
- 226
i wrap my worker threads up in their own class and use a terminated property to kill the thread proc loop.
sorry i dont have a java version to hand right now but you should get the idea from this http://pastie.org/880516
using System.Threading;
namespace LoaderDemo
{
class ParserThread
{
private bool m_Terminated;
private AutoResetEvent m_Signal;
private string m_FilePath;
...
public ParserThread(AutoResetEvent signal, string filePath)
{
m_Signal = signal;
m_FilePath = filePath;
Thread thrd = new Thread(this.ThreadProc);
thrd.Start();
}
public bool Terminated {
set { m_Terminated = value; }
}
private Guid Parse(ref string s)
{
//parse the string s and return a populated Guid object
Guid g = new Guid();
// do stuff...
return g;
}
private void ThreadProc()
{
TextReader tr = null;
string line = null;
int lines = 0;
try
{
tr = new StreamReader(m_FilePath);
while ((line = tr.ReadLine()) != null)
{
if (m_Terminated) break;
Guid g = Parse(ref line);
m_GuidList.Add(g);
lines++;
}
m_Signal.Set(); //signal done
}
finally
{
tr.Close();
}
}
}
}

- 16,223
- 5
- 43
- 42
The best way to kill a thread is to set up a flag for the thread to watch. Program the thread to exit when it sees the flag is set to true. There's no way to restart a killed thread.

- 57,498
- 14
- 111
- 168
-
2The best way to kill a thread is to call Thread.interrupt() and have the thread also monitor its interrupt status periodically. Adding yet another boolean is entirely redundant. – user207421 Mar 22 '10 at 08:02
-
In addition to what EJP said, the hand-rolled flag doesn't play well with executors. – Nathan Hughes Jun 07 '16 at 16:44
If you want to start, stop, restart threads at will, maybe using the Java 5 concurrency package would be a good idea. You can have an Executor that will do a bit of work, and when you need that bit of work to be done again, you can just re-schedule it to be done in the executor.

- 1,527
- 4
- 19
- 36
Regarding your first query on killing thread:
You can find more details about topic in below SE questions:
How to properly stop the Thread in Java?
How can I kill a thread? without using stop();
How to start/stop/restart a thread in Java?
Regarding your second query of re-starting thread, it's not possible in java.
You can find below details in documentation page
public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
Instead of plain Threads, you can use advanced concurrent API for thread life cycle management. Have a look at this post for ExecutorService details :

- 1
- 1

- 37,698
- 11
- 250
- 211