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.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.9 by jsr166, Mon Oct 12 20:16:47 2015 UTC

# Line 1 | Line 1
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/publicdomain/zero/1.0/
5 + */
6 +
7 + import java.util.*;
8   import java.util.concurrent.*;
2 import java.util.concurrent.locks.*;
9   import java.util.concurrent.atomic.*;
10 < import java.util.*;
10 > import java.util.concurrent.locks.*;
11  
12   public final class ContextSwitchTest {
13 <    final static int iters = 1000000;
13 >    static final 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 19 | 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 <
25 <    private static int nextRandom(int x) {
26 <        int t = (x % 127773) * 16807 - (x / 127773) * 2836;
27 <        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 +
41 +        static {
42 +            // Reduce the risk of rare disastrous classloading in first call to
43 +            // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
44 +            Class<?> ensureLoaded = LockSupport.class;
45 +        }
46 +
47          volatile Thread other;
48          volatile int nparks;
33        volatile int result;
49  
50          public void run() {
51 <            int x = 17;
51 >            final AtomicReference t = turn;
52 >            final Thread other = this.other;
53 >            if (turn == null || other == null)
54 >                throw new NullPointerException();
55              int p = 0;
56              for (int i = 0; i < iters; ++i) {
57 <                while (!turn.compareAndSet(other, this)) {
57 >                while (!t.compareAndSet(other, this)) {
58                      LockSupport.park();
59                      ++p;
60                  }
43                x = nextRandom(x);
61                  LockSupport.unpark(other);
62              }
63              LockSupport.unpark(other);
64              nparks = p;
48            result = x;
65              System.out.println("parks: " + p);
50            
66          }
67      }
68   }
54
55

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines