select - Polling objects for data in Java -
i have many packetconnection
objects connected remote computers. each packetconnection
has thread-safe (synchronized
) read , write methods accept packets
. other classes should not have access enclosed streams.
i connections' owner notified when packetconnection
has data read (it can block until event; spinning cpu in while-loop unwanted). owner ask appropriate object read , return packet
.
what idomatic way accomplish this?
use java nio access "selector" functionality tell sockets have data can read. socketchannel
represents selectable socket. call select()
on selector
determine if of sockets readable; can provide selection time-out if necessary.
Comments
Post a Comment