For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2005 > NIO or IO?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author NIO or IO?
Jeffrey Spoon

2005-01-27, 4:03 pm




Hello. I'm thinking of implementing a simple (to start with) P2P system.
The test system will be pretty simple but I was wondering whether I
should use IO or NIO. The thing is it would have to be scalable as I
envisage quite a high number of nodes. Would NIO be worth the hassle, or
should I stick with IO for now? I have some knowledge of IO but none of
NIO. Are the performance advantages of NIO that good? As far as I know
NIO is much better at multiple connections since you use channels rather
than sockets, and switch between channels, thus saving threads. For IO I
take it I would simply have to use multiple threads for each connection?
That could get quite heavy on the CPU, I suspect. But I know IO is going
to be a lot easier to use. Any suggestions?




--
Jeffrey Spoon

Stefan Schulz

2005-01-27, 4:03 pm

On Thu, 27 Jan 2005 16:36:36 +0000, Jeffrey Spoon wrote:

> Hello. I'm thinking of implementing a simple (to start with) P2P system.
> The test system will be pretty simple but I was wondering whether I
> should use IO or NIO. The thing is it would have to be scalable as I
> envisage quite a high number of nodes. Would NIO be worth the hassle, or
> should I stick with IO for now? I have some knowledge of IO but none of
> NIO. Are the performance advantages of NIO that good? As far as I know
> NIO is much better at multiple connections since you use channels rather
> than sockets, and switch between channels, thus saving threads. For IO I
> take it I would simply have to use multiple threads for each connection?
> That could get quite heavy on the CPU, I suspect. But I know IO is going
> to be a lot easier to use. Any suggestions?


Take the best of both worlds, as always. *G* Create channels, but return
to Reader/Writer IO as soon as you can (right after your Selector has
given you the "can be read" notification). The java.nio.Channels class
contains a lot of useful helper methods for doing just that.

Also, be sure to _fully_ read the documentation for Selector. That class
has a lot of non-obvious behaviour... Luckily it's all documented. Be
smart, learn the quick way, instead of spending half a dozen hours finding
out about wakeup() (like i did).

--
In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
--- Rear Admiral Grace Murray Hopper

Casey Hawthorne

2005-01-27, 4:03 pm

Make the I/O transparent to your program (i.e. Java interface) so that
you can start with the usual I/O routines and test the functionality
of your app and then switch to NIO without changing all your existing
code!


--
Regards,
Casey
Jeffrey Spoon

2005-01-27, 8:59 pm

In message <pan.2005.01.27.17.24.12.77987@spacetime.de>, Stefan Schulz
<terra@spacetime.de> writes

>
>Take the best of both worlds, as always. *G* Create channels, but return
>to Reader/Writer IO as soon as you can (right after your Selector has
>given you the "can be read" notification). The java.nio.Channels class
>contains a lot of useful helper methods for doing just that.
>


Ok, I'll try that.

>Also, be sure to _fully_ read the documentation for Selector. That class
>has a lot of non-obvious behaviour... Luckily it's all documented. Be
>smart, learn the quick way, instead of spending half a dozen hours finding
>out about wakeup() (like i did).
>


I was a bit worried about the fact there was no tutorial, but there are
a few examples so I'll have a look at them. Thanks for the ideas.




--
Jeffrey Spoon

Jeffrey Spoon

2005-01-27, 8:59 pm

In message <hveiv0p12bc0q2g95dptc6r28ok83jbotf@4ax.com>, Casey Hawthorne
<caseyhHAMMER_TIME@istar.ca> writes
>Make the I/O transparent to your program (i.e. Java interface) so that
>you can start with the usual I/O routines and test the functionality
>of your app and then switch to NIO without changing all your existing
>code!
>
>
>--
>Regards,
>Casey


Now that's a good idea. Cheers.



--
Jeffrey Spoon

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com