Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java not do things asynchronously?

I am trying to learn Java, I was reading a tutorial that said something like this:

while (N <= 0) {
           TextIO.put("The starting point must be positive. Please try again: ");
           N = TextIO.getlnInt();
        }

It seems like when you ask the user for input it suspends until the response is recieved? I am used to these things happening asynchronously. Is this normal for Java? or can you do both?

Thanks.

like image 949
JD Isaacks Avatar asked Feb 20 '26 11:02

JD Isaacks


1 Answers

I'm not familiar with that TextIO library, but when calling InputStream.read(), i.e. when using System.in.read(), it will block until input data is available. This makes it synchronous.

You can avoid this (i.e. make it asynchronous) by using another thread to capture input.

like image 70
Peter Avatar answered Feb 21 '26 23:02

Peter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!