I am looking for a clean design / solution for this problem: I have two threads that can work as long as the user wants, but end up when the user issues a stop command. However, if one of the threads ends abruptly (for example, due to an exception at runtime), I want to stop the other thread.
Now both threads execute Runnable (so when I say stop the thread, I mean that I call the stop () method on the Runnable instance), I think that you should avoid using threads (Thread class) and use the CompletionService interface. and then send both Runnables to an instance of this service.
With this, I would use the CompletionService take() method, when this method returns, I would stop both Runnables, since I know that at least one of them has already been completed. Now it works, but if possible, I would like to know a simpler / better solution for my case.
Also, what is a good solution when we have n threads, and as soon as one of them ends to stop all the others?
Thanks in advance.
source share