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

Comparing jsr166/src/test/loops/CachedThreadPoolLoops.java (file contents):
Revision 1.1 by dl, Tue May 31 15:08:32 2005 UTC vs.
Revision 1.5 by jsr166, Thu Oct 29 23:09:07 2009 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. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5   */
6  
7 + //import jsr166y.*;
8   import java.util.concurrent.*;
9   import java.util.concurrent.atomic.*;
10  
11   public class CachedThreadPoolLoops {
12 +    static final int NCPUS = Runtime.getRuntime().availableProcessors();
13      static final AtomicInteger remaining = new AtomicInteger();
14      static final int maxIters = 1000000;
15  
16      public static void main(String[] args) throws Exception {
17 <        int maxThreads = 100;
18 <
17 <        if (args.length > 0)
17 >        int maxThreads = NCPUS * 3 / 2; // 100;
18 >        if (args.length > 0)
19              maxThreads = Integer.parseInt(args[0]);
20 <        
20 >
21 >        System.out.print("Warmup:");
22 >        for (int j = 0; j < 1; ++j) {
23 >            int k = 1;
24 >            for (int i = 1; i <= maxThreads;) {
25 >                System.out.print(" " + i);
26 >                oneTest(i, 10000, false);
27 >                Thread.sleep(100);
28 >                if (i == k) {
29 >                    k = i << 1;
30 >                    i = i + (i >>> 1);
31 >                }
32 >                else
33 >                    i = k;
34 >            }
35 >        }
36 >        System.out.println();
37 >
38          int k = 1;
39          for (int i = 1; i <= maxThreads;) {
40              System.out.println("Threads:" + i);
41 <            oneTest(i);
41 >            oneTest(i, maxIters, true);
42              Thread.sleep(100);
43              if (i == k) {
44                  k = i << 1;
45                  i = i + (i >>> 1);
46 <            }
47 <            else
46 >            }
47 >            else
48                  i = k;
49          }
50     }
51  
52 <    static void oneTest(int nThreads) throws Exception {
53 <        System.out.print("SynchronousQueue        ");
54 <        oneRun(new SynchronousQueue<Runnable>(), nThreads);
55 <        System.out.print("SynchronousQueue(fair)  ");
56 <        oneRun(new SynchronousQueue<Runnable>(true), nThreads);
52 >    static void oneTest(int nThreads, int iters, boolean print) throws Exception {
53 >        Thread.sleep(100); // System.gc();
54 >        if (print) System.out.print("LinkedTransferQueue      ");
55 >        oneRun(new LinkedTransferQueue<Runnable>(), nThreads, iters, print);
56 >
57 >        Thread.sleep(100); // System.gc();
58 >        if (print) System.out.print("LinkedBlockingQueue      ");
59 >        oneRun(new LinkedBlockingQueue<Runnable>(), nThreads, iters, print);
60 >
61 >        Thread.sleep(100); // System.gc();
62 >        if (print) System.out.print("SynchronousQueue         ");
63 >        oneRun(new SynchronousQueue<Runnable>(false), nThreads, iters, print);
64 >
65 >        Thread.sleep(100); // System.gc();
66 >        if (print) System.out.print("SynchronousQueue(fair)   ");
67 >        oneRun(new SynchronousQueue<Runnable>(true), nThreads, iters, print);
68 >
69 >        Thread.sleep(100); // System.gc();
70 >        if (print) System.out.print("LinkedTransferQueue(xfer)");
71 >        oneRun(new LTQasSQ<Runnable>(), nThreads, iters, print);
72 >
73 >        Thread.sleep(100); // System.gc();
74 >        if (print) System.out.print("LinkedTransferQueue(half)");
75 >        oneRun(new HalfSyncLTQ<Runnable>(), nThreads, iters, print);
76 >
77 >        Thread.sleep(100); // System.gc();
78 >        if (print) System.out.print("ArrayBlockingQueue(256) ");
79 >        oneRun(new ArrayBlockingQueue<Runnable>(256), nThreads, iters, print);
80 >
81      }
82  
83      static final class Task implements Runnable {
84          final ThreadPoolExecutor pool;
85          final CountDownLatch done;
86 <        Task(ThreadPoolExecutor p, CountDownLatch d) {
87 <            pool = p;
86 >        Task(ThreadPoolExecutor p, CountDownLatch d) {
87 >            pool = p;
88              done = d;
89          }
90          public void run() {
# Line 50 | Line 92 | public class CachedThreadPoolLoops {
92              remaining.incrementAndGet();
93              int n;
94              while (!Thread.interrupted() &&
95 <                   (n = remaining.get()) > 0 &&
95 >                   (n = remaining.get()) > 0 &&
96                     done.getCount() > 0) {
97                  if (remaining.compareAndSet(n, n-1)) {
98                      try {
# Line 65 | Line 107 | public class CachedThreadPoolLoops {
107              }
108          }
109      }
110 <    
111 <    static void oneRun(BlockingQueue<Runnable> q, int nThreads) throws Exception {
112 <      
113 <        ThreadPoolExecutor pool =
110 >
111 >    static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception {
112 >
113 >        ThreadPoolExecutor pool =
114              new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE,
115                                     1L, TimeUnit.SECONDS, q);
116  
117 <        CountDownLatch done = new CountDownLatch(maxIters);
117 >        CountDownLatch done = new CountDownLatch(iters);
118          remaining.set(nThreads-1);
119          pool.prestartAllCoreThreads();
120          Task t = new Task(pool, done);
# Line 80 | Line 122 | public class CachedThreadPoolLoops {
122          pool.execute(t);
123          done.await();
124          long time = System.nanoTime() - start;
125 <        System.out.println("\t: " + LoopHelpers.rightJustify(time / maxIters) + " ns per task");
125 >        if (print)
126 >            System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task");
127 >        q.clear();
128 >        Thread.sleep(100);
129 >        pool.shutdown();
130 >        Thread.sleep(100);
131          pool.shutdownNow();
132      }
133  
134 +    static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
135 +        LTQasSQ() { super(); }
136 +        public void put(T x) {
137 +            try { super.transfer(x);
138 +            } catch (InterruptedException ex) { throw new Error(); }
139 +        }
140 +    }
141 +
142 +    static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
143 +        int calls;
144 +        HalfSyncLTQ() { super(); }
145 +        public void put(T x) {
146 +            if ((++calls & 1) == 0)
147 +                super.put(x);
148 +            else {
149 +                try { super.transfer(x);
150 +                } catch (InterruptedException ex) {
151 +                    throw new Error();
152 +                }
153 +            }
154 +        }
155 +    }
156 +
157   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines