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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines