Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6350055

(se) SelectionKey.interestOps variants to atomically update interest ops

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b18
    • CPU:
      x86
    • OS:
      windows_xp

      Backports

        Description

        A DESCRIPTION OF THE REQUEST :
        This is just a request for either API or documentation enhancement.
        In my NIO application, threads often manipulate SelectionKey's interestOps. For example, to clear WRITE interest op, you need to invoke
        key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE)
        And this is the place where race problem can occur: if another thread updates interestOps between reading them and setting them.

        I don't know if this this is common pitfall, but because interestOps(int) must synchronize somehow, there can be 2 methods to clear interest op:
        key.clearInterestOp(int)
        and to add interest op:
        key.addInterestOp(int)
        which will perform reading and writing interest ops atomically.

        Or API documentation can be updated with a note showing this pitfall.

        JUSTIFICATION :
        Spare time of others.


        CUSTOMER SUBMITTED WORKAROUND :
        For clearing:
        synchronized (key) {
            key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE)
        }

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  alanb Alan Bateman
                  Reporter:
                  gmanwanisunw Girish Manwani (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  1 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: