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

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

    XMLWordPrintable

Details

    • Enhancement
    • Status: Resolved
    • P4
    • Resolution: Fixed
    • None
    • 11
    • core-libs
    • b18
    • x86
    • 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

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

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: