【发布时间】:2015-06-08 14:03:40
【问题描述】:
我正在研究如下用例。我是多线程的新手,在使用它时遇到了这个问题。
- 我在网络上广播了一个事件。
- 所有听众都收到了,他们用他们的信息单播了我。
- 这是在回调方法中收到的,如下所示,我将获得 N 个未知数量的回调线程。取决于当时的听众。
- 我必须收集所有订阅者的列表。
我必须等待至少 10 秒才能让所有订阅者回复我。
//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.
}
我只关心如何..?
- 在 sendMulticast 事件处开始等待 X 秒
- 在收到的所有事件都添加到列表后,在 receiveEventsCallback() 处通知。
我已经阅读了关于等待和通知、倒计时和障碍的理论。但我不确定哪个更好,因为我在多线程方面的经验很差。
【问题讨论】:
-
投反对票?? .. 请评论为什么?
-
我对自己投了赞成票,但您确实应该提供更多信息。您没有提供有关如何管理网络层的足够信息。你在监听端口吗?在多播呼叫中使用 15 秒计时器有什么问题?我认为 wait/notify 不会像你想的那样。
标签: java multithreading thread-safety wait notify