Basically, a thread cannot be restarted.
So if you want a reusable "thread", you are really talking about a Runnable
. You might do something like this:
Runnable myTask = new Runnable() {
public void run() {
// Do some task
}
}
Thread t1 = new Thread(myTask);
t1.start();
t1.join();
Thread t2 = new Thread(myTask);
t2.start();
(This is purely for illustration purposes only! It is much better to run your "runnables" using a more sophisticated mechanism, such as provided by one of the ExecutorService
classes, which is going to manage the actual threads in a way that avoids them terminating.)