小编Dia*_*lus的帖子

Java套接字占用太多的CPU和虚拟内存

我已经开发了一个Java套接字,但是它占用了太多的CPU和虚拟内存。您能告诉我代码中的问题是什么吗?

private void listen() {
    try {
        serverSocket = new ServerSocket(port);

        System.out.println("Server socket listening on port: " + port);
        System.out.println("Waiting for connections...");

        while(true) {
            // accept the connection
            Socket socket = serverSocket.accept();
            socket.setSoTimeout(30000);
            System.out.println("Got connection");
            // start processing the connection
            Thread connectionManager = new Thread(new Elevator(socket, socket.getInputStream()));//new Thread(new ConnectionManager(socket, odometer));
            connectionManager.start();
        }
    } catch (IOException exc) {
        System.out.println(Listener.class.getName() + ": " + exc.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

在电梯班我有这个:

public class Elevator implements Runnable 
{

private String imei;
private Socket socket;
private InputStream …
Run Code Online (Sandbox Code Playgroud)

java sockets multithreading memory-management cpu-usage

2
推荐指数
1
解决办法
1302
查看次数