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

Comparing jsr166/src/test/loops/CancelledLockLoops.java (file contents):
Revision 1.3 by jsr166, Thu Jan 22 19:39:18 2009 UTC vs.
Revision 1.11 by jsr166, Sat Dec 31 18:54:28 2016 UTC

# Line 1 | Line 1
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import java.util.concurrent.*;
8 < import java.util.concurrent.locks.*;
9 < import java.util.*;
7 > import java.util.Arrays;
8 > import java.util.Collections;
9 > import java.util.Random;
10 > import java.util.concurrent.BrokenBarrierException;
11 > import java.util.concurrent.CyclicBarrier;
12 > import java.util.concurrent.locks.ReentrantLock;
13  
14   public final class CancelledLockLoops {
15      static final Random rng = new Random();
16      static boolean print = false;
17      static final int ITERS = 10000000;
18 <    static final long TIMEOUT = 100;
18 >    static final long TIMEOUT = 100;
19  
20      public static void main(String[] args) throws Exception {
21          int maxThreads = 100;
22 <        if (args.length > 0)
22 >        if (args.length > 0)
23              maxThreads = Integer.parseInt(args[0]);
24  
25          print = true;
# Line 26 | Line 29 | public final class CancelledLockLoops {
29              try {
30                  new ReentrantLockLoop(i).test();
31              }
32 <            catch(BrokenBarrierException bb) {
32 >            catch (BrokenBarrierException bb) {
33                  // OK, ignore
34              }
35          }
# Line 47 | Line 50 | public final class CancelledLockLoops {
50  
51          final void test() throws Exception {
52              Thread[] threads = new Thread[nthreads];
53 <            for (int i = 0; i < threads.length; ++i)
53 >            for (int i = 0; i < threads.length; ++i)
54                  threads[i] = new Thread(this);
55 <            for (int i = 0; i < threads.length; ++i)
55 >            for (int i = 0; i < threads.length; ++i)
56                  threads[i].start();
57 <            Thread[] cancels = (Thread[]) (threads.clone());
57 >            Thread[] cancels = threads.clone();
58              Collections.shuffle(Arrays.asList(cancels), rng);
59              barrier.await();
60              Thread.sleep(TIMEOUT);
61              for (int i = 0; i < cancels.length-2; ++i) {
62                  cancels[i].interrupt();
63                  // make sure all OK even when cancellations spaced out
64 <                if ( (i & 3) == 0)
64 >                if ( (i & 3) == 0)
65                      Thread.sleep(1 + rng.nextInt(10));
66              }
67              barrier.await();
68              if (print) {
69                  long time = timer.getTime();
70 <                double secs = (double)(time) / 1000000000.0;
70 >                double secs = (double) time / 1000000000.0;
71                  System.out.println("\t " + secs + "s run time");
72              }
73  
# Line 85 | Line 88 | public final class CancelledLockLoops {
88  
89          public final void run() {
90              try {
91 <                barrier.await();
91 >                barrier.await();
92                  int sum = v;
93                  int x = 0;
94                  int n = ITERS;
# Line 106 | Line 109 | public final class CancelledLockLoops {
109                      sum += LoopHelpers.compute2(x);
110                  } while (n-- > 0);
111                  if (n <= 0) {
112 <                    lock.lock();
112 >                    lock.lock();
113                      try {
114                          ++completed;
115                      }
# Line 117 | Line 120 | public final class CancelledLockLoops {
120                  barrier.await();
121                  result += sum;
122              }
123 <            catch (Exception ex) {
123 >            catch (Exception ex) {
124                  //                ex.printStackTrace();
125 <                return;
125 >                return;
126              }
127          }
128      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines