[Solved] java.lang.IllegalThreadStateException in Java with Examples

In this post, I will be sharing how to fix java.lang.IllegalThreadStateException in Java with examples. According to Oracle docs, it is thrown to indicate that a thread is not in an appropriate state for the requested operation.

Read Also: How to fix java.lang.IllegalStateException in Java

java.lang.IllegalThreadStateException is an unchecked exception since it extends RuntimeException class. Just like java.lang.IllegalMonitorStateException, the java.lang.IllegalThreadStateException was introduced in JDK (Java Development Kit) 1.0.

[Fixed] java.lang.IllegalThreadStateException in Java with Examples

As always, first, we will produce the java.lang.IllegalThreadStateException before moving on to the solution.

1. Producing the exception by calling the start() method when the thread is already executing the run() method


java.lang.IllegalThreadStateException can be produced by calling the start() method on a Thread that is already executing the run() method as shown below in the example.

public class MyThread extends Thread // Creating MyThread class that is extending the Thread class
{
    public void run() 
    {
	  for (int i = 0; i < 3 ; i++)
	  {	
		System.out.println(i);	
	  }
    }

    public static void main(String args[])
    {
        //In main() method, creating a thread object  
        MyThread th = new MyThread();

        //Stating the thread.
        th.start();
        
        System.out.println("Alive is Awesome");

        //Starting thread again, when it's already running, causing IllegalThreadStateException.
th.start();
//Won't be displayed because the main thread is halted by IllegalThreadStateException System.out.println("Main Thread Completed"); } }


Output:
Alive is Awesome
0
Exception in thread "main" 1
2
java.lang.IllegalThreadStateException
    at java.base/java.lang.Thread.start(Thread.java:789)
    at MyThread.main(MyThread.java:22)

Solution


In the above example, we can easily get rid of the java.lang.IllegalThreadStateException if we call the start() method on the thread only once instead of twice.

public class MyThread extends Thread // Creating MyThread class that is extending the Thread class
{
    public void run() 
    {
	  for (int i = 0; i < 3 ; i++)
	  {	
		System.out.println(i);	
	  }
    }

    public static void main(String args[])
    {
        //In main() method, creating a thread object  
        MyThread th = new MyThread();

        //Stating the thread.
        th.start();
        
        System.out.println("Alive is Awesome");
			
        System.out.println("Main Thread Completed");  
    }
}


Output:
Alive is Awesome
Main Thread Completed
0
1
2

2. Producing the exception by calling the start() method when the thread is finished executing the run() method


java.lang.IllegalThreadStateException can be produced by calling the start() method on a Thread that has already finished executing the run() method as shown below in the example.

public class MyThread extends Thread // Creating MyThread class that is extending the Thread class
{
    public void run() 
    {
	  for (int i = 0; i < 3 ; i++)
	  {	
		System.out.println(i);	
	  }
    }

    public static void main(String args[])
    {
        //In main() method, creating a thread object  
        MyThread th = new MyThread();

        //Stating the thread.
        th.start();
        
        try 
        {
            System.out.println("Main thread is going to sleep for 2 seconds");
            th.sleep(2000);
            System.out.println("Main thread is awakened");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }

        //Starting thread again, when it's already dead, causing IllegalThreadStateException.
th.start();
//Won't be displayed because the main thread is halted by IllegalThreadStateException System.out.println("Main Thread Completed"); } }


Output:
Main thread is going to sleep for 2 seconds
0
1
2
Main thread is awakened
Exception in thread "main" java.lang.IllegalThreadStateException
    at java.base/java.lang.Thread.start(Thread.java:789)
    at MyThread.main(MyThread.java:31)

Solution


In the above example, the java.lang.IllegalThreadStateException can be removed if we call the start() method on the thread only once as shown below in the example.

public class MyThread extends Thread // Creating MyThread class that is extending the Thread class
{
    public void run() 
    {
	  for (int i = 0; i < 3 ; i++)
	  {	
		System.out.println(i);	
	  }
    }

    public static void main(String args[])
    {
        //In main() method, creating a thread object  
        MyThread th = new MyThread();

        //Stating the thread.
        th.start();
        
        try 
        {
            System.out.println("Main thread is going to sleep for 2 seconds");
            th.sleep(2000);
            System.out.println("Main thread is awakened");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
			
        System.out.println("Main Thread Completed");  
    }
}


Output:
Main thread is going to sleep for 2 seconds
0
1
2
Main thread is awakened
Main Thread Completed


That's all for today. Please mention in the comments if you have any questions related to how to fix java.lang.IllegalThreadStateException in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry