English 中文(简体)
如果服务器套件是垃圾收集的,那么其相关的装订端口会怎么样?
原标题:If a ServerSocket is garbage-collected, what happens to its associated port binding?

我想创建一个能够检测到其他自身实例并防止它们运行的应用程序。 为了实现这一点, 我正在考虑在一些应用程序特定端口打开一个新的 < code> ServerSocket , 然后依赖如果我试图多次连接到同一个端口以“ 检测” 并杀死重复应用程序实例而应该推出的例外。 我知道可以做一些事情, 比如将文件写入当前工作目录, 并“ 检测” 来完成同样的行为, 但我真的不想这样做( 如果应用程序死, 无法删除文件会发生什么? ), 因此我选择了 < code> ServerSocket 路径 。

可以说我有以下代码:

public class MyClass{
    public static void main(String[] args) throws IOException{
        new ServerSocket(1234);

        new Thread(){
            //This is a non-daemon thread. Assume its run() method never returns.
        }.start();
    }
}

<强问/强>

除了创建 < code> ServerSocket 之外, 我的应用程序从不需要再使用它, 因为它的存在本身就让我能够探测到另一个应用程序尝试启动的实例。 因此, 省下上述 < code> ServerSocket 的引用将导致编译警告( 未使用的引用 ) 。 我是一个整洁的错误, 所以如果我能避免它, 我宁愿不保存一个引用 。 我的问题是, 这个 < code> ServerSocket 实例是否会在所有非天线条退出前收集垃圾( 假设应用程序不失败或退出其他方式), 如果是这样, 其相关端将因此被解开?

最佳回答

我感到困惑:你是一个“无赖怪胎”,但宁愿让垃圾收集者随心所欲地离开套座,而不愿在正常出口的情况下在申请控制下有一个密切的程序。 (我想这是事实,因为要建立这样一个机制,你就必须保持一个参考,从而消除你所察觉的问题。 )

我不认为实际中会有问题 如果你不坚持这个提法:

  • internally, it is likely that a reference will be held while the socket is bound
  • it is likely that the ServerSocket s close() method will be called on garbage collection. It is called in the finalize() method of AbstractPlainSocketImpl, so in principle will be called, with the proviso that there is no guarantee that any finalize method will actually get called.
  • if the application terminates abnormally, there is no guarantee that the socket will be unbound immediately, though on recent O/Ses it is likely to be (best to test this).

然而,我确实建议为您的应用程序被干净地关闭的情况编码一个“干净”的关闭机制,并且你需要坚持提及插座。所以我认为你真的在给自己制造一个问题,而如果你只使用明智的编程实践,就不需要存在这样的问题。

问题回答

OK, 让我试着将自己从我先前的错误理解中解脱出来。 您提到的文件锁和套接字技术被广泛使用, 但还有一个 — 拥有一个观察者来保留当前 Yor 程序的例子( 通过方法注册和取消注册 ) 。

如果一个新实例试图在另一个实例运行期间进行登记,登记程序将失败,您的申请将顺利结束。

您的应用程序随后可以执行一个可观察的界面, 该界面包含一种被观察的方法, 以便您的观察者知道您的应用程序仍然有效。 因此, 如果您的应用程序崩溃, 定期检查将会失败, 并且它会自动解除崩溃应用程序的注册 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签