×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Is there anything wrong with this code? No object refers to KKMultiServerThread after it is created. Will it be removed by garbage collector?

import java.net.*;
import java.io.*;

public class KKMultiServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;

try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}

while (listening)
new KKMultiServerThread(serverSocket.accept()).start();

serverSocket.close();
}
}
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Is there anything wrong with this code? No object refers to KKMultiServerThread after it is created. Will it be removed by garbage collector?
    import java.net.*;
    import java.io.*;

    public class KKMultiServer {
    public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    boolean listening = true;

    try {
    serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
    System.err.println("Could not listen on port: 4444.");
    System.exit(-1);
    }

    while (listening)
    new KKMultiServerThread(serverSocket.accept()).start();

    serverSocket.close();
    }
    }
    • help me! The code is very simple!
    • An instance of KKMultiServerThread will not become eligible for gc before it's terminated. Keep in mind, the new thread created by JVM calls KKMultiServerThread.run() method. You didn't say what happened.
      • you mean the Thread is refered by the JVM. Ok! Let's try an other example
        import javax.swing.*;
        public class testf
        {
        public static void main(String arg[])
        {
        JFrame f;

        (new JFrame("god")).show();

        System.out.println("Hello");

        }

        }
        • Sure, each window is owned by an independent thread. What's your point?