permaculture playing cards
The moose likes I/O and Streams and the fly likes Non blocking socket in NIO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Non blocking socket in NIO" Watch "Non blocking socket in NIO" New topic

Non blocking socket in NIO

ParagS Kulkarni
Ranch Hand

Joined: Oct 04, 2004
Posts: 60
what is the concept behind non blocking sockets in NIO package of java?

Thanks, Parag
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to IO and Streams...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You can find great tutorials on using NIO by searching Google. I did one from IBM's DeveloperWorks, and it was pretty complete.

The main concept is that non-blocking sockets don't block when no data is available to read or the buffer is too full to write. That's it in a nut shell.

Normally, if you have a server that handles multiple requests simultaneously, with blocking I/O you're forced to use a separate thread for each request. If you want to handle 1000 clients at once, you need 1000 threads. Since the majority of time the threads are blocked waiting on I/O operations, this is a huge waste of resources.

With non-blocking I/O, you can have one thread handle all the requests. The thread loops checking a bunch of sockets at once for available data rather than blocking on a single socket.
I agree. Here's the link:
subject: Non blocking socket in NIO
It's not a secret anymore!