ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CancelledFutureLoops.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# User Rev Content
1 dl 1.1 /*
2     * @test
3     * @summary Checks for responsiveness of futures to cancellation.
4     * Runs under
5     * the assumption that ITERS computations require more than TIMEOUT
6     * msecs to complete.
7     */
8    
9     /*
10     * Written by Doug Lea with assistance from members of JCP JSR-166
11     * Expert Group and released to the public domain. Use, modify, and
12     * redistribute this code in any way without acknowledgement.
13     */
14    
15     import java.util.concurrent.*;
16     import java.util.concurrent.locks.*;
17     import java.util.*;
18    
19     public final class CancelledFutureLoops {
20     static final ExecutorService pool = Executors.newCachedThreadPool();
21     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
22     static boolean print = false;
23     static final int ITERS = 10000000;
24     static final long TIMEOUT = 100;
25    
26     public static void main(String[] args) throws Exception {
27     int maxThreads = 100;
28     if (args.length > 0)
29     maxThreads = Integer.parseInt(args[0]);
30    
31     print = true;
32    
33     for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
34     System.out.print("Threads: " + i);
35     new FutureLoop(i).test();
36     Thread.sleep(TIMEOUT);
37     }
38     pool.shutdown();
39     }
40    
41     static final class FutureLoop implements Callable {
42     private int v = rng.next();
43     private final ReentrantLock lock = new ReentrantLock();
44     private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
45     private final CyclicBarrier barrier;
46     private final int nthreads;
47     FutureLoop(int nthreads) {
48     this.nthreads = nthreads;
49     barrier = new CyclicBarrier(nthreads+1, timer);
50     }
51    
52     final void test() throws Exception {
53     Future[] futures = new Future[nthreads];
54     for (int i = 0; i < nthreads; ++i)
55     futures[i] = pool.submit(this);
56    
57     barrier.await();
58     Thread.sleep(TIMEOUT);
59     boolean tooLate = false;
60     for (int i = 1; i < nthreads; ++i) {
61     if (!futures[i].cancel(true))
62     tooLate = true;
63     // Unbunch some of the cancels
64     if ( (i & 3) == 0)
65     Thread.sleep(1 + rng.next() % 10);
66     }
67    
68     Object f0 = futures[0].get();
69     if (!tooLate) {
70     for (int i = 1; i < nthreads; ++i) {
71     if (!futures[i].isDone() || !futures[i].isCancelled())
72     throw new Error("Only one thread should complete");
73     }
74     }
75     else
76     System.out.print("(cancelled too late) ");
77    
78     long endTime = System.nanoTime();
79     long time = endTime - timer.startTime;
80     if (print) {
81     double secs = (double)(time) / 1000000000.0;
82     System.out.println("\t " + secs + "s run time");
83     }
84    
85     }
86    
87     public final Object call() throws Exception {
88     barrier.await();
89     int sum = v;
90     int x = 0;
91     int n = ITERS;
92     while (n-- > 0) {
93     lock.lockInterruptibly();
94     try {
95     v = x = LoopHelpers.compute1(v);
96     }
97     finally {
98     lock.unlock();
99     }
100     sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
101     }
102     return new Integer(sum);
103     }
104     }
105    
106     }