This topic has been archived. It cannot be replied.
工作学习 / IT技术讨论 / Is there anything wrong with this code? No object refers to KKMultiServerThread after it is created. Will it be removed by garbage collector?
-std(std);
2001-9-18{561}(#204419@0)
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!
-std(std);
2001-9-18(#204461@0)
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.
-doors(道士);
2001-9-18(#204490@0)
you mean the Thread is refered by the JVM. Ok! Let's try an other example
-std(std);
2001-9-18{210}(#204511@0)
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?
-doors(道士);
2001-9-18(#204519@0)