Summary
Change java.nio.channels.Selector
so that selection operations (select
and selectNow
methods) do not synchronize on the selector's key set. Change the java.nio.channels.SelectableChannel
register
and java.nio.channels.SelectionKey
interestOps
methods so that they may be called at any time.
Problem
Selection operations are specified to synchronize on the selector, its key set, and on its selected-key set (in that order). New registrations are specified to synchronize on the selector's key set and block if a selection operation is in progress. Changes to the interest set of channels registered with a selector may or may not block (it's implementation specific, the JDK implementations have historically not blocked if there is a selection operation in progress, at least not in recent releases).
Registering new channels or changing the interest set of existing registrations is problematic and a long standing bugbear for high performance libraries as it requires using a "gate" object and additional synchronization in order to coordinate with threads doing selection operations.
Solution
The proposed solution is to re-specify the Selector key-set to be safe for use by concurrent threads so that selection operations and new registrations do not synchronize on the key-set. Selection operations will continue to specify that they synchronize on the selector and its selected-key set (also its cancelled-key set during the cancellation phases of selection).
Additionally, the SelectionKey
interestOps()
and interestOps(int)
methods are re-specified so that they can be invoked at any time without concern that they may block indefinitely. This part of the solution aligns the spec with the behavior of recent releases.
If the proposal is accepted, it means that new registrations, changes to interest operations, or cancellations can be coordinated with concurrent selection operations in a relatively simple and consistent manner (i.e. register
followed by wakeup
, interestOps(ops)
followed by wakeup
, cancel
followed by wakeup
).
Specification
The specdiffs with the proposed changes are attached.
The change is significant and therefore worthy of a release note.
- csr of
-
JDK-8201315 (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
-
- Resolved
-