Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

What is non blocking in nio

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic