ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CachedThreadPoolLoops.java
Revision: 1.2
Committed: Sun Aug 7 19:25:55 2005 UTC (18 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.1: +32 -10 lines
Log Message:
Add exchanger performance tests; update others

File Contents

# Content
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.
5 */
6
7 import java.util.concurrent.*;
8 import java.util.concurrent.atomic.*;
9
10 public class CachedThreadPoolLoops {
11 static final AtomicInteger remaining = new AtomicInteger();
12 static final int maxIters = 1000000;
13
14 public static void main(String[] args) throws Exception {
15 int maxThreads = 100;
16
17 if (args.length > 0)
18 maxThreads = Integer.parseInt(args[0]);
19
20 System.out.print("Warmup:");
21 for (int j = 0; j < 2; ++j) {
22 int k = 1;
23 for (int i = 1; i <= maxThreads;) {
24 System.out.print(" " + i);
25 oneTest(i, 10000, false);
26 Thread.sleep(100);
27 if (i == k) {
28 k = i << 1;
29 i = i + (i >>> 1);
30 }
31 else
32 i = k;
33 }
34 }
35 System.out.println();
36
37 int k = 1;
38 for (int i = 1; i <= maxThreads;) {
39 System.out.println("Threads:" + i);
40 oneTest(i, maxIters, true);
41 Thread.sleep(100);
42 if (i == k) {
43 k = i << 1;
44 i = i + (i >>> 1);
45 }
46 else
47 i = k;
48 }
49 }
50
51 static void oneTest(int nThreads, int iters, boolean print) throws Exception {
52 if (print) System.out.print("SynchronousQueue ");
53 oneRun(new SynchronousQueue<Runnable>(false), nThreads, iters, print);
54 if (print) System.out.print("SynchronousQueue(fair) ");
55 oneRun(new SynchronousQueue<Runnable>(true), nThreads, iters, print);
56 }
57
58 static final class Task implements Runnable {
59 final ThreadPoolExecutor pool;
60 final CountDownLatch done;
61 Task(ThreadPoolExecutor p, CountDownLatch d) {
62 pool = p;
63 done = d;
64 }
65 public void run() {
66 done.countDown();
67 remaining.incrementAndGet();
68 int n;
69 while (!Thread.interrupted() &&
70 (n = remaining.get()) > 0 &&
71 done.getCount() > 0) {
72 if (remaining.compareAndSet(n, n-1)) {
73 try {
74 pool.execute(this);
75 }
76 catch (RuntimeException ex) {
77 System.out.print("*");
78 while (done.getCount() > 0) done.countDown();
79 return;
80 }
81 }
82 }
83 }
84 }
85
86 static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception {
87
88 ThreadPoolExecutor pool =
89 new ThreadPoolExecutor(nThreads+1, Integer.MAX_VALUE,
90 1L, TimeUnit.SECONDS, q);
91
92 CountDownLatch done = new CountDownLatch(iters);
93 remaining.set(nThreads-1);
94 pool.prestartAllCoreThreads();
95 Task t = new Task(pool, done);
96 long start = System.nanoTime();
97 pool.execute(t);
98 done.await();
99 long time = System.nanoTime() - start;
100 if (print)
101 System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task");
102 q.clear();
103 Thread.sleep(100);
104 pool.shutdown();
105 Thread.sleep(100);
106 pool.shutdownNow();
107 }
108
109 }