Recreating Thread Object After Termination
Recreating Thread After Termination
I have created a thread. It can be started and stopped but can't start it again because the thread has been terminated. How can i create the object again so the state will be "NEW".
Declaring object:
Code:
public MainBot mainBotObject = new MainBot();
Start Thread:
Code:
if(mainBotObject.isInterrupted()) {
//need to renew the object here
}
System.out.println(mainBotObject.getState());//First Run: New,Second Run: Terminated <<state of thread
mainBotObject.start();
System.out.println(mainBotObject.getState());//First Run: Runnable <<state of thread
Stop Thread:
Code:
System.out.println(mainBotObject.getState());//First Run: Runnable <<state of thread
mainBotObject.interrupt();
System.out.println(mainBotObject.getState());//First Run: Runnable <<state of thread
Re: Recreating Thread Object After Termination
Quote:
How can i create the object again
To create a Thread object, use the new statement.
Re: Recreating Thread Object After Termination
How can i do this? I need to recreate the object with another statement:
Code:
public MainBot mainBotObject = new MainBot();
If so how should I go by calling this statement? I tried putting it into a public void then calling when the object is terminated, but for some reason public objects can't be declared in a void. How can i go about doing this?
If not what is the best approach?
Thanks for a reply.
Re: Recreating Thread Object After Termination
Quote:
how should I go by calling this statement?
The same way you did it the first time round.
Quote:
I tried putting it into a public void then calling when the object is terminated, but for some reason public objects can't be declared in a void
Trail: Learning the Java Language (The Java™ Tutorials)
db