ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ExecutorCompletionServiceLoops.java
Revision: 1.5
Committed: Thu Apr 14 23:16:10 2011 UTC (13 years ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.4: +1 -1 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.2 * Expert Group and released to the public domain, as explained at
4 jsr166 1.4 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.concurrent.*;
8    
9     public class ExecutorCompletionServiceLoops {
10     static final int POOLSIZE = 100;
11 jsr166 1.3 static final ExecutorService pool =
12 dl 1.1 Executors.newFixedThreadPool(POOLSIZE);
13 jsr166 1.3 static final ExecutorCompletionService<Integer> ecs =
14 dl 1.1 new ExecutorCompletionService<Integer>(pool);
15     static boolean print = false;
16    
17     public static void main(String[] args) throws Exception {
18     int max = 8;
19     int base = 10000;
20    
21 jsr166 1.3 if (args.length > 0)
22 dl 1.1 max = Integer.parseInt(args[0]);
23    
24     System.out.println("Warmup...");
25     oneTest( base );
26     Thread.sleep(100);
27     print = true;
28 jsr166 1.3
29 dl 1.1 for (int i = 1; i <= max; i += (i+1) >>> 1) {
30     System.out.print("n: " + i * base);
31     oneTest(i * base );
32     Thread.sleep(100);
33     }
34     pool.shutdown();
35 jsr166 1.5 }
36 jsr166 1.3
37 dl 1.1 static class Task implements Callable<Integer> {
38     public Integer call() {
39     int l = System.identityHashCode(this);
40     l = LoopHelpers.compute2(l);
41     int s = LoopHelpers.compute1(l);
42     l = LoopHelpers.compute2(l);
43     s += LoopHelpers.compute1(l);
44     return new Integer(s);
45     }
46     }
47    
48     static class Producer implements Runnable {
49     final ExecutorCompletionService cs;
50     final int iters;
51 jsr166 1.3 Producer(ExecutorCompletionService ecs, int i) {
52 dl 1.1 cs = ecs;
53 jsr166 1.3 iters = i;
54 dl 1.1 }
55     public void run() {
56     for (int i = 0; i < iters; ++i)
57     ecs.submit(new Task());
58     }
59     }
60    
61     static void oneTest(int iters) throws Exception {
62     long startTime = System.nanoTime();
63     new Thread(new Producer(ecs, iters)).start();
64    
65     int r = 0;
66     for (int i = 0; i < iters; ++i)
67     r += ecs.take().get().intValue();
68    
69     long elapsed = System.nanoTime() - startTime;
70     long tpi = elapsed/ iters;
71    
72     if (print)
73     System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task");
74    
75     if (r == 0) // avoid overoptimization
76     System.out.println("useless result: " + r);
77    
78    
79     }
80    
81     }