如果网络被修改,Java 应用程序将挂起

发布于 2024-08-09 01:49:23 字数 418 浏览 18 评论 0原文

我正在开发一个有多个视图的 Eclipse 应用程序。从这些视图中,我必须通过网络访问一些数据。在以下情况下会引发错误。

第 1 步:启动应用程序,一切正常。
第 2 步:禁用网络
第 3 步:启用网络
第 4 步:尝试从其中一个视图通过线路访问数据。该应用程序显然挂起。
第 5 步:现在,如果您尝试从其他视图访问数据,一切都会正常。

最初我认为这是由于 JVM 的 DNS 缓存而发生的。所以我做了这样的事情。

System.setProperty( "networkaddress.cache.ttl", "0" );
System.setProperty( "networkaddress.cache.negative.ttl" , "0" );

但问题仍然存在。任何帮助表示赞赏。

I am working on an eclipse app that has got multiple views. From these views I have to access some data over the wire. An error is thrown in following scenario.

Step 1: Start app, everything works fine.
Step 2: Disable network
Step 3: Enable Network
Step 4: Try to access data over wire from one of the views. The app apparently hangs.
Step 5: Now if you try to access data from some other view, everything works.

Initially I thought that it is happening due to DNS caching by JVM. So I did something like this.

System.setProperty( "networkaddress.cache.ttl", "0" );
System.setProperty( "networkaddress.cache.negative.ttl" , "0" );

But the problem is still there. Any help is appreciated.

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

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

发布评论

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

评论(3

不醒的梦 2024-08-16 01:49:24

可能在步骤2和步骤3之间。抛出什么类型的异常?

it's probably between step 2 and 3. What type of exception is thrown?

瑶笙 2024-08-16 01:49:24

当应用程序挂起时查看当前堆栈的另一种选择是使用 jps/jstack(与从 1.5 开始的 Sun JDK 捆绑在一起)或 Visualvm(在 1.6 SDK 中提供,并与 https://visualvm.dev.java.net/)

jps 列出计算机上当前的所有 java 进程。然后,您可以使用每个进程 ID 调用 jstack 来获取当前堆栈。

也许如果您可以发布这样的堆栈跟踪,我们可以提供帮助。

Another option to see the current stack when your app is hanging would be to use jps/jstack (bundled with Sun JDKs beginning with 1.5) or visualvm (available in 1.6 SDKs and separately from https://visualvm.dev.java.net/)

jps lists all current java processes on a machine. you can then call jstack with each of these process ids to get the current stack.

Maybe if you could post such a stack trace we could help.

单身狗的梦 2024-08-16 01:49:23

完全疯狂的猜测:如果您使用 TCP,您的应用程序可能不会注意到其套接字已断开连接。如果它在读取中被阻止,它可能不会注意到,直到它尝试向套接字写入内容。这取决于套接字关闭的“干净”程度——如果套接字没有从另一端收到 RST 数据包,它就不会知道另一端已经消失。

您可以尝试从应用程序中强制执行线程转储。通常,JVM 将在终端上运行时响应 Ctrl-\ 或响应带有每个线程的堆栈跟踪的 kill -QUIT 信号。这可以让您看到有问题的线程被阻塞的位置。

如果您可以发布一些可疑代码,那么尝试做出诊断就会容易得多。或者您能解释一下为什么您认为 DNS 缓存会成为一个问题吗?

Completely Wild Guess: if you're using TCP your app may not be noticing that its socket has been disconnected. If it's blocked in a read it may not notice until it tries to write something to the socket. It depends on how "clean" the socket shutdown is--if the socket doesn't get a RST packet from the other side it won't know that the other end has gone away.

You could try coercing a thread dump out of your app. Typically the JVM will respond to either Ctrl-\ when running at a terminal or to a kill -QUIT signal with a stack trace of every thread. That'd let you see where the thread in question is blocked.

If you can post some suspect code it'd be much easier to try to make a diagnosis. Or can you explain why you think DNS caching would be an issue?

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