I am using ExecutorService to launch multiple threads.
All thread started concurrently. I am using a ConcurrentHashMap to store result of Thread A , Thread B , Thread C which can be used by Thread D in future.
Now it may happen that Thread A is still under execution when Thread D is looking for the output from Thread A.
This is applicable for Thread C which may be dependent on output of Thread D
Or Thread N which may be dependent on output of any of threads like Thread N-1 , or Thread N- 4 etc ...
What can be the best approach to handle this situation or to make thread A wait until other threads are ready with there output
I suggest having three tasks producing results which are queued into the same Executor or another executor. This way Thread D only does work when there is something to do.
A better option might be for Thread A to process its own work immediately (and the same for Thread B & C) in which case you don't need Thread D.
Sounds like you want to use join. Joining threads after starting will effectively make your program run in a liniear fashion. Thread A runs, Then B, then C then D will look for the results of the first 3 threads once thread C has finished.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.