I want to start several threads and join them at the end of my main method, so I can find out when they finished and processed some information.
I donβt want to put my threads into an array and do join () one by one, since joining is a locking method, and I remain waiting in the main thread for some threads still working, while other threads may have already finished not being able to know.
I thought about the possibility of implementing an observer pattern for my threads: an interface with the update () method, an abstract class extending from the stream (or an executable executable) using the set and get methods for listeners and starting the class all my topics and waiting for them to complete.
If my understanding is correct, the observer will not block the specific connection () for the stream. Instead, it will wait one way or another until the update () method is called by the thread to complete the action. In this case, update () should be called immediately after the stream ends.
I do not know how to implement this. I tried with similar models, but I don't know how to use the observer / listener to wake / block my main thread. I used this old post as a template: How do I know if other threads have ended? , but I canβt find a way to wake up my main method after the thread calls the update () method. Only one observer object will be created for all threads.
Could you please think about how to use the observer pattern to wait for the completion of all threads without blocking the main one call join ()? Any other suggestion to solve this problem would be very helpful. Thanks in advance.
source share