• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

start thread again

 
Abhishek Gayakwad
Greenhorn
Posts: 4
Hibernate IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we can not start a dead thread again.
what kind of implementaion it has ???
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baically once the thread is completed its tenure of the run(), you cannot agian start the same thread.You will encounter an exception.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Juggy Obhi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic