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

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

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 11
    • None
    • core-libs
    • b18
    • x86
    • windows_xp

        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)
        }

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

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: