Swing 中的计时器任务

发布于 2024-09-28 03:19:19 字数 153 浏览 0 评论 0原文

我正在尝试编写一个消息确认应用程序,我需要: a) 将每条新消息添加到消息队列中。只需使用 Arraylist 来创建消息队列。 b) 通知计时器任务期望在 50 秒内收到消息确认,因此使其休眠 50 秒或在收到确认时唤醒。

实现这一点的最佳实践是什么?

谢谢

I'm trying to write a message acknowledgement application where I need to:
a) Add every new message on a message queue. Just using an Arraylist for creating a message queue.
b) Notify a timertask to expect an acknowledge in 50 sec for the message so make it to either sleep for 50sec or wake up when an acknowledgement is received.

What is the best practice to implement this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

马蹄踏│碎落叶 2024-10-05 03:19:19

我不太清楚你的需求是什么。这与 Swing 或计时器有什么关系?您在这里处理哪种线程?我将做出一些假设并提出一些建议。

听起来您想将消息放入队列中,然后等待收到响应,或者最多 50 秒。您应该查看 BlockingQueue< /a>.它是线程安全的,你可以等待特定的时间让另一个线程往里面放东西。这似乎对于消息/确认问题很有用。

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 );

我需要有关您的问题的更多详细信息以获得更具体的帮助。

I'm not quite clear what your needs are. What does this have to do with Swing or timers? What kind of threading are you dealing with here? I'll make some assumptions and suggest a couple things.

It sounds like you want to put a message in a queue, then wait until a response is received, or 50s max. You should check out BlockingQueue. It is thread-safe, and you can wait for a specific amount of time for another thread to put something in it. This seems like it could be useful for message/acknowledge problem.

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 );

I need more details on your problem for more specific help.

天赋异禀 2024-10-05 03:19:19

我认为您想要某种机制,在特定间隔后需要更新,您可以使用线程并根据间隔要求设置睡眠,例如:

public void run() {

    while (Start == true) {
        getMessage();      //yourmethod();
        try {
            Thread.sleep(50);
        } catch (InterruptedException ie) {
            stop();
        }
    }
}

i thought u wanted to have some mechanism, where updation needs after particular intervals, you can use the thread and set a sleep as per interval requirement like :

public void run() {

    while (Start == true) {
        getMessage();      //yourmethod();
        try {
            Thread.sleep(50);
        } catch (InterruptedException ie) {
            stop();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文