JDK-8201496 : Release Note: SelectableChannel.register Can Be Invoked While a Selection Operation Is in Progress
  • Type: Sub-task
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 11
  • Priority: P4
  • Status: Closed
  • Resolution: Delivered
  • Submitted: 2018-04-12
  • Updated: 2019-04-09
  • Resolved: 2018-04-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11
11Resolved
Description
`java.nio.channels.Selector` historically specified its key set (the set containing the keys representing the channels registered with the selector) to not be thread safe. Selection operations were specified to synchronize on this key set. Additionally, the `register` methods defined by `java.nio.channels.SelectableChannel` (`SocketChannel`, `ServerSocketChannel`, ...) were also specified to synchronize on the selector's key set and therefore block if invoked concurrently with another registration or selection operation. 

The specification has changed in Java SE 11 so that the selector's key set is specified to be safe for use by concurrent threads. Selection operations are no longer specified to synchronize on the key set. This allows threads to register channels while a selection operation is in progress; the new registration takes effect at the next selection operation. `SelectionKey` `interestOps(int)` has also been re-specified so that it can be called at any time. If invoked while a selection operation is in progress, then it has no effect upon that operation; the change to the key's interest set will be seen by the next selection operation.

Code that synchronizes on the `Selector`'s selected-key set is not impacted by this change as selection operations continue to be specified to synchronize on the selected-key set.

The `Selector` API is pluggable. `SelectorProvider` implementations that exist outside of the JDK will need to be updated to align their `Selector` implementation with the updated specification.