
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - IllegalThreadStateException
The Java IllegalThreadStateException is thrown when a thread is not in an appropriate state for the requested operation. The IllegalThreadStateException is a runtime exception that is thrown when a thread is not in an appropriate state for the requested operation.
Following are the reason when JVM throws an IllegalThreadStateException in Java:
- When a thread is not in an appropriate state for the requested operation, JVM throws an IllegalThreadStateException.
- When a thread is not in a new state and you try to start it, JVM throws an IllegalThreadStateException.
Constructors of IllegalThreadStateException
There are two constructors of IllegalThreadStateException class:
- IllegalThreadStateException(): This constructor is used to create an IllegalThreadStateException object without any message.
- IllegalThreadStateException(String message): This constructor is used to create an IllegalThreadStateException object with a message.
Methods of IllegalThreadStateException
There are some methods of IllegalThreadStateException class:
Method | Description |
---|---|
getMessage() | It is used to return the message of the exception. |
toString() | It is used to return the detail message string of the exception. |
printStackTrace() | It is used to print the stack trace of the exception. |
Example of IllegalThreadStateException
In this example, we are trying to start a thread that is already started, so JVM will throw an IllegalThreadStateException.
public class IllegalThreadStateExceptionExample { public static void main(String[] args) { Thread thread = new Thread(); thread.start(); // First start - Works fine thread.start(); // Second start - Throws IllegalThreadStateException } }
Output
Following is the output of the above code:
Exception in thread "main" java.lang.IllegalThreadStateException at java.base/java.lang.Thread.start(Thread.java:1525) at IllegalThreadStateExceptionExample.main(IllegalThreadStateExceptionExample.java:7)
As you can see in the output, JVM throws an IllegalThreadStateException because we are trying to start a thread that is already started.
Handling IllegalThreadStateException
In this example, we are trying to start a thread that is already started, so JVM will throw an IllegalThreadStateException. We are handling this exception using try-catch block.
public class IllegalThreadStateExceptionExample { public static void main(String[] args) { Thread thread = new Thread(); try { thread.start(); // First start - Works fine thread.start(); // Second start - Throws IllegalThreadStateException } catch (IllegalThreadStateException e) { System.out.println("IllegalThreadStateException: A thread cannot be restarted once started!"); } } }
Output
Following is the output of the above code:
IllegalThreadStateException: A thread cannot be restarted once started!
How to avoid IllegalThreadStateException?
There are some ways to avoid IllegalThreadStateException in Java:
- Before starting a thread, check if it is already started or not.
- Always check the state of the thread before starting it.
By following the above ways, you can avoid the IllegalThreadStateException in Java.
Let's see an example to avoid IllegalThreadStateException:
public class IllegalThreadStateExceptionExample { public static void main(String[] args) { Thread thread = new Thread(); if(!thread.isAlive()) { thread.start(); } } }
Output
Following is the output of the above code:
As you can see in the output, we are checking the state of the thread before starting it, so JVM does not throw an IllegalThreadStateException.