< Prev
Next >





IllegalThreadStateException in Thread






IllegalThreadStateException comes in two situations -:







Calling start() on the thread when it's already executing run() method


In this code, we are trying to create IllegalThreadStateException by calling start() method on the thread object, which is already executing its run() method
class ThRun implements Runnable		    //implementing the Runnable interface
{


public void run()  //Entry point of new thread
{
	for(int i=0;i<5;i++)
	{	
		System.out.println(i);	
	}
}


public static void main(String... ar)
{
ThRun newTh= new ThRun();
Thread th= new Thread(newTh, "Thread2");    //Calling Thread's constructor & passing the object
					    //of class that  implemented  Runnable interface
					    //& the name of new thread.

//Stating the thread(Thread2).
th.start();

//Starting thread(Thread2) again, when it's already running, causing IllegalStateException.
th.start();	
			
//This won't be printed because the main thread is halted by IllegalStateException
System.out.println("Main Thread"); 
}
}


Output is - :


0Exception in thread "main"
1
2
3
4java.lang.IllegalThreadStateException

        at java.lang.Thread.start(Unknown Source)
        at ThRun.main(Thread8.java:21)


Program Analysis









Calling start() on thread when it has finished executing run() method


In this code, we are trying to create IllegalThreadStateException by calling start() method on the thread object, which has already finished executing its run() method and its in a dead state.
class ThRun implements Runnable
{


public void run()    //Entry point of new thread
{

	for(int i=0;i<5;i++)
	{	
		System.out.println(i);	
	}
}


public static void main(String... ar)
{
ThRun newTh= new ThRun();
Thread th= new Thread(newTh,"Thread2");   //Calling Thread's constructor & passing the object
					    //of class that  implemented  Runnable interface
					    //& the name of new thread.

//Stating the thread(Thread2).
th.start();
	
try
{
	System.out.println("Main Thread is going to sleep");
	Thread.sleep(3000); //Making main thread sleep for 3000ms / 3 seconds
	System.out.println("Main thread is awakened");
}
catch(Exception e)
{
	System.out.println(e);
}

//Calling start() method on a dead thread, causing IllegalStateException
th.start();

//This won't be printed because the main thread is halted by IllegalStateException
System.out.println("Main Thread"); 
}
}

Output is - :

Main Thread is going to sleep
0
1
2
3
4
Main thread is awakened
Exception in thread "main" java.lang.IllegalThreadStateException
        at java.lang.Thread.start(Unknown Source)
        at ThRun.main(Thread8.java:32)


Program Analysis




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< join() in Thread
Interrupt a running thread >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2