SocketChannels NIO

A

Andersen

Say I have a machine with high speed concurrent IO, if I use one thread
which uses NIO (for SocketChannels) with a Selector, would it not impair
the performance? What is the solution?

Can I mix NIO with ordinary blocking calls? Say the selector says there
is data on a channel, then I use blocking IO on that channel in a
different thread?
 
S

Stefan Schulz

I am not entirely sure about this, but i think you need to keep your
channel in nonblocking mode as long as it is registered with a
selector. However, if this really is your bottleneck, you can have one
(or several) threads on standbye, which get handed a channel once it
becomes readable, read as much data as possible, and then return to
standbye mode.
 
E

EJP

Andersen said:
Say I have a machine with high speed concurrent IO, if I use one thread
which uses NIO (for SocketChannels) with a Selector, would it not impair
the performance? What is the solution?

Why would it impair the performance? It would only mean that your
Selector would fire more frequently and the reads and writes would
execute more quickly.
Can I mix NIO with ordinary blocking calls? Say the selector says there
is data on a channel, then I use blocking IO on that channel in a
different thread?

If you deregister the channel from the Selector you can put it into
blocking mode, but then if you really want to have different threads why
would you use NIO and a Selector at all?
 
T

Thomas Hawtin

Stefan said:
I am not entirely sure about this, but i think you need to keep your
channel in nonblocking mode as long as it is registered with a
selector. However, if this really is your bottleneck, you can have one
(or several) threads on standbye, which get handed a channel once it
becomes readable, read as much data as possible, and then return to
standbye mode.

One way of handling this is to copy from the channel into a
PipedOutputStream (or saner equivalent) in your main selector handling
thread. Then have worker thread work as traditional on the matching
InputStream.

This can be particularly effective with tasks such as web serving.
Connections will be idle most of the time. However bursts of activity
occur where traditional I/O may be significantly simpler.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top