• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IllegalThread State

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A thread object may only be used (started) once.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic