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.3 by dl, Mon Feb 19 00:46:06 2007 UTC vs.
Revision 1.5 by jsr166, Thu Oct 29 23:09:07 2009 UTC

# Line 4 | Line 4
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  
21          System.out.print("Warmup:");
22 <        for (int j = 0; j < 2; ++j) {
22 >        for (int j = 0; j < 1; ++j) {
23              int k = 1;
24              for (int i = 1; i <= maxThreads;) {
25                  System.out.print(" " + i);
# Line 27 | Line 28 | public class CachedThreadPoolLoops {
28                  if (i == k) {
29                      k = i << 1;
30                      i = i + (i >>> 1);
31 <                }
32 <                else
31 >                }
32 >                else
33                      i = k;
34              }
35          }
# Line 42 | Line 43 | public class CachedThreadPoolLoops {
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, int iters, boolean print) throws Exception {
53 <        //        if (print) System.out.print("LinkedBlockingQueue     ");
54 <        //        oneRun(new LinkedBlockingQueue<Runnable>(256), nThreads, iters, print);
55 <        //        if (print) System.out.print("ArrayBlockingQueue      ");
56 <        //        oneRun(new ArrayBlockingQueue<Runnable>(256), nThreads, iters, print);
57 <        if (print) System.out.print("SynchronousQueue        ");
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 <        if (print) System.out.print("SynchronousQueue(fair)  ");
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 71 | 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 86 | Line 107 | public class CachedThreadPoolLoops {
107              }
108          }
109      }
110 <    
110 >
111      static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception {
112 <      
113 <        ThreadPoolExecutor pool =
112 >
113 >        ThreadPoolExecutor pool =
114              new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE,
115                                     1L, TimeUnit.SECONDS, q);
116  
# Line 110 | Line 131 | public class CachedThreadPoolLoops {
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