1. /*
  2. * @(#)AtomicReference.java 1.5 04/01/12
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent.atomic;
  8. import sun.misc.Unsafe;
  9. /**
  10. * An object reference that may be updated atomically. See the {@link
  11. * java.util.concurrent.atomic} package specification for description
  12. * of the properties of atomic variables.
  13. * @since 1.5
  14. * @author Doug Lea
  15. * @param <V> The type of object referred to by this reference
  16. */
  17. public class AtomicReference<V> implements java.io.Serializable {
  18. private static final long serialVersionUID = -1848883965231344442L;
  19. private static final Unsafe unsafe = Unsafe.getUnsafe();
  20. private static final long valueOffset;
  21. static {
  22. try {
  23. valueOffset = unsafe.objectFieldOffset
  24. (AtomicReference.class.getDeclaredField("value"));
  25. } catch(Exception ex) { throw new Error(ex); }
  26. }
  27. private volatile V value;
  28. /**
  29. * Create a new AtomicReference with the given initial value.
  30. *
  31. * @param initialValue the initial value
  32. */
  33. public AtomicReference(V initialValue) {
  34. value = initialValue;
  35. }
  36. /**
  37. * Create a new AtomicReference with null initial value.
  38. */
  39. public AtomicReference() {
  40. }
  41. /**
  42. * Get the current value.
  43. *
  44. * @return the current value
  45. */
  46. public final V get() {
  47. return value;
  48. }
  49. /**
  50. * Set to the given value.
  51. *
  52. * @param newValue the new value
  53. */
  54. public final void set(V newValue) {
  55. value = newValue;
  56. }
  57. /**
  58. * Atomically set the value to the given updated value
  59. * if the current value <tt>==</tt> the expected value.
  60. * @param expect the expected value
  61. * @param update the new value
  62. * @return true if successful. False return indicates that
  63. * the actual value was not equal to the expected value.
  64. */
  65. public final boolean compareAndSet(V expect, V update) {
  66. return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
  67. }
  68. /**
  69. * Atomically set the value to the given updated value
  70. * if the current value <tt>==</tt> the expected value.
  71. * May fail spuriously.
  72. * @param expect the expected value
  73. * @param update the new value
  74. * @return true if successful.
  75. */
  76. public final boolean weakCompareAndSet(V expect, V update) {
  77. return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
  78. }
  79. /**
  80. * Set to the given value and return the old value.
  81. *
  82. * @param newValue the new value
  83. * @return the previous value
  84. */
  85. public final V getAndSet(V newValue) {
  86. while (true) {
  87. V x = get();
  88. if (compareAndSet(x, newValue))
  89. return x;
  90. }
  91. }
  92. /**
  93. * Returns the String representation of the current value.
  94. * @return the String representation of the current value.
  95. */
  96. public String toString() {
  97. return String.valueOf(get());
  98. }
  99. }