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

Comparing jsr166/src/test/loops/CancelledFutureLoops.java (file contents):
Revision 1.2 by dl, Mon May 9 19:33:30 2005 UTC vs.
Revision 1.9 by jsr166, Sat Dec 31 19:34:08 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   /*
8   * @test
9 < * @summary Checks for responsiveness of futures to cancellation.
9 > * @summary Checks for responsiveness of futures to cancellation.
10   * Runs under
11   * the assumption that ITERS computations require more than TIMEOUT
12   * msecs to complete.
# Line 18 | Line 18
18   * redistribute this code in any way without acknowledgement.
19   */
20  
21 < import java.util.concurrent.*;
22 < import java.util.concurrent.locks.*;
23 < import java.util.*;
21 > import java.util.concurrent.Callable;
22 > import java.util.concurrent.CyclicBarrier;
23 > import java.util.concurrent.ExecutorService;
24 > import java.util.concurrent.Executors;
25 > import java.util.concurrent.Future;
26 > import java.util.concurrent.locks.ReentrantLock;
27  
28   public final class CancelledFutureLoops {
29      static final ExecutorService pool = Executors.newCachedThreadPool();
30      static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
31      static boolean print = false;
32      static final int ITERS = 10000000;
33 <    static final long TIMEOUT = 100;
33 >    static final long TIMEOUT = 100;
34  
35      public static void main(String[] args) throws Exception {
36          int maxThreads = 100;
37 <        if (args.length > 0)
37 >        if (args.length > 0)
38              maxThreads = Integer.parseInt(args[0]);
39  
40          print = true;
# Line 44 | Line 47 | public final class CancelledFutureLoops
47          pool.shutdown();
48      }
49  
50 <    static final class FutureLoop implements Callable {
50 >    static final class FutureLoop implements Callable<Object> {
51          private int v = rng.next();
52          private final ReentrantLock lock = new ReentrantLock();
53          private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
# Line 56 | Line 59 | public final class CancelledFutureLoops
59          }
60  
61          final void test() throws Exception {
62 <            Future[] futures = new Future[nthreads];
63 <            for (int i = 0; i < nthreads; ++i)
62 >            Future<?>[] futures = new Future<?>[nthreads];
63 >            for (int i = 0; i < nthreads; ++i)
64                  futures[i] = pool.submit(this);
65  
66              barrier.await();
# Line 67 | Line 70 | public final class CancelledFutureLoops
70                  if (!futures[i].cancel(true))
71                      tooLate = true;
72                  // Unbunch some of the cancels
73 <                if ( (i & 3) == 0)
73 >                if ( (i & 3) == 0)
74                      Thread.sleep(1 + rng.next() % 10);
75              }
76  
# Line 80 | Line 83 | public final class CancelledFutureLoops
83              }
84              else
85                  System.out.print("(cancelled too late) ");
86 <            
86 >
87              long endTime = System.nanoTime();
88              long time = endTime - timer.startTime;
89              if (print) {
90 <                double secs = (double)(time) / 1000000000.0;
90 >                double secs = (double) time / 1000000000.0;
91                  System.out.println("\t " + secs + "s run time");
92              }
93  
94          }
95  
96          public final Object call() throws Exception {
97 <            barrier.await();
97 >            barrier.await();
98              int sum = v;
99              int x = 0;
100              int n = ITERS;
# Line 104 | Line 107 | public final class CancelledFutureLoops
107                      lock.unlock();
108                  }
109                  sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
110 <            }
110 >            }
111              return new Integer(sum);
112          }
113      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines