Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

IllegalThread State  RSS feed

 
R K Parulekar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I'm unable to start -stop - start the thread
it gives below exception.

however first time start and stop works

What need to do?

My java code is as below

public class ThreadTest extends Thread{
boolean runNow =true;
boolean isInterrupted = false;
int count = 0;
public void run(){
while (runNow) {
if (isInterrupted()){
runNow=false;
break;
}
System.out.println(count++);
}
}
public void stopThread(boolean graceful) {
if (graceful) {
runNow=false;
}else{
interrupt();

}
}
public static void main(String[] args){
ThreadTest myThread = new ThreadTest();
myThread.start();
myThread.stopThread(false);
myThread.start();
}
}


Exception
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at ThreadTest.main(ThreadTest.java:27)
 
Henry Wong
author
Sheriff
Posts: 22835
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A thread object may only be used (started) once.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!