How can you make BufferedReader.readLine () not hang?

Is there a way to make BufferedReader.readLine()non-freezing?

I create a server that:

  • Checks if the client has completed any input.
  • If not, it executes a different code and, in the end, returns to checking the client for input.

How to check if the client performed any input without starting readLine()? If I run readLine(), does the thread freeze until input is delivered?

+5
source share
1 answer

You can use BufferedReader.ready(), for example:

BufferedReader b = new BufferedReader(); //Initialize your BufferedReader in the way you have been doing before, not like this.

if(b.ready()){
    String input = b.readLine();
}

ready()will return trueif the input source does not put anything in a stream that has not been read.

: , true , . read(), , , .

: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#ready()

+8

All Articles