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

Comparing jsr166/src/test/tck/CyclicBarrierTest.java (file contents):
Revision 1.21 by jsr166, Sat May 28 14:52:11 2011 UTC vs.
Revision 1.24 by jsr166, Tue May 31 16:16:23 2011 UTC

# Line 8 | Line 8
8  
9   import junit.framework.*;
10   import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.util.concurrent.locks.*;
13 < import java.util.concurrent.atomic.*;
11 > import java.util.concurrent.BrokenBarrierException;
12 > import java.util.concurrent.CountDownLatch;
13 > import java.util.concurrent.CyclicBarrier;
14 > import java.util.concurrent.TimeoutException;
15 > import java.util.concurrent.atomic.AtomicBoolean;
16   import static java.util.concurrent.TimeUnit.MILLISECONDS;
17  
18   public class CyclicBarrierTest extends JSR166TestCase {
# Line 27 | Line 29 | public class CyclicBarrierTest extends J
29      }
30  
31      /**
32 +     * Spin-waits till the number of waiters == numberOfWaiters.
33 +     */
34 +    void awaitNumberWaiting(CyclicBarrier barrier, int numberOfWaiters) {
35 +        long startTime = System.nanoTime();
36 +        while (barrier.getNumberWaiting() != numberOfWaiters) {
37 +            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
38 +                fail("timed out");
39 +            Thread.yield();
40 +        }
41 +    }
42 +
43 +    /**
44       * Creating with negative parties throws IAE
45       */
46      public void testConstructor1() {
# Line 186 | Line 200 | public class CyclicBarrierTest extends J
200              }});
201          Thread t2 = newStartedThread(new CheckedRunnable() {
202              public void realRun() throws Exception {
203 <                while (c.getNumberWaiting() == 0)
190 <                    Thread.yield();
203 >                awaitNumberWaiting(c, 1);
204                  long startTime = System.nanoTime();
205                  try {
206                      c.await(timeoutMillis(), MILLISECONDS);
# Line 215 | Line 228 | public class CyclicBarrierTest extends J
228              }});
229          Thread t2 = newStartedThread(new CheckedRunnable() {
230              public void realRun() throws Exception {
231 <                while (c.getNumberWaiting() == 0)
219 <                    Thread.yield();
231 >                awaitNumberWaiting(c, 1);
232                  long startTime = System.nanoTime();
233                  try {
234                      c.await(timeoutMillis(), MILLISECONDS);
# Line 250 | Line 262 | public class CyclicBarrierTest extends J
262          t1.start();
263          t2.start();
264          await(pleaseReset);
265 +
266 +        awaitNumberWaiting(c, 2);
267          c.reset();
268          awaitTermination(t1);
269          awaitTermination(t2);
# Line 386 | Line 400 | public class CyclicBarrierTest extends J
400                  }});
401              Thread t2 = newStartedThread(new CheckedRunnable() {
402                  public void realRun() throws Exception {
403 <                    while (barrier.getNumberWaiting() == 0)
390 <                        Thread.yield();
403 >                    awaitNumberWaiting(barrier, 1);
404                      long startTime = System.nanoTime();
405                      try {
406                          barrier.await(timeoutMillis(), MILLISECONDS);
# Line 432 | Line 445 | public class CyclicBarrierTest extends J
445              t1.start();
446              t2.start();
447              start.await();
448 <            while (barrier.getNumberWaiting() < 2) { Thread.yield(); }
448 >            awaitNumberWaiting(barrier, 2);
449              try {
450                  barrier.await();
451                  shouldThrow();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines