Class IllegalThreadStateException in Java
IllegalThreadStateException is thrown from code to indicate that the thread is not in an appropriate state for the required or requested operation. The call to sleep() method is enclosed with a try block followed by a catch block. The sleep() method may throw an exception, which needs to be caught and handle.
Java throws IllegalThreadStateException whenever we attempt to invoke a method that a thread can not handle in the given state.
Example-
A sleeping thread can not deal with the resume() method as a sleeping thread can not receive any messages or instructions. It is even true with the suspend() method when it is used on a blocked(Not runnable) thread.
or The thread maybe already started and running when we want to start the same thread.
The structure of the class IllegalThreadStateException is as follows:
public class java.lang.IllegalThreadStateException extends java.lang.IllegalArgumentException{
//constructor
public IllegalThreadStateException();
//constructs an empty IllegalThreadStateException object that is an object with no message specified.
public IllegalThreadStateException(String message);
//constructs an IllegalThreadStateException object with the message specified.
}
The class IllegalThreadStateException also inherits methods from class Object and Throwable.
From Object class | From Throwable class |
---|---|
clone() | fillInStackTrace() |
getClass() | getMessage() |
notify() | printStackTrace() |
wait() | printStackTrace(PrintWriter) |
wait(long, int) | getLocalizedMessage() |
wait(long) | printStackTrace(PrintStream) |
equals(Object) | |
toString() | |
finalize() | |
hashCode() | |
notifyAll() |