For me below are the most probable definitions for asynchronous and non-blocking I/O:
Asynchronous I/O:
In asynchronous I/O applications return immediately and OS will let them know when the bytes are available for processing.
NON-blocking I/O:
Here application returns immediately what ever data available and application should have polling mechanism to find out when more data is ready.
After knowing these definitions if we analyze java channels i.e. SocketChannel
, ServerSocketChannel
, DatagramSocketChannel
then we can find that these channels can be used as blocking or non-blocking mode by the method configureBlocking(boolean block)
. and assume that we are using them as non-blocking mode. So here comes the questions:
if i will use Selector
i.e. register channels to a selector
whether it is Asynchronous I/O or NON-blocking I/O?
I feel this is Asynchronous I/O in java if and only if underlying operating system is informing the java application about readiness selection of a channel. Else it is non-blocking I/O and selector
is just a mechanism which helps us polling the above mention channels as i mention in the definition. Which is correct? Thanks in advance.
EDIT:
I have answered one part of the question i.e. types of I/O and how java facilitates these functionality.
But one question still remains whether all these functionalities are provides by java is simulated at java layer or it uses underlaying OS to facilitate? Assume the underlaying OS has all the support for these functionalities.
Please refer to the answer.
2.1 Java thread status It is generally called "synchronization" or "blocking"; if multiple tasks can be performed at the same time, there is no constraint between them, and there is no need to wait for each other. It is called "asynchronous" or "non-blocking".
Non-blocking I/O. Blocking IO wait for the data to be write or read before returning. Java IO's various streams are blocking. It means when the thread invoke a write() or read(), then the thread is blocked until there is some data available for read, or the data is fully written.
Last update: 2021-03-10. The Java NIO Selector is a component which can examine one or more Java NIO Channel instances, and determine which channels are ready for e.g. reading or writing. This way a single thread can manage multiple channels, and thus multiple network connections.
A Non-Blocking server means that it is able to have multiple requests in progress at the same time by the same process or thread because it uses Non-Blocking I/O. In the Non-Blocking approach – one thread can handle multiple queries at a time.
I thought of answering my question by doing some more homework. This post will also help in understanding the I/O concepts w.r.t. underlying OS.
This is blocking I/O: FileInputStream
, FileOutputStream
and even reading to or writing from Socket come under this category
This is non-blocking I/O: this is used by Socket Channels like ServerSocketchannel
, SocketChannel
, DatagramChannel
in Java
Selector
to handle multiple channels and these channels should be non-blocking
by nature. So Socket channels can be registered to Selector
and Selector
can manage by I/O multiplexing facility of underlaying OS.AsynchronousSocketChannel
, AsynchronousServerSocketChannel
, AsynchronousFileChannel
.For these above functionalities java uses underlying OS heavily. This is evident when i was going through the book . Here in chapter 4 the author mentions that
True readiness selection must be done by the operating system. One of the most important functions performed by an operating system is to handle I/O requests and notify processes when their data is ready. So it only makes sense to delegate this function down to the operating system. The Selector class provides the abstraction by which Java code can request readiness selection service from the underlying operating system in a portable way.
Hence it's clear that Java uses underlying OS heavily for these features.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With