ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ContextSwitchTest.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

# User Rev Content
1 dl 1.1 import java.util.concurrent.*;
2     import java.util.concurrent.locks.*;
3     import java.util.concurrent.atomic.*;
4     import java.util.*;
5    
6     public final class ContextSwitchTest {
7     final static int iters = 1000000;
8     static AtomicReference turn = new AtomicReference();
9     public static void main(String[] args) throws Exception {
10     MyThread a = new MyThread();
11     MyThread b = new MyThread();
12     a.other = b;
13     b.other = a;
14     turn.set(a);
15     long startTime = System.nanoTime();
16     a.start();
17     b.start();
18     a.join();
19     b.join();
20     long endTime = System.nanoTime();
21     int np = a.nparks + b.nparks;
22     System.out.println((endTime - startTime) / np);
23     }
24    
25     private static int nextRandom(int x) {
26     int t = (x % 127773) * 16807 - (x / 127773) * 2836;
27     return (t > 0)? t : t + 0x7fffffff;
28     }
29    
30     static final class MyThread extends Thread {
31     volatile Thread other;
32     volatile int nparks;
33     volatile int result;
34    
35     public void run() {
36     int x = 17;
37     int p = 0;
38     for (int i = 0; i < iters; ++i) {
39     while (!turn.compareAndSet(other, this)) {
40     LockSupport.park();
41     ++p;
42     }
43     x = nextRandom(x);
44     LockSupport.unpark(other);
45     }
46     LockSupport.unpark(other);
47     nparks = p;
48     result = x;
49     System.out.println("parks: " + p);
50    
51     }
52     }
53     }
54    
55