|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AtomicBoolean | Line # 20 | 16 | 11 | 95.1% |
0.9512195
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (14) | |||
| Result | |||
|
0.34146342
|
AtomicBooleanTest.testCompareAndSet
AtomicBooleanTest.testCompareAndSet
|
1 PASS | |
|
0.29268292
|
AtomicBooleanTest.testWeakCompareAndSet
AtomicBooleanTest.testWeakCompareAndSet
|
1 PASS | |
|
0.2682927
|
AtomicBooleanTest.testGetAndSet
AtomicBooleanTest.testGetAndSet
|
1 PASS | |
|
0.24390244
|
AtomicBooleanTest.testCompareAndSetInMultipleThreads
AtomicBooleanTest.testCompareAndSetInMultipleThreads
|
1 PASS | |
|
0.2195122
|
AtomicBooleanTest.testGetLazySet
AtomicBooleanTest.testGetLazySet
|
1 PASS | |
|
0.2195122
|
AtomicBooleanTest.testGetSet
AtomicBooleanTest.testGetSet
|
1 PASS | |
|
0.19512194
|
ExecutorCompletionServiceTest.testNewTaskForCallable
ExecutorCompletionServiceTest.testNewTaskForCallable
|
1 PASS | |
|
0.19512194
|
AtomicBooleanTest.testToString
AtomicBooleanTest.testToString
|
1 PASS | |
|
0.19512194
|
ExecutorCompletionServiceTest.testNewTaskForRunnable
ExecutorCompletionServiceTest.testNewTaskForRunnable
|
1 PASS | |
|
0.14634146
|
AtomicBooleanTest.testSerialization
AtomicBooleanTest.testSerialization
|
1 PASS | |
|
0.14634146
|
CyclicBarrierTest.testReset_Leakage
CyclicBarrierTest.testReset_Leakage
|
1 PASS | |
|
0.12195122
|
AtomicBooleanTest.testConstructor
AtomicBooleanTest.testConstructor
|
1 PASS | |
|
0.07317073
|
AtomicBooleanTest.testConstructor2
AtomicBooleanTest.testConstructor2
|
1 PASS | |
|
0.048780486
|
CyclicBarrierTest.testResetWithoutBreakage
CyclicBarrierTest.testResetWithoutBreakage
|
1 PASS | |
| 1 | /* | |
| 2 | * Written by Doug Lea with assistance from members of JCP JSR-166 | |
| 3 | * Expert Group and released to the public domain, as explained at | |
| 4 | * http://creativecommons.org/licenses/publicdomain | |
| 5 | */ | |
| 6 | ||
| 7 | package edu.emory.mathcs.backport.java.util.concurrent.atomic; | |
| 8 | ||
| 9 | /** | |
| 10 | * A {@code boolean} value that may be updated atomically. See the | |
| 11 | * {@link edu.emory.mathcs.backport.java.util.concurrent.atomic} package specification for | |
| 12 | * description of the properties of atomic variables. An | |
| 13 | * {@code AtomicBoolean} is used in applications such as atomically | |
| 14 | * updated flags, and cannot be used as a replacement for a | |
| 15 | * {@link java.lang.Boolean}. | |
| 16 | * | |
| 17 | * @since 1.5 | |
| 18 | * @author Doug Lea | |
| 19 | */ | |
| 20 | public class AtomicBoolean implements java.io.Serializable { | |
| 21 | private static final long serialVersionUID = 4654671469794556979L; | |
| 22 | ||
| 23 | private volatile int value; | |
| 24 | ||
| 25 | /** | |
| 26 | * Creates a new {@code AtomicBoolean} with the given initial value. | |
| 27 | * | |
| 28 | * @param initialValue the initial value | |
| 29 | */ | |
| 30 | 9 |
public AtomicBoolean(boolean initialValue) { |
| 31 | 9 | value = initialValue ? 1 : 0; |
| 32 | } | |
| 33 | ||
| 34 | /** | |
| 35 | * Creates a new {@code AtomicBoolean} with initial value {@code false}. | |
| 36 | */ | |
| 37 | 4 |
public AtomicBoolean() { |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * Returns the current value. | |
| 42 | * | |
| 43 | * @return the current value | |
| 44 | */ | |
| 45 | 27 |
public final boolean get() { |
| 46 | 27 | return value != 0; |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Atomically sets the value to the given updated value | |
| 51 | * if the current value {@code ==} the expected value. | |
| 52 | * | |
| 53 | * @param expect the expected value | |
| 54 | * @param update the new value | |
| 55 | * @return true if successful. False return indicates that | |
| 56 | * the actual value was not equal to the expected value. | |
| 57 | */ | |
| 58 | 6 |
public final synchronized boolean compareAndSet(boolean expect, boolean update) { |
| 59 | 6 | if (expect == (value != 0)) { |
| 60 | 5 | value = update ? 1 : 0; |
| 61 | 5 | return true; |
| 62 | } | |
| 63 | else { | |
| 64 | 1 | return false; |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Atomically sets the value to the given updated value | |
| 70 | * if the current value {@code ==} the expected value. | |
| 71 | * | |
| 72 | * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> | |
| 73 | * and does not provide ordering guarantees, so is only rarely an | |
| 74 | * appropriate alternative to {@code compareAndSet}. | |
| 75 | * | |
| 76 | * @param expect the expected value | |
| 77 | * @param update the new value | |
| 78 | * @return true if successful. | |
| 79 | */ | |
| 80 | 3 |
public synchronized boolean weakCompareAndSet(boolean expect, boolean update) { |
| 81 | 3 | if (expect == (value != 0)) { |
| 82 | 3 | value = update ? 1 : 0; |
| 83 | 3 | return true; |
| 84 | } | |
| 85 | else { | |
| 86 | 0 | return false; |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Unconditionally sets to the given value. | |
| 92 | * | |
| 93 | * @param newValue the new value | |
| 94 | */ | |
| 95 | 7 |
public final synchronized void set(boolean newValue) { |
| 96 | 7 | value = newValue ? 1 : 0; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Eventually sets to the given value. | |
| 101 | * | |
| 102 | * @param newValue the new value | |
| 103 | * @since 1.6 | |
| 104 | */ | |
| 105 | 2 |
public final synchronized void lazySet(boolean newValue) { |
| 106 | 2 | value = newValue ? 1 : 0; |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Atomically sets to the given value and returns the previous value. | |
| 111 | * | |
| 112 | * @param newValue the new value | |
| 113 | * @return the previous value | |
| 114 | */ | |
| 115 | 3 |
public final synchronized boolean getAndSet(boolean newValue) { |
| 116 | 3 | int old = value; |
| 117 | 3 | value = newValue ? 1 : 0; |
| 118 | 3 | return old != 0; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Returns the String representation of the current value. | |
| 123 | * @return the String representation of the current value. | |
| 124 | */ | |
| 125 | 2 |
public String toString() { |
| 126 | 2 | return Boolean.toString(get()); |
| 127 | } | |
| 128 | ||
| 129 | } | |
|
||||||||||