0

我有两个线程会增加 CPU 开销。1. 以同步方式从套接字读取。2.等待接受来自其他客户端的连接

问题 1,我只是想读取来自客户端的任何数据,但我不能使用 readline,因为传入的数据有我标记的换行符以了解消息的标题结尾。所以我在线程中使用这种方式,但它增加了 CPU 开销

 public static String convertStreamToString(TCPServerConnectionListner socket) throws UnsupportedEncodingException, IOException, InterruptedException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getSocket().getInputStream()));
        // At this point it is too early to read. So it most likely return false
        System.out.println("Buffer Reader ready? " + reader.ready());
        // StringBuilder to hold the response
        StringBuilder sb = new StringBuilder();
        // Indicator to show if we have started to receive data or not
        boolean dataStreamStarted = false;
        // How many times we went to sleep waiting for data
        int sleepCounter = 0;
        // How many times (max) we will sleep before bailing out
        int sleepMaxCounter = 5;
        // Sleep max counter after data started
        int sleepMaxDataCounter = 50;
        // How long to sleep for each cycle
        int sleepTime = 5;
        // Start time
        long startTime = System.currentTimeMillis();
        // This is a tight loop. Not sure what it will do to CPU
        while (true) {
            if (reader.ready()) {
                sb.append((char) reader.read());
                // Once started we do not expect server to stop in the middle and restart
                dataStreamStarted = true;
            } else {
                Thread.sleep(sleepTime);
                if (dataStreamStarted && (sleepCounter >= sleepMaxDataCounter)) {
                    System.out.println("Reached max sleep time of " + (sleepMaxDataCounter * sleepTime) + " ms after data started");
                    break;
                } else {
                    if (sleepCounter >= sleepMaxCounter) {
                        System.out.println("Reached max sleep time of " + (sleepMaxCounter * sleepTime) + " ms. Bailing out");
                        // Reached max timeout waiting for data. Bail..
                        break;
                    }
                }
                sleepCounter++;
            }

        }
        long endTime = System.currentTimeMillis();

        System.out.println(sb.toString());
        System.out.println("Time " + (endTime - startTime));

        return sb.toString();
    }

问题 2,我不知道这样做的最佳方法是什么,我只是有一个线程不断等待其他客户端并接受它。但这也需要大量的 CPU 开销。

 // Listner to accept any client connection
    @Override
    public void run() {

        while (true) {
            try {
                mutex.acquire();
                if (!welcomeSocket.isClosed()) {
                    connectionSocket = welcomeSocket.accept();
                   // Thread.sleep(5);
                }


            } catch (IOException ex) {
                Logger.getLogger(TCPServerConnectionListner.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(TCPServerConnectionListner.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally
            {
                mutex.release();
            }

        }
    }
}

Profiler 图片也会有所帮助,但我想知道为什么 SwingWorker 线程要花那么多时间? 在此处输入图像描述

问题一的更新代码:

    public static String convertStreamToString(TCPServerConnectionListner socket) throws UnsupportedEncodingException, IOException, InterruptedException {

            byte[] resultBuff = new byte[0];
            byte[] buff = new byte[65534];
            int k = -1;
            k = socket.getSocket().getInputStream().read(buff, 0, buff.length);
                byte[] tbuff = new byte[resultBuff.length + k]; // temp buffer size = bytes already read + bytes last read
                System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length); // copy previous bytes
                System.arraycopy(buff, 0, tbuff, resultBuff.length, k);  // copy current lot
                resultBuff = tbuff; // call the temp buffer as your result buff

        return new String(resultBuff);
    }

}
        ![snapshot][2]
4

3 回答 3

2

只需摆脱ready()呼叫并阻止。在 ready() 为 false 时所做的一切实际上都是在浪费时间,包括睡眠。将read()阻塞恰好合适的时间。Asleep()不会。您要么睡眠时间不够长,这会浪费 CPU 时间,要么睡眠时间过长,这会增加延迟。偶尔你可能会睡到正确的时间,但这是 100% 的运气,而不是好的管理。如果您想要读取超时,请使用读取超时。

于 2013-04-12T10:00:49.180 回答
1

您似乎在等待一段时间后没有更多数据。

我建议你使用Socket.setSoTimeout(timeout in seconds)

一个更好的解决方案是不需要通过一个协议来执行此操作,该协议允许您知道何时到达数据末尾。如果您的服务器实施不当并且您无法修复它,您只会这样做。

于 2013-04-12T08:23:33.380 回答
0

对于问题 1。100% CPU 可能是因为您正在从 BufferedReader.read() 读取单个字符。相反,您可以将数据块读取到数组并将其添加到您的字符串构建器。

于 2013-04-12T11:14:07.273 回答