ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/PhaserLoops.java
Revision: 1.2
Committed: Mon Nov 2 20:23:53 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +5 -7 lines
Log Message:
whitespace

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, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 */
6
7 import java.util.*;
8 import java.util.concurrent.*;
9 //import jsr166y.*;
10
11 /*
12 * Based loosely on Java Grande Forum barrierBench
13 */
14
15 public class PhaserLoops {
16 static final int NCPUS = Runtime.getRuntime().availableProcessors();
17 static final ExecutorService pool = Executors.newCachedThreadPool();
18 static final int FIRST_SIZE = 10000;
19 static final int LAST_SIZE = 1000000;
20 /** for time conversion */
21 static final long NPS = (1000L * 1000 * 1000);
22
23 static final class PhaserAction implements Runnable {
24 final int id;
25 final int size;
26 final Phaser phaser;
27 public PhaserAction(int id, Phaser b, int size) {
28 this.id = id;
29 this.phaser = b;
30 this.size = size;
31 phaser.register();
32 }
33
34
35 public void run() {
36 int n = size;
37 Phaser b = phaser;
38 for(int i = 0; i < n; ++i)
39 b.arriveAndAwaitAdvance();
40 }
41 }
42
43 public static void main(String[] args) throws Exception {
44 int nthreads = NCPUS;
45 if (args.length > 0)
46 nthreads = Integer.parseInt(args[0]);
47
48 System.out.printf("max %d Threads\n", nthreads);
49
50 for (int k = 2; k <= nthreads; k *= 2) {
51 for (int size = FIRST_SIZE; size <= LAST_SIZE; size *= 10) {
52 long startTime = System.nanoTime();
53
54 Phaser phaser = new Phaser();
55 PhaserAction[] actions = new PhaserAction[nthreads];
56 for (int i = 0; i < k; ++i) {
57 actions[i] = new PhaserAction(i, phaser, size);
58 }
59
60 Future[] futures = new Future[k];
61 for (int i = 0; i < k; ++i) {
62 futures[i] = pool.submit(actions[i]);
63 }
64 for (int i = 0; i < k; ++i) {
65 futures[i].get();
66 }
67 long elapsed = System.nanoTime() - startTime;
68 long bs = (NPS * size) / elapsed;
69 System.out.printf("%4d Threads %8d iters: %11d barriers/sec\n",
70 k, size, bs);
71 }
72 }
73 pool.shutdown();
74 }
75
76 }