IllegalThreadStateException


This is an unchecked exception and as the name indicates, is thrown by the Java runtime environment, when the programmer is trying to modify the state of the thread when it is illegal. It is also of commonsense, there is no meaning of starting a thread which is already started. If the programmer tries to do so, the JVM throws IllegalThreadStateException.

Following is the hierarchy.

Object –> Throwable –> Exception –> RuntimeException –> IllegalThreadStateException

Complete hierarchy of exceptions is available at "Hierarchy of Exceptions – Checked/Unchecked Exceptions".

Following program illustrates.

public class Demo extends Thread
{
   public static void main(String args[])
   {
       Demo d1 = new Demo();
       d1.start();
       d1.start();
   }
}

IllegalThreadStateException

Observe, in the above program, d1 is a thread (how?) and when created, it is in born state (inactive state). To make the thread d1 active, the start() method is called on it.

d1.start();

The start() method of Thread class makes d1 thread starting and when started it will be executing the task assigned. While the task is going on, again activating the same thread with start() method is of no meaning. At this juncture, the JVM throws IllegalThreadStateException. Observe the screenshot.

Pass comments and suggestions to improve the quality of this tutorial "IllegalThreadStateException".

Leave a Comment

Your email address will not be published.