我正在尝试编写一个消息确认应用程序,我需要: a) 将每条新消息添加到消息队列中。只需使用 Arraylist 来创建消息队列。 b) 通知计时器任务期望在 50 秒内收到消息确认,因此使其 hibernate 50 秒或在收到确认时唤醒。
实现这一点的最佳实践是什么?
谢谢
最佳答案
不太清楚你的需求是什么。这与 Swing 或计时器有什么关系?您在这里处理哪种线程?我将做出一些假设并提出一些建议。
听起来您想将消息放入队列中,然后等待收到响应,或者最多 50 秒。您应该查看BlockingQueue 。它是线程安全的,您可以等待特定的时间让另一个线程向其中放入内容。这似乎对于消息/确认问题很有用。
BlockingQueue<MSG> queue = new LinkedBlockingQueue<MSG>();
// put a message in the queue
queue.put( msg );
// have a thread wait on the queue until something is available in it
MSG msg = queue.poll( 50, TimeUnit.SECONDS );
我需要有关您问题的更多详细信息以获得更具体的帮助。
关于java - Swing 中的计时器任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3969663/