I have many
PacketConnection objects connected to remote computers. Each
PacketConnection has thread-safe (
synchronized) read and write methods that accept
Packets. Other classes should not have access to the enclosed streams.
I would like the connections' owner to be notified when a
PacketConnection has data to read (it can block until an event; spinning the CPU in a while-loop is obviously unwanted). The owner would then ask the appropriate object to read and return a
What is the most idomatic way to accomplish this?
Use Java NIO to access the "selector" functionality to tell you which sockets have data that you can read.
SocketChannel represents a selectable Socket. You then call
select() on the
Selector to determine if any of the sockets are readable; you can provide a selection time-out if necessary.