Java - 从缓冲读取器(从套接字)读取正在暂停线程

Dav*_*vid 8 java sockets networking buffer

我有一个线程从缓冲读取器读取字符(从套接字创建如下):

inputStream = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
Run Code Online (Sandbox Code Playgroud)

此代码只能运行一次.例如,如果客户端连接并发送:"这是一个测试"和"这是另一个测试",则主机输出为:

 Reading from stream:
 Chars read from stream: 16
 This is a test

 Reading from stream:
Run Code Online (Sandbox Code Playgroud)

请注意,程序不会收到"这是另一个测试",因为它一直在读取流.有没有办法处理这个而不减少缓冲区大小?这是线程的代码:

public void run() {
        boolean dataRecieved = false;
        char[] inputChars = new char[1024];
        int charsRead = 0;

        while (!stopNow) {

            try {
                Thread.sleep(getDataDelay);

                //Read 1024 characters. Note: This will pause the thread when stream is empty.
                System.out.println("Reading from stream:");
                charsRead =  inputStream.read(inputChars); //<< THIS LINE IS PAUSING THE THREAD!> 


                if ((charsRead =  inputStream.read(inputChars)) != -1)
                {
                    System.out.println("Chars read from stream: " + charsRead);  
                    System.out.println(inputChars);
                    System.out.flush();
                }


            } catch (IOException e) {
                System.out.println("IOException");
                //TODO: CLIENT HAS DISCONNECTED...
            } catch (InterruptedException e) {
                System.out.println("Interrupted");
                // Sleep was interrupted.
            } 

        }

    }
Run Code Online (Sandbox Code Playgroud)

客户/发件人代码(不是我的代码):

public static void main(String[] args) throws IOException {
        // <<<<<<<<<<< CLIENT >>>>>>>>>>>>>>>

        Socket sock = new Socket("127.0.0.1", 3000);
        // reading from keyboard (keyRead object)
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
        // sending to client (pwrite object)
        OutputStream ostream = sock.getOutputStream(); 
        PrintWriter pwrite = new PrintWriter(ostream, true);

        // receiving from server ( receiveRead  object)
        InputStream istream = sock.getInputStream();
        BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

        System.out.println("Start the chitchat, type and press Enter key");

        String receiveMessage, sendMessage;               
        while(true)
        {
            sendMessage = keyRead.readLine();     // keyboard reading
            pwrite.println(sendMessage);       // sending to server
            System.out.flush();         // flush the data

            if((receiveMessage = receiveRead.readLine()) != null) //receive from server
            {
                System.out.println(receiveMessage); // displaying at DOS prompt
            }         
        }               
    }          
Run Code Online (Sandbox Code Playgroud)

Boh*_*ian 10

java.io.InputStream.read()是一个阻塞调用,这意味着如果没有数据可用,则线程会停止,直到数据可用.

对于非阻塞I/O,请使用java.nio包中的类.