3
public class GetCurrentPrice implements Runnable{
   public void run(){
      // some business logic
   }
}



public class Main{
   public static void main(){
       GetCurrentPrice gcp = new GetCurrentPrice();
       Thread t = new Thread(gcp);
       while(true){
           t.start();
           //once this thread execution is complete, restart this thread.
        }
   }
}

This throws java.lang.IllegalThreadStateException. What i am trying to achieve is, run this thread once, wait for it to complete, once it is complete, run this thread again.

  • similar question here: http://stackoverflow.com/questions/1215548/is-it-legal-to-call-the-start-method-twice-on-the-same-thread – CSCH Apr 10 '15 at 14:35
  • Haven't you found the API to check it? No problem, here it is: https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start%28%29. Btw 4 upvotes? I absolutely don't these users here. – Tom Apr 10 '15 at 14:57

7 Answers7

5

No you cant do that. From Thread.start() API: It is never legal to start a thread more than once.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
4

From javadoc of class java.lang.Thread

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

See https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
4

While you can't re-run a Thread directly, you can use a ScheduledExecutorService to execute a Thread (or anything else that implements Runnable) periodically.

From the API:

The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate and scheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled.

You can just create a pool with one thread, hand it your Runnable and ask it to re-run it each time it finishes, by doing something like this:

public class GetCurrentPrice implements Runnable {
    public void run() {
        // some business logic
    }
}
public class Main {
    public static void main() {
        GetCurrentPrice gcp = new GetCurrentPrice();
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        // each time thread execution completes, start a new one without delay
        scheduler.scheduleAtFixedRate(gcp, 0, 0, TimeUnit.SECONDS);
    }
}
azurefrog
  • 10,785
  • 7
  • 42
  • 56
2

Threads in Java are not reusable.

It is not legal to try to restart the thread in Java.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
2

Yes, the same Thread cannot be started more than once, but the same Runnable instance can be passed to different Thread objects.

More here -> Initializing two threads with the same instance of a runnable

Community
  • 1
  • 1
0

No you can't start Thread more than once, You need to initialize new Runnable every time you want to start same thread.

Hunain
  • 385
  • 5
  • 15
0

Here's one way to repeat the business logic:

public class GetCurrentPrice implements Runnable {
    public void run() {
        businessLogic();
        businessLogic();
    }

    public void businessLogic() {
        // some business logic
    }
}

public class Main {
    public static void main() {
        GetCurrentPrice gcp = new GetCurrentPrice();
        new Thread(gcp).start();
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111