--- jsr166/src/test/tck/AtomicIntegerTest.java 2003/09/20 18:20:07 1.4 +++ jsr166/src/test/tck/AtomicIntegerTest.java 2003/09/25 11:02:41 1.5 @@ -18,7 +18,7 @@ public class AtomicIntegerTest extends J } /** - * + * constructor initializes to given value */ public void testConstructor(){ AtomicInteger ai = new AtomicInteger(1); @@ -26,7 +26,7 @@ public class AtomicIntegerTest extends J } /** - * + * default constructed intializes to zero */ public void testConstructor2(){ AtomicInteger ai = new AtomicInteger(); @@ -34,7 +34,7 @@ public class AtomicIntegerTest extends J } /** - * + * get returns the last value set */ public void testGetSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -46,7 +46,7 @@ public class AtomicIntegerTest extends J } /** - * + * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -60,7 +60,30 @@ public class AtomicIntegerTest extends J } /** - * + * compareAndSet in one thread enables another waiting for value + * to succeed + */ + public void testCompareAndSetInMultipleThreads() { + final AtomicInteger ai = new AtomicInteger(1); + Thread t = new Thread(new Runnable() { + public void run() { + while(!ai.compareAndSet(2, 3)) Thread.yield(); + }}); + try { + t.start(); + assertTrue(ai.compareAndSet(1, 2)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(ai.get(), 3); + } + catch(Exception e) { + unexpectedException(); + } + } + + /** + * repeated weakCompareAndSet succeeds in changing value when equal + * to expected */ public void testWeakCompareAndSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -72,7 +95,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndSet returns previous value and sets to given value */ public void testGetAndSet(){ AtomicInteger ai = new AtomicInteger(1); @@ -82,7 +105,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndAdd returns previous value and adds given value */ public void testGetAndAdd(){ AtomicInteger ai = new AtomicInteger(1); @@ -93,7 +116,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndDecrement returns previous value and decrements */ public void testGetAndDecrement(){ AtomicInteger ai = new AtomicInteger(1); @@ -103,7 +126,7 @@ public class AtomicIntegerTest extends J } /** - * + * getAndIncrement returns previous value and increments */ public void testGetAndIncrement(){ AtomicInteger ai = new AtomicInteger(1); @@ -117,7 +140,7 @@ public class AtomicIntegerTest extends J } /** - * + * addAndGet adds given value to current, and returns current value */ public void testAddAndGet(){ AtomicInteger ai = new AtomicInteger(1); @@ -128,7 +151,7 @@ public class AtomicIntegerTest extends J } /** - * + * decrementAndGet decrements and returns current value */ public void testDecrementAndGet(){ AtomicInteger ai = new AtomicInteger(1); @@ -139,7 +162,7 @@ public class AtomicIntegerTest extends J } /** - * + * incrementAndGet increments and returns current value */ public void testIncrementAndGet(){ AtomicInteger ai = new AtomicInteger(1); @@ -153,7 +176,7 @@ public class AtomicIntegerTest extends J } /** - * + * a deserialized serialized atomic holds same value */ public void testSerialization() { AtomicInteger l = new AtomicInteger();