*
The moose likes Threads and Synchronization and the fly likes start thread again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "start thread again" Watch "start thread again" New topic
Author

start thread again

Abhishek Gayakwad
Greenhorn

Joined: Aug 25, 2006
Posts: 4

Why we can not start a dead thread again.
what kind of implementaion it has ???
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

I don't believe there's a sound technical reason why they couldn't have made it possible to start a Thread again; it's just a simple model that works well in practice. The Thread class is implemented mostly in native methods; on modern JVMs these are implemented by calling out to the native platform's thread library.


[Jess in Action][AskingGoodQuestions]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Baically once the thread is completed its tenure of the run(), you cannot agian start the same thread.You will encounter an exception.


Rahul Bhattacharjee
LinkedIn - Blog
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You cannot restart a Thread. However, you can use a single Runnable in many Threads. That's the way to achieve something close to what you want.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Juggy Obhi
Ranch Hand

Joined: Jul 02, 2007
Posts: 51
I firmly believe that after a successful execution of run() the Thread instance comes back exactly where it started from...so i don't see any good reason why start() can't be called again.

and Thread class being implemented over native method is not a very good reason for denying 2nd call to start().

Please clarify what could be the reason.


It does not matter how many times you fall,what matters is how many times you stand back.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Juggy, your "firm belief" does not reflect reality. Yes, Java's designers probably could have designed Threads this way. But they didn't. If you call start() a second time, you will get an IllegalThreadStateException. A dead thread does not have the same state as a new thread.

Now I'm just guessing, but I think the reason for this might be that they wanted to make it easier for the thread scheduler to immediately free up some of the thread resources as soon as a thread terminates. I'm not talking about the Thread object, which will exist on the heap for as long as there's a reachable reference in memory somewhere. But there should be additional resources which are accessible only in native code, and for efficient thread scheduling these should probably be freed as soon as possible, without necessarily waiting for garbage collection.


"I'm not back." - Bill Harding, Twister
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Juggy obhi:
I firmly believe that after a successful execution of run() the Thread instance comes back exactly where it started from...so i don't see any good reason why start() can't be called again.


I believe that there is rarely much to be gained by worrying about why Java does or doesn't have some or other feature. Occasionally, it will be enlightening, but in this case, and many others, it isn't.

They could have done it how you suggest, but they didn't. It's still possible to implement re-runnable tasks, using Runnable, so no important tools are being denied you.

Move on please, nothing to see here...
Juggy Obhi
Ranch Hand

Joined: Jul 02, 2007
Posts: 51
Jim,Peter,Thanks for clarifying my doubt...You are right that Dead Thread(on which you can't call start())is not exactly New Thread(on which you can call start()).

And your guess is respectable as well.

It's was a bit strange for me to encounter with an Object that allows you to call its member function for single time only.
[ July 23, 2007: Message edited by: Juggy obhi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: start thread again