为大牛指导指导,网络编程中的服务器端,为什么一句代码放的位置不对,就会出现内存使用特别大,甚至卡死电脑的问题?代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Server_Test {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
final ServerSocket server = new ServerSocket(9000);
while (true) {
final Socket socket = server.accept(); //这一句
new Thread(){
public void run(){
try {
//将上面这一句放在这,为什么会内存使用特别大,甚至卡死电脑的呢? 搞不明白,电脑挂了好几次了都
//Socket socket = server.accept();
BufferedReader br = new BufferedReader
(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("hello!");
System.out.println(br.readLine());
ps.println("helloworld");
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
|
|