等待 DNS 查找时使用多线程还是观察者模式?

发布于 2024-09-25 05:50:16 字数 219 浏览 5 评论 0原文

我正在用java设计一个系统,它利用dns查找类。

我的问题是,当调用类的 dnsLookup() 时,是在新线程中执行还是使用观察者模式并让 dns 类告诉我何时完成。

只要查找几乎立即返回一个值,这就不是问题,但是当需要几秒钟(没有得到响应时)时,我不想在等待时冻结 GUI。

所以,新的线程或观察者。也欣赏一些关于该主题的良好链接。

预先感谢 - 丹尼斯

I'm designing a system in java which utilizes a dns lookup class.

My question is, when calling the class's dnsLookup(), whether to do it in a new thread or use the observer pattern and let the dns class tell me when it's done.

This isn't a problem as long as the lookup returns a value almost instantly but when it takes a few seconds (when it doesn't get a response), I don't want to freeze the GUI while waiting.

So, new thread or observer. Appreciate some good links on the subjects as well.

Thanks beforehand - Dennis

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

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

发布评论

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

评论(3

时光暖心i 2024-10-02 05:50:16

您将必须同时使用观察者模式和多个线程。无法在同一线程中调用 DNS 回调方法。

You will have to employ both the observer pattern and more than one thread. There's no way to have the DNS invoking callback method in the same thread.

吾家有女初长成 2024-10-02 05:50:16

你的 GUI 是一个事件驱动系统,所以异步通知是很好的。

另一方面,如果使用同步(阻塞)网络调用,则执行网络 I/O(特别是如果它只是单个 DNS 查找)会容易得多。

因此,我倾向于选择单独的线程选项,但随后让该线程在完成时通知主 GUI 线程。

Your GUI is an event driver system so asynchronous notifications are good.

On the other hand, it's a lot easier to do network I/O (particularly if it's just a single DNS lookup) if you use synchronous (blocking) network calls.

Hence I would tend to go for the separate thread option, but then have that thread notify the main GUI thread when it's done.

平定天下 2024-10-02 05:50:16

由于它是一个 GUI 进行调用,我认为最好将调用卸载到不同的字符串。事实上,您需要确保没有使用 AWT-Thread 进行阻止 GUI 刷新的调用。我建议使用类似 ExecutorService< /a> 执行命令,然后在返回时使用 SwingUtilities 并调用 invokeLater(Runnable doRun) 方法以使用响应更新 GUI。

Since it's a GUI that is making the call, I think it's best that you off-load the call to a different string. In fact, you want to make sure that you're not using the AWT-Thread to make a call that is blocking the GUI from refreshing. I would suggest using something like an ExecutorService to execute your commands and then upon the return, use SwingUtilities and call the invokeLater(Runnable doRun) method to update the GUI with the response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文