Java- reusing a thread with the same run but different parameters Java- reusing a thread with the same run but different parameters multithreading multithreading

Java- reusing a thread with the same run but different parameters


First, it is best to use the standard Thread class (don't subclass it!) and put your application code into a class that implements Runnable. This makes it much easier to separate your application logic from the problem of managing the threads.

Second you have to understand that a Thread object calls the run method just once. After the run method has returned (or terminated with an exception), the Thread object is dead, and cannot be brought back to life.

So if you want to "reuse" Thread instances, you have to arrange that the run method is a loop that (somehow) waits for the next thing to be done. And before you know it you are implementing a thread pool.

There is another (more "modern") alternative to a thread pool. Create an ExecutorService instance, and use the submit() method to submit the Runnable instances for execution. The interface javadoc has a good usage example, using an executor service instance with a private thread pool. If you wanted to, you could reuse the Runnable instances, but generally it is simpler (and safer) to create new ones each time.


Make a separate class for your code that implements Runnable. You will need to make new Thread objects each time, but construct these with your single Runnable object. The run method will be reused by each Thread.


A thread always starts exactly once, runs exactly once and then dies exactly once. So you can't call start() on the thread multiple times in the way that you suggest.

So, one way or another, you have to either:

  • Have a single thread that, one way or another within its run() method, executes each of your method calls with the different sets of parameters.
  • Start a new thread for each "run" (method call to the Thing That You Actually Want To Do).

Which implementation is more appropriate depends a little on your exact application. But in general, it is helpful to think of a thread as being a sub-task that runs parallel to other subtasks. So in general:

  • if you have a series of independent "runs" of something which you have no reason to run in parallel to one another, arrange for these runs to be executed one after the other inside a single Thread.run() method;
  • if you have a series of tasks that may as well run parallel to one another, put each in a separate Thread.

A thread pool is generally used for cases where at any given moment you can have some arbitrary task to execute, and such tasks can run in parallel. For example, on a typical server. Think of a web server receiving requests for pages from different clients at arbitrary points in time: each request can come at some arbitrary point in time and a request for a page from one client is completely independent from a request from another client. In principle, you don't care which requests run in parallel to which, other than that your machine has resource constraints such as the number of CPUs. For this type of case, where you effectively want to "run arbitrary tasks in arbitrary threads, up to a limit of X tasks running in parallel", you would generally use a thread pool.