ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java (file contents):
Revision 1.10 by dl, Wed May 25 14:27:37 2005 UTC vs.
Revision 1.15 by jsr166, Tue Nov 17 06:58:50 2009 UTC

# Line 2 | Line 2
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 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import java.util.concurrent.atomic.*;
10   import junit.framework.*;
11   import java.util.*;
12  
13 < public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase{
13 > public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
14      volatile Integer x = null;
15      Object z;
16      Integer w;
17  
18 <    public static void main(String[] args){
18 >    public static void main(String[] args) {
19          junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
# Line 25 | Line 25 | public class AtomicReferenceFieldUpdater
25      /**
26       * Construction with non-existent field throws RuntimeException
27       */
28 <    public void testConstructor(){
29 <        try{
28 >    public void testConstructor() {
29 >        try {
30              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
31                  a = AtomicReferenceFieldUpdater.newUpdater
32                  (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
# Line 39 | Line 39 | public class AtomicReferenceFieldUpdater
39      /**
40       * construction with field not of given type throws RuntimeException
41       */
42 <    public void testConstructor2(){
43 <        try{
42 >    public void testConstructor2() {
43 >        try {
44              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
45                  a = AtomicReferenceFieldUpdater.newUpdater
46                  (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
# Line 52 | Line 52 | public class AtomicReferenceFieldUpdater
52      /**
53       * Constructor with non-volatile field throws exception
54       */
55 <    public void testConstructor3(){
56 <        try{
55 >    public void testConstructor3() {
56 >        try {
57              AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
58                  a = AtomicReferenceFieldUpdater.newUpdater
59                  (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
# Line 65 | Line 65 | public class AtomicReferenceFieldUpdater
65      /**
66       *  get returns the last value set or assigned
67       */
68 <    public void testGetSet(){
68 >    public void testGetSet() {
69          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
70          try {
71              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 83 | Line 83 | public class AtomicReferenceFieldUpdater
83      /**
84       *  get returns the last value lazySet by same thread
85       */
86 <    public void testGetLazySet(){
86 >    public void testGetLazySet() {
87          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
88          try {
89              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 101 | Line 101 | public class AtomicReferenceFieldUpdater
101      /**
102       * compareAndSet succeeds in changing value if equal to expected else fails
103       */
104 <    public void testCompareAndSet(){
104 >    public void testCompareAndSet() {
105          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
106          try {
107              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 122 | Line 122 | public class AtomicReferenceFieldUpdater
122       * compareAndSet in one thread enables another waiting for value
123       * to succeed
124       */
125 <    public void testCompareAndSetInMultipleThreads() {
125 >    public void testCompareAndSetInMultipleThreads() throws Exception {
126          x = one;
127          final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
128          try {
# Line 131 | Line 131 | public class AtomicReferenceFieldUpdater
131              return;
132          }
133  
134 <        Thread t = new Thread(new Runnable() {
135 <                public void run() {
136 <                    while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield();
137 <                }});
138 <        try {
139 <            t.start();
140 <            assertTrue(a.compareAndSet(this, one, two));
141 <            t.join(LONG_DELAY_MS);
142 <            assertFalse(t.isAlive());
143 <            assertEquals(a.get(this), three);
144 <        }
145 <        catch(Exception e) {
146 <            unexpectedException();
147 <        }
134 >        Thread t = new Thread(new CheckedRunnable() {
135 >            public void realRun() {
136 >                while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
137 >                    Thread.yield();
138 >            }});
139 >
140 >        t.start();
141 >        assertTrue(a.compareAndSet(this, one, two));
142 >        t.join(LONG_DELAY_MS);
143 >        assertFalse(t.isAlive());
144 >        assertEquals(a.get(this), three);
145      }
146  
147      /**
148       * repeated weakCompareAndSet succeeds in changing value when equal
149 <     * to expected
149 >     * to expected
150       */
151 <    public void testWeakCompareAndSet(){
151 >    public void testWeakCompareAndSet() {
152          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
153          try {
154              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
# Line 159 | Line 156 | public class AtomicReferenceFieldUpdater
156              return;
157          }
158          x = one;
159 <        while(!a.weakCompareAndSet(this,one,two));
160 <        while(!a.weakCompareAndSet(this,two,m4));
159 >        while (!a.weakCompareAndSet(this,one,two));
160 >        while (!a.weakCompareAndSet(this,two,m4));
161          assertEquals(m4,a.get(this));
162 <        while(!a.weakCompareAndSet(this,m4,seven));
162 >        while (!a.weakCompareAndSet(this,m4,seven));
163          assertEquals(seven,a.get(this));
164      }
165  
166      /**
167       * getAndSet returns previous value and sets to given value
168       */
169 <    public void testGetAndSet(){
169 >    public void testGetAndSet() {
170          AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
171          try {
172              a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines