--- jsr166/src/test/loops/CachedThreadPoolLoops.java 2007/02/19 00:46:06 1.3 +++ jsr166/src/test/loops/CachedThreadPoolLoops.java 2009/10/23 19:57:06 1.4 @@ -4,21 +4,22 @@ * http://creativecommons.org/licenses/publicdomain */ +//import jsr166y.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; public class CachedThreadPoolLoops { + static final int NCPUS = Runtime.getRuntime().availableProcessors(); static final AtomicInteger remaining = new AtomicInteger(); static final int maxIters = 1000000; public static void main(String[] args) throws Exception { - int maxThreads = 100; - + int maxThreads = NCPUS * 3 / 2; // 100; if (args.length > 0) maxThreads = Integer.parseInt(args[0]); System.out.print("Warmup:"); - for (int j = 0; j < 2; ++j) { + for (int j = 0; j < 1; ++j) { int k = 1; for (int i = 1; i <= maxThreads;) { System.out.print(" " + i); @@ -49,14 +50,34 @@ public class CachedThreadPoolLoops { } static void oneTest(int nThreads, int iters, boolean print) throws Exception { - // if (print) System.out.print("LinkedBlockingQueue "); - // oneRun(new LinkedBlockingQueue(256), nThreads, iters, print); - // if (print) System.out.print("ArrayBlockingQueue "); - // oneRun(new ArrayBlockingQueue(256), nThreads, iters, print); - if (print) System.out.print("SynchronousQueue "); + Thread.sleep(100); // System.gc(); + if (print) System.out.print("LinkedTransferQueue "); + oneRun(new LinkedTransferQueue(), nThreads, iters, print); + + Thread.sleep(100); // System.gc(); + if (print) System.out.print("LinkedBlockingQueue "); + oneRun(new LinkedBlockingQueue(), nThreads, iters, print); + + Thread.sleep(100); // System.gc(); + if (print) System.out.print("SynchronousQueue "); oneRun(new SynchronousQueue(false), nThreads, iters, print); - if (print) System.out.print("SynchronousQueue(fair) "); + + Thread.sleep(100); // System.gc(); + if (print) System.out.print("SynchronousQueue(fair) "); oneRun(new SynchronousQueue(true), nThreads, iters, print); + + Thread.sleep(100); // System.gc(); + if (print) System.out.print("LinkedTransferQueue(xfer)"); + oneRun(new LTQasSQ(), nThreads, iters, print); + + Thread.sleep(100); // System.gc(); + if (print) System.out.print("LinkedTransferQueue(half)"); + oneRun(new HalfSyncLTQ(), nThreads, iters, print); + + Thread.sleep(100); // System.gc(); + if (print) System.out.print("ArrayBlockingQueue(256) "); + oneRun(new ArrayBlockingQueue(256), nThreads, iters, print); + } static final class Task implements Runnable { @@ -110,4 +131,27 @@ public class CachedThreadPoolLoops { pool.shutdownNow(); } + static final class LTQasSQ extends LinkedTransferQueue { + LTQasSQ() { super(); } + public void put(T x) { + try { super.transfer(x); + } catch (InterruptedException ex) { throw new Error(); } + } + } + + static final class HalfSyncLTQ extends LinkedTransferQueue { + int calls; + HalfSyncLTQ() { super(); } + public void put(T x) { + if ((++calls & 1) == 0) + super.put(x); + else { + try { super.transfer(x); + } catch (InterruptedException ex) { + throw new Error(); + } + } + } + } + }