×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

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?