ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ContextSwitchTest.java
(Generate patch)

Comparing jsr166/src/test/loops/ContextSwitchTest.java (file contents):
Revision 1.2 by dl, Mon May 9 19:33:30 2005 UTC vs.
Revision 1.3 by dl, Thu Sep 15 16:55:40 2005 UTC

# Line 13 | Line 13 | public final class ContextSwitchTest {
13      final static int iters = 1000000;
14      static AtomicReference turn = new AtomicReference();
15      public static void main(String[] args) throws Exception {
16 +        test();
17 +        test();
18 +        test();
19 +    }
20 +
21 +    static void test() throws Exception {
22          MyThread a = new MyThread();
23          MyThread b = new MyThread();
24          a.other = b;
# Line 25 | Line 31 | public final class ContextSwitchTest {
31          b.join();
32          long endTime = System.nanoTime();
33          int np = a.nparks + b.nparks;
34 <        System.out.println((endTime - startTime) / np);
35 <    }
36 <
31 <    private static int nextRandom(int x) {
32 <        int t = (x % 127773) * 16807 - (x / 127773) * 2836;
33 <        return (t > 0)? t : t + 0x7fffffff;
34 >        System.out.println("Average time: " +
35 >                           ((endTime - startTime) / np) +
36 >                           "ns");
37      }
38  
39      static final class MyThread extends Thread {
40          volatile Thread other;
41          volatile int nparks;
39        volatile int result;
42  
43          public void run() {
44 <            int x = 17;
44 >            final AtomicReference t = turn;
45 >            final Thread other = this.other;
46 >            if (turn == null || other == null)
47 >                throw new NullPointerException();
48              int p = 0;
49              for (int i = 0; i < iters; ++i) {
50 <                while (!turn.compareAndSet(other, this)) {
50 >                while (!t.compareAndSet(other, this)) {
51                      LockSupport.park();
52                      ++p;
53                  }
49                x = nextRandom(x);
54                  LockSupport.unpark(other);
55              }
56              LockSupport.unpark(other);
57              nparks = p;
54            result = x;
58              System.out.println("parks: " + p);
59              
60          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines