How to restart a thread in Java

V

vanisathish

Hi,

I need to restart a Java Thread in my application. what would be the
safe way of doing this.


Thanks,
 
B

Bent C Dalager

Hi,

I need to restart a Java Thread in my application. what would be the
safe way of doing this.

If a Thread object's run() method has exited (returned), then that
Thread cannot be restarted. I believe this is made explicit in the
Javadoc for the class.

What you may want to do is not actually exit from run() but pause it
and then restart it at some later point. If so, you probably have to
write that logic yourself.

Cheers
Bent D
 
C

Christopher Benson-Manica

I need to restart a Java Thread in my application. what would be the
safe way of doing this.

You can't restart a thread. From the javadoc:

"It is never legal to start a thread more than once. In particular, a
thread may not be restarted once it has completed execution."

You have lots of choices to get the effect you want. You can
create new threads to pick up where the dead threads leave off; better
would be to use one of the java.util.concurrent.* classes from 1.5 if
possible to provide a thread pool. There are other goodies in
java.util.concurrent that you may find helpful as well. If 1.5 isn't
an option for you, you still have a lot of flexibility, it just won't
come nicely wrapped with love from Sun - you'll have to write
something to suit your needs yourself.
 
B

Brandon McCombs

Christopher said:
You can't restart a thread. From the javadoc:

"It is never legal to start a thread more than once. In particular, a
thread may not be restarted once it has completed execution."

Does legal mean the code won't compile or that it just isn't good
practice? I ask because I have a class that contains a run() for a
thread and I call it everytime I need to do the operation that the run()
performs and I do so w/o creating a new instant of the class that the
run() is a part of. So far I haven't seen any problems with that
approach. Is that not the same thing as what the original poster was
trying to do?
 
K

Karl Uppiano

Does legal mean the code won't compile or that it just isn't good
practice? I ask because I have a class that contains a run() for a thread
and I call it everytime I need to do the operation that the run() performs
and I do so w/o creating a new instant of the class that the run() is a
part of. So far I haven't seen any problems with that approach. Is that
not the same thing as what the original poster was trying to do?

Legal in the sense that it won't work. If you create a new thread and start
it, when it finishes running whatever top level run() method you give it,
and that run() method returns, that thread is finished and it will not
restart, no matter what you do. You will have to create a new one. You can
create new threads all day long, and give them the same old run() method the
same old instance, that works just fine. But the thread dies when run()
returns, and it cannot be revived.
 
M

Mike Schilling

Brandon McCombs said:
Does legal mean the code won't compile or that it just isn't good
practice?

That it won't work.
I ask because I have a class that contains a run() for a thread and I call
it everytime I need to do the operation that the run() performs and I do
so w/o creating a new instant of the class that the run() is a part of.

But you're not creating a thread when you do that; you're just calling a
method. If you called "start()" repeatedly on a Thread (or subclass of
Thread), then you'd see it fail after the first time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top