View Single Post
  #7 (permalink)  
Old June 1st, 2006
sberlin sberlin is offline
Software Developer
 
Join Date: November 4th, 2002
Location: New York
Posts: 1,366
sberlin is flying high
Default

stief asked me to elaborate on NIO, so here's an elaboration that I tend to use when explaining what I do all day to friends and family (while I'm working on NIO things)...

Java's primary means of sending and receiving data from the network is using "streams". Streams are very basic ways of saying, "I want to write X" and "I want to read Y". A write to a stream doesn't finish until all of it is written, and there's no guarantee a read will finish at any time. So, you need to create a "Thread" to do these reads and writes. This is kind of like holding a conversation -- you need to devote a specific resource to maintaining the conversation with a person. Otherwise, if you tried to converse with many people at once, someone may experience a long delay between topics, as you were engrossed with someone else.

Newer versions of java introduced NIO -- new I/O. This allowed developers to write non-blocking I/O, meaning that you can just attempt to write or read, and it will only write or read as much as can be done right then. It also lets you know when something has room for more writing or has data that can be read. This lets you change the way you do network input & output so that you don't need to devote a specific resource to each conversation. Instead, you have a single thing that keeps track of all conversations you're currently in. When a conversation has something you can read, it informs anyone interested and they read as much as they can. The same thing with writing, connecting, and 'accepting' (being told someone wants to start talking with you).

The major difference is that before NIO, LimeWire required 2 threads for each Gnutella connection (one thread for reading, one thread for writing), so each Ultrapeer had at the very least 120 threads. It also required one thread for each download source, another thread for processing each new incoming connection, and another thread for making a new outgoing connection. With NIO, this is all reduced to a single thread.

Threads are a big deal because they waste resources on your computer. They're also a big deal because switching between them (what's known as a context-switch) is time-consuming.
Reply With Quote