ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FutileTimedTryLockLoops.java
Revision: 1.6
Committed: Sat Dec 31 22:17:40 2016 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +0 -1 lines
Log Message:
organize imports

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Written by Martin Buchholz with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/publicdomain/zero/1.0/
5     */
6    
7 jsr166 1.5 import java.util.Collections;
8     import java.util.concurrent.Callable;
9     import java.util.concurrent.ExecutorService;
10     import java.util.concurrent.Executors;
11     import java.util.concurrent.TimeUnit;
12     import java.util.concurrent.locks.ReentrantLock;
13 jsr166 1.1
14     /**
15     * Measures repeated futile timed attempts to acquire a ReentrantLock.
16     * Hard to draw any conclusions from.
17     */
18     public final class FutileTimedTryLockLoops {
19    
20     static Callable<Void> tryLocker(ReentrantLock lock, long iters, long nanos) {
21     return new Callable<Void>() {
22     public Void call() throws InterruptedException {
23     for (long i = 0; i < iters; i++)
24     if (lock.tryLock(nanos, TimeUnit.NANOSECONDS))
25     throw new AssertionError();
26     return null;
27     }};
28     }
29    
30     public static void main(String[] args) throws Exception {
31     int minThreads = 1;
32     int maxThreads = 4;
33     long nanos = 1001L;
34     long iters = 60_000_000L;
35     nextArg: for (String arg : args) {
36     String[] fields = arg.split("=");
37     if (fields.length == 2) {
38     String prop = fields[0], val = fields[1];
39     switch (prop) {
40     case "threads":
41     minThreads = maxThreads = Integer.valueOf(val);
42     continue nextArg;
43     case "minThreads":
44     minThreads = Integer.valueOf(val);
45     continue nextArg;
46     case "maxThreads":
47     maxThreads = Integer.valueOf(val);
48     continue nextArg;
49     case "nanos":
50     nanos = Long.valueOf(val);
51     continue nextArg;
52     case "iters":
53     iters = Long.valueOf(val);
54     continue nextArg;
55     }
56     }
57 jsr166 1.4 throw new Error("Usage: FutileTimedTryLockLoops minThreads=n maxThreads=n threads=n nanos=n iters=n");
58 jsr166 1.1 }
59     final ExecutorService pool = Executors.newCachedThreadPool();
60     final ReentrantLock lock = new ReentrantLock();
61     final Callable<Void> task = tryLocker(lock, iters, nanos);
62     lock.lock();
63    
64     for (int i = minThreads; i <= maxThreads; i += (i+1) >>> 1) {
65 jsr166 1.3 // warmup
66     if (i == minThreads)
67     pool.invokeAll(Collections.nCopies(i, task));
68    
69 jsr166 1.1 long startTime = System.nanoTime();
70     pool.invokeAll(Collections.nCopies(i, task));
71     long elapsed = System.nanoTime() - startTime;
72     System.out.printf
73     ("Threads: %d, %d ms total, %d ns/iter, overhead %d ns/iter%n",
74     i,
75     elapsed / (1000*1000),
76     elapsed / iters,
77     (elapsed / iters) - nanos);
78     }
79     pool.shutdown();
80     }
81     }