ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Mutex100M.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# Content
1
2 import java.util.concurrent.*;
3 import java.util.concurrent.atomic.*;
4 import java.util.concurrent.locks.*;
5
6 class Mutex100M {
7 public static void main(String[] args) throws Exception {
8 int x = loop((int)System.nanoTime(), 100000);
9 x = loop(x, 100000);
10 x = loop(x, 100000);
11 long start = System.nanoTime();
12 x = loop(x, 100000000);
13 if (x == 0) System.out.print(" ");
14 long time = System.nanoTime() - start;
15 double secs = (double)time / 1000000000.0;
16 System.out.println("time: " + secs);
17 start = System.nanoTime();
18 x = loop(x, 100000000);
19 if (x == 0) System.out.print(" ");
20 time = System.nanoTime() - start;
21 secs = (double)time / 1000000000.0;
22 System.out.println("time: " + secs);
23
24 }
25
26 static final Mutex100M.Mutex lock = new Mutex100M.Mutex();
27
28 static int loop(int x, int iters) {
29 final Mutex100M.Mutex l = lock;
30 for (int i = iters; i > 0; --i) {
31 l.lock();
32 x = x * 134775813 + 1;
33 l.unlock();
34 }
35 return x;
36 }
37
38
39 static final class Mutex extends AbstractQueuedSynchronizer {
40 public boolean isHeldExclusively() { return getState() == 1; }
41
42 public boolean tryAcquire(int acquires) {
43 return getState() == 0 && compareAndSetState(0, 1);
44 }
45
46 public boolean tryRelease(int releases) {
47 setState(0);
48 return true;
49 }
50 public Condition newCondition() { return new ConditionObject(); }
51
52 public void lock() {
53 if (!compareAndSetState(0, 1))
54 acquire(1);
55 }
56 public boolean tryLock() {
57 return tryAcquire(1);
58 }
59 public void lockInterruptibly() throws InterruptedException {
60 acquireInterruptibly(1);
61 }
62 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
63 return tryAcquireNanos(1, unit.toNanos(timeout));
64 }
65 public void unlock() { release(1); }
66 }
67
68 }