Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

How to restart a thread in Java

Thread view: 
vanisathish@gmail.com - 27 Oct 2006 15:53 GMT
Hi,

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

Thanks,
Bent C Dalager - 27 Oct 2006 15:58 GMT
>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
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Christopher Benson-Manica - 27 Oct 2006 19:16 GMT
> 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.

Signature

C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.

Brandon McCombs - 28 Oct 2006 00:32 GMT
>> I need to restart a Java Thread in my application. what would be the
>> safe way of doing this.
[quoted text clipped - 3 lines]
> "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?

> 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
[quoted text clipped - 4 lines]
> come nicely wrapped with love from Sun - you'll have to write
> something to suit your needs yourself.
Karl Uppiano - 28 Oct 2006 03:44 GMT
> 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.
Mike Schilling - 28 Oct 2006 18:48 GMT
>>> I need to restart a Java Thread in my application. what would be the
>>> safe way of doing this.
[quoted text clipped - 6 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.