[quote]Hi,
I have a bunch of threads which can read and write over a TCP/IP stream. Does anyone know whether it is better to get each of them to do asynchronous IO via NIO or poll them by setting a timeout on the socket?
[/quote]
Since you ask (although I’m not sure it’s quite what you wanted), it’s always better to use NIO. In many cases it’s quicker (to write the code) to use IO, but there’s little other reason to use anything but NIO.
You should NEVER EVER EVER poll ANYTHING ANYWHERE if you want to achieve high performance anything. It’s one of the gross generalizations of performance tuning which is nearly always true - along with things like “Writing large programs in Visual Basic will always result in memory hungry and slow beasts”
Well, something like that :).
By definition, polling wastes time - if you are very lucky or have a very specific situation it will be no worse than non-polling, but in general there will be many more polls than you needed to do (note: if you reduce the poll rate, by definition you simultaneously reduce the responsiveness, which is almost always undesirable)
AFAIAA there is no way to do this using standard libs; however, this is based on experimentation long ago in the past, so I may be completely wrong here :).
Why do you want to do a “genuine” poll, though? One of the many good reasons for high-level networking API’s a la java standard libs is that 99.999% of the time you have no use for low-level API’s (network sniffers are the main counter example). The best reason I can come up for wanting access to poll is that you want to benchmark low-level aspects of a system?