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).
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.
There is no way to reuse a Thread because Thread once finishes (exit the run() method) its Thread.
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.
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.
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:
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With