Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java Selector is asynchronous or non-blocking architecture

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.

like image 698
Trying Avatar asked Jul 12 '13 12:07

Trying


People also ask

What is asynchronous and non-blocking in Java?

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".

Is Java blocking or nonblocking?

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.

What is a Java selector?

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.

What is non-blocking code in Java?

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.


1 Answers

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

  • This is multiplexed I/O: in Java it is used by 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.

  • Now comes Asynchronous I/O. In asynchronous I/O applications return immediately and OS will let them know when the bytes are available for processing. In java it is facilitated by 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.

like image 131
Trying Avatar answered Sep 27 '22 17:09

Trying