Ad

Sunday 13 July 2014

java.lang.IllegalThreadStateException at java.lang.Thread.start

IllegalThreadStateException is thrown when you try to start the thread more than once on the same thread object instance.Start method can be invoked only when the Current thread state is "New".For other states like Runnable,Running  or waiting etc invoking start method will throw the IllegalThreadStateException.

Below sample will show the typical scenario where the IllegalThreadStateException will be thrown:

public class IllegalThreadStateSample {                                                                                                    

public static void main(String[] args) {
        ProcessThread processThread= new ProcessThread();
        Thread thread= new Thread(processThread,"ProcessThread");
        thread.start();//ProcessThread State will become "Runnable" and then "Running"
        thread.start();//Starting the ProcessThread for the second time
}

}

class ProcessThread implements  Runnable {

public void run() {
System.out.println("Thread started::"+Thread.currentThread().getName());
}
}

When we run this program,IllegalThreadStateException  will be thrown as shown below:

Thread started::ProcessThread                                                                                                                  
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at test.IllegalThreadStateSample.main(IllegalThreadStateSample.java:9)

  • When we create the thread object(i.e new Thread(processThread,"ProcessThread")),the State of the ProcessThread will be become "New".
  • Invoking the start method will make the state of the Processthread to "Runnable" or "Running" and the ProcessThread started its execution.
  • Since Start method is invoked on the same thread object for the second time whose state is not "New",IllegalThreadStateException is thrown

Friday 11 July 2014

Joining Threads using join method in java

By using join method in Thread, we can make one thread to wait until the other thread completes its execution.There are two overloaded methods of join:

t.join()
    - The current executing thread which invokes the above join method,will make it to wait until thread "t" completes its execution.In other words the current thread will resumes its execution only after thread "t" completes.

t.join(10000)
    - The current executing thread which invokes the above join method,will make it to wait  until thread "t" completes its execution.But one exception to this  is, the current thread can wait only up to a maximum of 10 seconds i.e if thread "t" doesn't complete in 10 seconds,current thread will resumes its execution even though thread "t" has not completed.

The below sample program will explain this behavior


public class ThreadJoinSample {                                                                                                                

public static void main(String[] args) {
System.out.println("Thread started::"+Thread.currentThread().getName());
ChildThread childThread= new ChildThread();
Thread thread=new Thread(childThread, "ChildThread");
thread.start();
try {
thread.join();
//thread.join(10000);
          System.out.println("Thread Resumed::"+Thread.currentThread().getName());
}
   catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread completed::"+Thread.currentThread().getName());
}

}

class ChildThread implements  Runnable {
public void run() {
System.out.println("Thread started::"+Thread.currentThread().getName());
try {
Thread.sleep(20000);//Sleep for 20 seconds
}
    catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread completed::"+Thread.currentThread().getName());
}

}

The Main thread(ie main() method) creates and starts a new ChildThread and invokes  the join method on the Childthread.As a result the main thread will wait until the childThread completes its execution.When we run the above program,we will get the output as shown below:

Thread started::main
Thread started::ChildThread                                                                                                                      
Thread completed::ChildThread
Thread Resumed::main
Thread completed::main

As we can see in the output,the Main thread has resumes its execution only after the Child Thread has completed.

Now use the other overloaded join method in the above program i.e replace join() with join(10000).
Now the output will be:

Thread started::main
Thread started::ChildThread                                                                                                                      
Thread Resumed::main
Thread completed::main
Thread completed::ChildThread

Since the Main Thread can wait only maximum of 10 seconds and the ChildThread will take 20 seconds to completes its execution,the main thread has resumed its execution even though the ChildThread has not completed.