Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is non blocking in nio  RSS feed

 
Parmeet Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I gone through the java nio tutorial, i know nio work fast becaouse it doesnt copy file into the jvm.

but what i didnt get is..

Why nio also refer as non blocking io..

and what used to be blocked in conventional IOs.


Please assist..
thanks in advance.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gone through the java nio tutorial, i know nio work fast becaouse it doesnt copy file into the jvm.


I am assuming you are refering to the file channel.

Why nio also refer as non blocking io..

and what used to be blocked in conventional IOs.


Take a look at the NIO socket channels. And the selector which manages access to them.

Henry
 
Parmeet Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the java doc Selector class

A selectable channel is either in blocking mode or in non-blocking mode. In blocking mode, every I/O operation invoked upon the channel will block until it completes. In non-blocking mode an I/O operation will never block and may transfer fewer bytes than were requested or possibly no bytes at all. The blocking mode of a selectable channel may be determined by invoking its isBlocking method.

what does it mean when it says "I/O operation invoked upon the channel will block until it completes"

Please assist
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Parmeet]: what does it mean when it says "I/O operation invoked upon the channel will block until it completes"

The method will simply pause and not do anything until it can complete - until it has data to return. Other threads will be able to continue working, but the blocking thread will not be able to do anything until it completes.
 
Maris Orbidans
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you try to read 100 bytes from a socket. In blocking IO method invocation would block until all 100 bytes have been read. In case of non-blocking IO method would return immediately even if it has not read all 100 bytes.
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maris Orbidans:
In case of non-blocking IO method would return immediately even if it has not read all 100 bytes.


Actually, this is the case with the regular IO classes (see our FAQ entry, ReadDoesntDoWhatYouThinkItDoes

Non-blocking IO means that a read doesn't block, or wait for input. In the example above, the read blocks until it gets some input, not necessarily all 100 bytes. Using non-blocking IO, the read will return immediately even if there's no input to read.
Non-blocking is important for applications like servers. If you use blocking IO, you have to create a thread per connection because each thread will block reading the connection. Since there is a finite number of threads an application can spawn, this is not very scalable.
With non-blocking IO the application can have a pool of threads which poll connections until there's data to read. Since the number of threads is not directly tied to the number of connections, this application will scale better than the blocking IO version.
Here is a more in-depth article on the subject.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Maris Orbidans]: In case of non-blocking IO method would return immediately even if it has not read all 100 bytes.

[Joe Ess]: Actually, this is the case with the regular IO classes (see our FAQ entry, ReadDoesntDoWhatYouThinkItDoes


I disagree with this last sentence as I understand it. Since I agree with everything else Joe wrote above, I think this is just a minor misunderstanding by one of us. Regular IO classes (non-NIO) always block until at least one byte can be read. The don't necessarily block until all requested bytes have been read, but they do block until at least one byte can be read. That's true for both regular IO classes, and blocking NIO classes, as Joe said above. The part I'm adding is that reguilar IO behaves as blocking IO, not non-blocking IO.
[ April 14, 2008: Message edited by: Jim Yingst ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!