--- jsr166/src/test/tck/AtomicStampedReferenceTest.java 2003/09/20 18:20:07 1.3 +++ jsr166/src/test/tck/AtomicStampedReferenceTest.java 2003/09/25 11:02:41 1.4 @@ -17,7 +17,7 @@ public class AtomicStampedReferenceTest } /** - * + * constructeor initializes to given reference and stamp */ public void testConstructor(){ AtomicStampedReference ai = new AtomicStampedReference(one, 0); @@ -30,7 +30,7 @@ public class AtomicStampedReferenceTest } /** - * + * get returns the last values of reference and stamp set */ public void testGetSet(){ int[] mark = new int[1]; @@ -52,7 +52,7 @@ public class AtomicStampedReferenceTest } /** - * + * attemptStamp succeeds in single thread */ public void testAttemptStamp(){ int[] mark = new int[1]; @@ -65,7 +65,8 @@ public class AtomicStampedReferenceTest } /** - * + * compareAndSet succeeds in changing values if equal to expected reference + * and stamp else fails */ public void testCompareAndSet(){ int[] mark = new int[1]; @@ -88,7 +89,54 @@ public class AtomicStampedReferenceTest } /** - * + * compareAndSet in one thread enables another waiting for reference value + * to succeed + */ + public void testCompareAndSetInMultipleThreads() { + final AtomicStampedReference ai = new AtomicStampedReference(one, 0); + Thread t = new Thread(new Runnable() { + public void run() { + while(!ai.compareAndSet(two, three, 0, 0)) Thread.yield(); + }}); + try { + t.start(); + assertTrue(ai.compareAndSet(one, two, 0, 0)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(ai.getReference(), three); + assertEquals(ai.getStamp(), 0); + } + catch(Exception e) { + unexpectedException(); + } + } + + /** + * compareAndSet in one thread enables another waiting for stamp value + * to succeed + */ + public void testCompareAndSetInMultipleThreads2() { + final AtomicStampedReference ai = new AtomicStampedReference(one, 0); + Thread t = new Thread(new Runnable() { + public void run() { + while(!ai.compareAndSet(one, one, 1, 2)) Thread.yield(); + }}); + try { + t.start(); + assertTrue(ai.compareAndSet(one, one, 0, 1)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(ai.getReference(), one); + assertEquals(ai.getStamp(), 2); + } + catch(Exception e) { + unexpectedException(); + } + } + + /** + * repeated weakCompareAndSet succeeds in changing values when equal + * to expected */ public void testWeakCompareAndSet(){ int[] mark = new int[1];