检查互联网连接的可用性 + 2个线程之间通信
我有 2 个问题:
如何检查互联网连接是否打开或关闭?我正在使用 Html Unit,并且在 Windows 上工作。
我想制作一个 JLabel 来说明我的 JFrame 中互联网连接的可用性。类似于:
while(true)
{
if(isOnline()) label.setText("online");
else label.setText("offline");
}
但我认为我需要 2 个分离的线程,但是我如何创建这 2 个线程并在它们之间进行通信并实现这一目标?
I have 2 questions:
How can I check if Internet connection is whether turned on or turned off? I'm using Html Unit and I'm working on Windows.
I want to make a JLabel that states the availability of internet connection in my JFrame. Something like:
while(true)
{
if(isOnline()) label.setText("online");
else label.setText("offline");
}
but I think I need 2 sperated threads, but how could I create these 2 threads and communicate between them and achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此处回答了类似的问题:how-to-check -if-internet-connection-is-present-in-java
要完成问题的第二部分,我想您可以调用 InetAddress.isReachable 方法在包装在 SwingWorker 线程中的 java.util.Timer 中,以定期轮询连接。
A similar question was answered here: how-to-check-if-internet-connection-is-present-in-java
To do the second part of your question, I suppose you could call the InetAddress.isReachable method in a java.util.Timer that is wrapped in a SwingWorker thread to periodically poll the connection.
while (true)
对您的资源有点苛刻,应该有大约 10 秒的暂停。这并不需要像股票价格行情一样准确和实时。对于 Swing,规则是不执行任何在事件分派线程上花费时间的操作。后台任务或其他长时间运行的操作不应在其上运行。
对于您的特定情况此适用:
因此,我要做的是设置一个计划任务,每 10 秒检查一次互联网连接的可用性关闭事件调度线程 - 这可能最好立即在您的
中设置main 方法。另一方面,GUI 的更新应该发生在事件分派线程上。这是一个可以在标签上进行更新的
。Runnable
提案。稍后我将解释ConnectionChecker
。我们使用 ExecutorService 执行它并设置 5 秒的超时。如果ConnectionChecker
在 5 秒内成功,则您已连接,否则未连接。然后,使用SwingUtilities#invokeLater
使用此结果来更新事件调度线程上的 JLabel现在我们必须在 main 方法中设置一个定期执行的
Updater
,同时我们还创建 JLabel 和 ExecutorService:最后,让我们来推理一下
ConnectionChecker
。有很多方法可以做到这一点。一个快速而肮脏的解决方案是简单地获取一个可能在未来几年内保留的网页 - 例如 www.google.com 。如果您(正确)连接到 Internet,则此查询将会成功。否则,它最终会抛出一些异常。通过连接,下载应在 5 秒内完成 - 如果不是,则可以安全地使连接尝试超时。无论哪种方式,如果下载未及时成功,您都会在Updater
中收到异常。使用简单的URLConnection
或类似的内容进行下载。while (true)
is a bit harsh on your resources, there should be a pause of ~10s in there. It's not like this would need to be as accurate and real-time as a stock price ticker.With Swing, the rule is to do nothing that takes time on the event dispatch thread. Background tasks or other long-running operations should never run on it.
For your particular case this applies:
So what I would do is set up a scheduled task that checks for the availability of an internet connection every 10 seconds off the event-dispatch thread - this is probably best set up right away in your
main
method. The updating of the GUI on the other hand should then happen on the event dispatch thread. Here is a proposal for aRunnable
that could do the update on your label. I'll explain theConnectionChecker
later. We execute it using aExecutorService
and set a timeout of 5 seconds. If theConnectionChecker
succeeds within 5 seconds, you are connected, otherwise you are not. This result is then used to update theJLabel
on the event dispatch thread usingSwingUtilities#invokeLater
.Now we have to set up a periodically executed
Updater
in the main method, where we also create the JLabel and the ExecutorService:Finally, let's reason about
ConnectionChecker
. There's plenty of ways how you could do this. A quick and dirty solution is to simply fetch a web page that's likely to stay in the next few years - how about www.google.com. If you are connected to the Internet (correctly), this query will succeed. Otherwise, it will finally throw some Exception. With a connection, the download should be done in under 5 seconds - if it's not, then it's safe to time out the connection attempt. Either way, you will receive an Exception in yourUpdater
if the download did not succeed in time. Use a simpleURLConnection
or similar for the download.