i am working on a usecase as below. I am new to multi threading and facing this issue with using it.
- I broadcast a event on network.
- Its received by all the listeners, and they unicast me with their information.
- This is received inside the call back method as below, i will get N unknown number of callback threads. depending on listeners at that particular time.
- I have to collect a list of all subscribers.
I have to wait at least 10sec for all the subscribers to reply to me.
//Sender
public void sendMulticastEvent() {
api.sendEvent();
/* after sending event wait for 15 sec so call back can collect all the subscribers */
//start waiting now
}
//Callback method
public void receiveEventsCallback(final Event event) {
//i will receive multiple response threads here..
//event object will have the topic and subscribers details, which i will collect here
list.add(event)
notify()
//notify thread here so i have a cumulative list of all received events.
}
I am only concerned on How to.. ?
- Start a wait at the sendMulticast event for X seconds
- Notify at receiveEventsCallback() after all the recieved events has been added to the list.
I have read theroitically on wait and notify, Countdownlatch and Barrier. But i am not sure which would be good, because of my poor experience in multithreading.