Friday, 26 June 2015

Can we start the same thread twice in Java

What if we start the same thread again is one question you will be confronted with in many Java Multi-threading interview questions. This post tries to answer this question whether you can start the same thread twice or not.

According to Java Docs - http://docs.oracle.com/javase/8/docs/api/ description of start method in the Thread class says -

  • public void start() - Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
    The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
    It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
    Throws: IllegalThreadStateException - if the thread was already started.

Thus a Thread can only be started once and any attempt to start the same thread twice will throw IllegalThreadStateException.

In that case if we want to run the same thread again then we should make a new instance of the Thread and call start on it.

Example code of starting the same thread twice

public class ThreadTwiceStart implements Runnable {
    public static void main(String[] args) {
        // creating a thread 
        Thread t = new Thread(new ThreadTwiceStart(), "MyThread");
        t.start();
        // starting the same thread again
        t.start();
    }

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

Output

Thread started running MyThread
Exception in thread "main" java.lang.IllegalThreadStateException
 at java.lang.Thread.start(Unknown Source)
 at org.netjs.example.ThreadTwiceStart.main(ThreadTwiceStart.java:9)

It can be seen how it has thrown IllegalThreadStateException if an attempt is made to start the same thread again.

If we have to start the same thread again we have to create a new thread object. This code will run fine -

public class ThreadTwiceStart implements Runnable {
    public static void main(String[] args) {
        // creating a thread 
        Thread t = new Thread(new ThreadTwiceStart(), "MyThread");
        t.start();
        t = new Thread(new ThreadTwiceStart(), "MyThread");        
        t.start();
    }

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

Here a new thread object is created before calling the start method again thus this code will run without throwing any exception

That's all for this topic Can we start a thread twice in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Difference between Thread and Process in Java
  2. How to create thread in Java
  3. Thread states in Java multithreading
  4. What if run() method called directly instead of start() method
  5. Synchronization in Java multithreading
  6. Java Multi-threading interview questions

You may also like -

No comments:

Post a Comment