Java使用套接字使用100%的CPU

时间:2013-10-18 08:31:31

标签: java sockets tomcat

我在Linux机器上的tomcat上运行Java应用程序。此应用程序的工作方式类似于套接字服务器,其中连接了大约15个设看起来当设备发送大消息时,cpu会一直增长,直到100%使用。问题是,如果我取消部署应用程序,java仍然拥有99%的CPU。该应用程序有两个部分:

套接字服务器:

    public void iniciarSocket() {

    Runnable serverTask = new Runnable() {
        @Override
        public void run() {
            try {

                serverSocket = new ServerSocket(PORT);

                System.out.println("Waiting a connection");

                while (true) {
                    Socket socket = null;

                    try {
                        socket = serverSocket.accept();

                        System.out.println("Client connected");
                    } catch (Exception e) {
                        System.out.println("Error: " + e.getMessage());
                    }
                    new SocketThread(socket).start();
                }

            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    };
    Thread serverThread = new Thread(serverTask);
    serverThread.start();
}

每个套接字线程,与每个设备连接:

    public void run() {
    try {

        //Output channel
        DataOutputStream salida;
        salida = new DataOutputStream(socket.getOutputStream());

        System.out.println("Client connected.... ");

        byte[] buff = new byte[1024];
        int bytesLeidos = 0;
        socket.setSoTimeout(300000);
        System.out.println("Timeout: " + socket.getSoTimeout());

        while((bytesLeidos = socket.getInputStream().read(buff, 0, buff.length)) > -1) {

            System.out.println("Bytes leidos: " + bytesLeidos);

            if ((bytesLeidos == 70) && (Protocolo.isStatusMessage(buff))) {
                Protocolo.decode(buff, salida);
            } else {

                int offset = 0;

                while (offset < bytesLeidos) {

                    while ((offset + 70 <= bytesLeidos) &&(!Protocolo.isStatusMessageWithOffset(buff, offset))) {
                        offset++;
                    }

                    if ((offset + 70 <= bytesLeidos) &&(Protocolo.isStatusMessageWithOffset(buff, offset))) {
                        Protocolo.decodeWithOffset(buff, offset, salida);
                        offset += 70;
                    }
                }
            }
        }
    } catch (Exception e) {
        System.out.println();
    } finally {
        System.out.println("Communication ended");
        try {
            socket.close();
        } catch (Exception e) {
            System.out.println("Socket not closed");
        }
    }
}

我不明白发生了什么,我正在做很多事情,但我无法解决问题。

1 个答案:

答案 0 :(得分:1)

似乎问题已经解决了。 EJP是对的,如果消息不是70的倍数,则循环永远不会结束。我只需要更改socketThread。

            while((bytesLeidos = socket.getInputStream().read(buff, 0, buff.length)) > -1) {

            System.out.println("Bytes leidos: " + bytesLeidos);

            if ((bytesLeidos == 70) && (Protocolo.isStatusMessage(buff))) {
                Protocolo.decode(buff, salida);
            } else {


                int offset = 0;

                // Code changed
                while (offset < bytesLeidos) {

                    if (Protocolo.isStatusMessageWithOffset(buff, offset)) {
                        // decodificar
                        Protocolo.decodeWithOffset(buff, offset, salida);
                        offset += 70;
                    } else {
                        offset++;
                    }
                }
                // End code changed
            }
        }

非常感谢。