Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I still have some trouble wrapping my head around threads, and I'm trying to do this in the simplest way possible. I know that threads all have to have a run method they inherited from the Runnable class, but they can have additional methods as well, correct?

The reason being, I have a thread with some private variables and a run method. It calls it's run function, and once it's done, I want to reuse the thread with the exact same run method. It does exactly the same thing, just with different variables. So can I add something like a setArray method (the thread contains a private byte array) so I can just simply call run again with this new array, or is that not allowed. I guess to put it simply, it'd be something like

Thread thread = new MyThread();
thread.start();

// Check if the thread has finished in a non-blocking way
if (thread.isAlive() == false) {
    thread.setArray(newArray)
    thread.start();
}

Basically I only have a fixed number of threads and when the first thread is done running, I want to change the parameters a bit and have it run again. I don't wan them to die, which seems to be what join does.

For the specific problem, I have say 4 threads, that each are give a set size block of a larger byte array. Each thread compresses that array with a Deflater and passes their result to a manager object that handles the synchronization. Once the first thread (as in the thread that got the first part of the array, not the first to finish) is done, it moves on to the next block not assigned to a thread.

I know thread pools are an option, but it seems a bit overkill plus I really don't understand them (I'm still having trouble with just normal threads).

like image 438
user1777900 Avatar asked Oct 28 '12 05:10

user1777900


People also ask

Can you reuse threads in Java?

There is no "reuse" in this case. So it is true that you cannot call start() on a Java Thread twice but you can pass as many Runnable as you want to an executor and each Runnable 's run() method shall be called once.

Can you reuse threads?

There is no way to reuse a Thread because Thread once finishes (exit the run() method) its Thread.

What is likely to happen if you have multiple threads accessing the same resource in your program?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

Can we call same thread twice Java?

No, we cannot start Thread again, doing so will throw runtimeException java. lang. IllegalThreadStateException. > The reason is once run() method is executed by Thread, it goes into dead state.


Video Answer


3 Answers

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.

like image 162
Stephen C Avatar answered Nov 01 '22 05:11

Stephen C


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.

like image 27
slipperyseal Avatar answered Nov 01 '22 05:11

slipperyseal


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.

like image 32
Neil Coffey Avatar answered Nov 01 '22 03:11

Neil Coffey