--- jsr166/src/test/loops/ContextSwitchTest.java 2005/09/15 16:55:40 1.3 +++ jsr166/src/test/loops/ContextSwitchTest.java 2015/10/12 20:16:47 1.9 @@ -1,16 +1,16 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ +import java.util.*; import java.util.concurrent.*; -import java.util.concurrent.locks.*; import java.util.concurrent.atomic.*; -import java.util.*; +import java.util.concurrent.locks.*; public final class ContextSwitchTest { - final static int iters = 1000000; + static final int iters = 1000000; static AtomicReference turn = new AtomicReference(); public static void main(String[] args) throws Exception { test(); @@ -31,19 +31,26 @@ public final class ContextSwitchTest { b.join(); long endTime = System.nanoTime(); int np = a.nparks + b.nparks; - System.out.println("Average time: " + + System.out.println("Average time: " + ((endTime - startTime) / np) + "ns"); } static final class MyThread extends Thread { + + static { + // Reduce the risk of rare disastrous classloading in first call to + // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 + Class ensureLoaded = LockSupport.class; + } + volatile Thread other; volatile int nparks; public void run() { final AtomicReference t = turn; final Thread other = this.other; - if (turn == null || other == null) + if (turn == null || other == null) throw new NullPointerException(); int p = 0; for (int i = 0; i < iters; ++i) { @@ -56,9 +63,6 @@ public final class ContextSwitchTest { LockSupport.unpark(other); nparks = p; System.out.println("parks: " + p); - } } } - -