Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding start()

 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,all
here is my problem, i expect 2 lines of "ok" on the screen,while i could only get 1, why? it seems the 2th start() doesnt work at all!


[This message has been edited by James Du (edited March 25, 2001).]
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could be off on this but i dont think you can start a thread more than once. The first time you call start() it calls the run method. Then the thread dies when its job is done. If you wanted to re-use threads, you could use a run queue class to have the threads interact with the run queue and get new assignments(work to do).
What you could have done is create 2 threads to get the desired output instead.
ThreadTest t = new ThreadTest();
ThreadTest t2 = new ThreadTest();
and have the second thread start after the try/catch block. Hope that helped a bit.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. A dead thread can not be restarted.
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Andy and Thomas.thanks for your replies.
but i still dont understand why a died thread can not restart,what mechanism behind that and are there any java specifications regarding that?
when i called the second start(), no Exception appeared,then what did JVM do as for the thread?
and all dead threads are ready for Garbage Collection?

[This message has been edited by James Du (edited March 26, 2001).]
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure no exception is thrown? The documentation mentions that IllegalThreadStateException will be thrown if the thread was already started. And I have myself tested and found that this exception is thrown if a second start() is called, on the same thread object.
Although a dead thread cannot be restarted, there are deprecated suspend() and resume() methods which could suspend and resume the running of the thread, and this suspend() and resume() functionality can now be implemented through interrupts, and wait-notify regime. A thread can be made to run forever without dying, by enclosing its run() method code within a while(true) block, and using wait-notify protocol to suspend the running when it is not required.
So the fact that a dead thread cannot be restarted should not be seen as a limitation or drawback of the language. But as to exactly why this design was chosen by the inventors of Java Language, I hope some Guru enlightens us. Intuitively I do feel that it is a good design, by cannot pinpoint why it is so.
An object becomes ready for Garbage Collection when it is no longer referenced anywhere in the program. A dead thread continues to be an object, like any other object. Such a thread object would be ready for Garbage Collection when it is no longer referenced from anywhere in the program- such as where variables referencing the thread object go out of scope, or where the variables are explicitly set to null.
But a peculiar situation is where an alive and started thread object is no longer referenced from anywhere in the program. eg. make the thread t run forever by enclosing its run() method code within a while(true) loop. Now after doing t.start(); do t=null; so that t is no longer referenced. Enigmatically the thread keeps running and is not Garbage Collected. I hope some Guru helps us here.
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ,Rahul
As for the IllegalStateException,I think it was caused by the second start() which was called before the end of the thread died.Yes, under this circumstance,IllegalStateException was really thrown,but for avoiding this, I write a sleep() statement to give enough time for the thread to die before the following start() be called.
So,the premise of the problem is that the start() statement was called as for a DEAD thread.if Java doesnt allow that,it should throw a "DeadThreadException" or like (such as NullPointerException)or it just set the reference of the thread to null.
Note, the supposed "DeadThreadException" here are different with the "IllegalStateException". the first one is caused by manipulating a thread which was DEAD, while the other one is caused by intending to start a thead which is still ALIVE.


------------------
 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James

but for avoiding this, I write a sleep() statement to give enough time for the thread to die before the following start() be called.

if you try to start a thread that is dead then the start method is invoked in the current thread and not in a new Thread as given by the output below

Output is
in Start main
ok in runThread-0
in Start main
Note that the second start printed that it is executing from the main thread and not any new thread. Note also that the run method was not invoked from the start method when the start method was invoked the second time.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes James, I agree with you. Yes an exception should be thrown in the second start() even if the thread has died, but actually it is not. I think this exception should also be IllegalStateException, because being "dead" is also a state of thread. Hope somebody enlightens as to why the language is so designed that a second start() raises exception only when the thread is alive, but not when it has died.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic