8

From another post:

If a Thread needs to be run more than once, then one should make an new instance of the Thread and call start on it.

How is this done?

Community
  • 1
  • 1
Airflow46
  • 152
  • 1
  • 1
  • 7
  • 2
    Check this out: http://docs.oracle.com/javase/tutorial/essential/concurrency/ – pablochan Nov 10 '13 at 11:03
  • How do you create an instance of a thread and run it the first time? – NilsH Nov 10 '13 at 11:04
  • 2
    http://arashmd.blogspot.com/2013/06/java-threading.html –  Nov 10 '13 at 11:04
  • Martin would say 'if you want a thread to run twice, put a for loop around all the existing code in the thread function'. – Martin James Nov 10 '13 at 11:06
  • 1
    each thread just run its `Runnable`, and doesn't care if it's as same as other, because each thread has its private stack. for example check [this example](http://arashmd.blogspot.com/2013/07/java-thread-example.html#rccig) –  Nov 10 '13 at 11:23
  • check this answer [link](http://stackoverflow.com/questions/1215548/is-it-legal-to-call-the-start-method-twice-on-the-same-thread) – DnA Dec 14 '16 at 15:54

3 Answers3

6

I would use another layer of abstraction. Use an ExecutorService.

Here is a simple example:

public static void main(String args[]) throws InterruptedException {
    final ExecutorService service = Executors.newCachedThreadPool();
    final class MyTask implements Runnable {

        @Override
        public void run() {
            System.out.println("Running my task.");
        }
    };
    for (int i = 0; i < 10; ++i) {
        service.submit(new MyTask());
    }
    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);
}

Just dump your task into the service as many times as you want.

The ExecutorService is a thread pool - it has a number of Threads that take tasks as they come. This removes the overhead of spawning new Threads because it caches them.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • 2
    Although this answer seems to be a little bit off-topic to me, I appreciate every statement that explains the ExecutorService and favours it over usual "Threading" as still there are much too few people knowing the new fancy concurrency API. +1! – isnot2bad Nov 10 '13 at 11:17
3

Basically, a thread cannot be restarted.

So if you want a reusable "thread", you are really talking about a Runnable. You might do something like this:

  Runnable myTask = new Runnable() {
      public void run() {
          // Do some task
      }
  }

  Thread t1 = new Thread(myTask);
  t1.start();
  t1.join();
  Thread t2 = new Thread(myTask);
  t2.start();

(This is purely for illustration purposes only! It is much better to run your "runnables" using a more sophisticated mechanism, such as provided by one of the ExecutorService classes, which is going to manage the actual threads in a way that avoids them terminating.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

A java Thread cannot be run twice. Once it has been started and finished its work, it cannot be started again (calling method start will fail). So you'll have to create a new instance of Thread (using the same Runnable) and start it.

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
  • 1
    Did you even read the description or did you only read the title ? I said i didn't understand what this ment ... – Airflow46 Nov 10 '13 at 11:14
  • 1
    What did you not understand? I explained that you have to create a new instance of Thread and start it. Its not more nor less than that. Do you really want me to write down this two lines of code? – isnot2bad Nov 10 '13 at 11:23