Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Thread problem when using start() more than once. It does nothing

Status
Not open for further replies.

zappsweden

Programmer
Aug 29, 2001
16
SE
I have a problem when i make a class that extends Thread because the first time i use start() it works but the second time it does nothing. My run() ends with an activation of a menu and from that menu i should be able to start the thread again but i can't.

Note: The second start() call is done after the first one is finished because I get no exception trying to start it when it is already alive. BTW, i also checked if (this.isAlive()==false) before the second start() call and it was.
 
(this.alive()==false) was correct i.e the thread was NOT alive.
 
Im not entirely sure, but I was under the impression that once a thread is finished, its finished. I think you would need to start another thread that does the same thing.
 
Which method did you use to stop your Thread? You should put it to sleep and when you need to wake it up, interrupt it. This should be the way it should work.

Leon
 
So a Thread is only ment to start() once?
Here is how I tried used it in my formula one game:

run()
{
blablabla;
blabla;
blablablabla;
"showSomeMenu"
}

so the last thing that the Thread does is starting a menu.
from that menu I want to be able to start it again because the class that extends Thread is called RaceWeekend and it wants to run several times because the game is a formula one game with(qualifying, practice, race). Every start() is in my case one session of the weekend in the game.
 
once a thread is in a dead state i.e. it has finished executing, it can not be restarted. you have to create a new instance on your class and start again.

you have 2 choices here.
1, you can reuse the current thread by placing it in a waiting state by calling wait(). This thread will stop executing until you need it again
2, this.isAlive()==false then create a new instance of the object and start again. this isn't the best for performance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top