equal
deleted
inserted
replaced
109 * and does not provide ordering guarantees, so is only rarely an |
109 * and does not provide ordering guarantees, so is only rarely an |
110 * appropriate alternative to {@code compareAndSet}. |
110 * appropriate alternative to {@code compareAndSet}. |
111 * |
111 * |
112 * @param expect the expected value |
112 * @param expect the expected value |
113 * @param update the new value |
113 * @param update the new value |
114 * @return true if successful. |
114 * @return true if successful |
115 */ |
115 */ |
116 public boolean weakCompareAndSet(boolean expect, boolean update) { |
116 public boolean weakCompareAndSet(boolean expect, boolean update) { |
117 int e = expect ? 1 : 0; |
117 int e = expect ? 1 : 0; |
118 int u = update ? 1 : 0; |
118 int u = update ? 1 : 0; |
119 return unsafe.compareAndSwapInt(this, valueOffset, e, u); |
119 return unsafe.compareAndSwapInt(this, valueOffset, e, u); |
144 * |
144 * |
145 * @param newValue the new value |
145 * @param newValue the new value |
146 * @return the previous value |
146 * @return the previous value |
147 */ |
147 */ |
148 public final boolean getAndSet(boolean newValue) { |
148 public final boolean getAndSet(boolean newValue) { |
149 for (;;) { |
149 boolean prev; |
150 boolean current = get(); |
150 do { |
151 if (compareAndSet(current, newValue)) |
151 prev = get(); |
152 return current; |
152 } while (!compareAndSet(prev, newValue)); |
153 } |
153 return prev; |
154 } |
154 } |
155 |
155 |
156 /** |
156 /** |
157 * Returns the String representation of the current value. |
157 * Returns the String representation of the current value. |
158 * @return the String representation of the current value. |
158 * @return the String representation of the current value |
159 */ |
159 */ |
160 public String toString() { |
160 public String toString() { |
161 return Boolean.toString(get()); |
161 return Boolean.toString(get()); |
162 } |
162 } |
163 |
163 |