Class SynchronizedRef

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.Object value_
      The maintained reference
    • Constructor Summary

      Constructors 
      Constructor Description
      SynchronizedRef​(java.lang.Object initialValue)
      Create a SynchronizedRef initially holding the given reference and using its own internal lock.
      SynchronizedRef​(java.lang.Object initialValue, java.lang.Object lock)
      Make a new SynchronizedRef with the given initial value, and using the supplied lock.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean commit​(java.lang.Object assumedValue, java.lang.Object newValue)
      Set value to newValue only if it is currently assumedValue.
      java.lang.Object get()
      Return the current value
      java.lang.Object set​(java.lang.Object newValue)
      Set to newValue.
      java.lang.Object swap​(SynchronizedRef other)
      Atomically swap values with another SynchronizedRef.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • value_

        protected java.lang.Object value_
        The maintained reference
    • Constructor Detail

      • SynchronizedRef

        public SynchronizedRef​(java.lang.Object initialValue)
        Create a SynchronizedRef initially holding the given reference and using its own internal lock.
      • SynchronizedRef

        public SynchronizedRef​(java.lang.Object initialValue,
                               java.lang.Object lock)
        Make a new SynchronizedRef with the given initial value, and using the supplied lock.
    • Method Detail

      • get

        public final java.lang.Object get()
        Return the current value
      • set

        public java.lang.Object set​(java.lang.Object newValue)
        Set to newValue.
        Returns:
        the old value
      • commit

        public boolean commit​(java.lang.Object assumedValue,
                              java.lang.Object newValue)
        Set value to newValue only if it is currently assumedValue.
        Returns:
        true if successful
      • swap

        public java.lang.Object swap​(SynchronizedRef other)
        Atomically swap values with another SynchronizedRef. Uses identityHashCode to avoid deadlock when two SynchronizedRefs attempt to simultaneously swap with each other. (Note: Ordering via identyHashCode is not strictly guaranteed by the language specification to return unique, orderable values, but in practice JVMs rely on them being unique.)
        Returns:
        the new value