8225490: Miscellaneous changes imported from jsr166 CVS 2019-09

Reviewed-by: martin, alanb
This commit is contained in:
Doug Lea 2019-09-14 11:26:26 -07:00
parent 9275097a02
commit eb1eadb69f
46 changed files with 622 additions and 640 deletions

@ -385,7 +385,7 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
* cases where old nodes can be reused because their next fields * cases where old nodes can be reused because their next fields
* won't change. On average, only about one-sixth of them need * won't change. On average, only about one-sixth of them need
* cloning when a table doubles. The nodes they replace will be * cloning when a table doubles. The nodes they replace will be
* garbage collectable as soon as they are no longer referenced by * garbage collectible as soon as they are no longer referenced by
* any reader thread that may be in the midst of concurrently * any reader thread that may be in the midst of concurrently
* traversing table. Upon transfer, the old table bin contains * traversing table. Upon transfer, the old table bin contains
* only a special forwarding node (with hash field "MOVED") that * only a special forwarding node (with hash field "MOVED") that
@ -3286,9 +3286,8 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
return true; return true;
} }
private static final Unsafe U = Unsafe.getUnsafe();
private static final long LOCKSTATE private static final long LOCKSTATE
= U.objectFieldOffset(TreeBin.class, "lockState"); = U.objectFieldOffset(TreeBin.class, "lockState");
} }
/* ----------------Table Traversal -------------- */ /* ----------------Table Traversal -------------- */
@ -6345,28 +6344,20 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
// Unsafe mechanics // Unsafe mechanics
private static final Unsafe U = Unsafe.getUnsafe(); private static final Unsafe U = Unsafe.getUnsafe();
private static final long SIZECTL; private static final long SIZECTL
private static final long TRANSFERINDEX; = U.objectFieldOffset(ConcurrentHashMap.class, "sizeCtl");
private static final long BASECOUNT; private static final long TRANSFERINDEX
private static final long CELLSBUSY; = U.objectFieldOffset(ConcurrentHashMap.class, "transferIndex");
private static final long CELLVALUE; private static final long BASECOUNT
private static final int ABASE; = U.objectFieldOffset(ConcurrentHashMap.class, "baseCount");
private static final long CELLSBUSY
= U.objectFieldOffset(ConcurrentHashMap.class, "cellsBusy");
private static final long CELLVALUE
= U.objectFieldOffset(CounterCell.class, "value");
private static final int ABASE = U.arrayBaseOffset(Node[].class);
private static final int ASHIFT; private static final int ASHIFT;
static { static {
SIZECTL = U.objectFieldOffset
(ConcurrentHashMap.class, "sizeCtl");
TRANSFERINDEX = U.objectFieldOffset
(ConcurrentHashMap.class, "transferIndex");
BASECOUNT = U.objectFieldOffset
(ConcurrentHashMap.class, "baseCount");
CELLSBUSY = U.objectFieldOffset
(ConcurrentHashMap.class, "cellsBusy");
CELLVALUE = U.objectFieldOffset
(CounterCell.class, "value");
ABASE = U.arrayBaseOffset(Node[].class);
int scale = U.arrayIndexScale(Node[].class); int scale = U.arrayIndexScale(Node[].class);
if ((scale & (scale - 1)) != 0) if ((scale & (scale - 1)) != 0)
throw new ExceptionInInitializerError("array index scale not a power of two"); throw new ExceptionInInitializerError("array index scale not a power of two");

@ -97,7 +97,7 @@ import java.util.concurrent.locks.LockSupport;
* associated recovery within handlers of those exceptions, * associated recovery within handlers of those exceptions,
* often after invoking {@code forceTermination}. Phasers may * often after invoking {@code forceTermination}. Phasers may
* also be used by tasks executing in a {@link ForkJoinPool}. * also be used by tasks executing in a {@link ForkJoinPool}.
* Progress is ensured if the pool's parallelismLevel can * Progress is ensured if the pool's parallelism level can
* accommodate the maximum number of simultaneously blocked * accommodate the maximum number of simultaneously blocked
* parties. * parties.
* *

@ -1053,18 +1053,18 @@ public class ThreadLocalRandom extends Random {
// Unsafe mechanics // Unsafe mechanics
private static final Unsafe U = Unsafe.getUnsafe(); private static final Unsafe U = Unsafe.getUnsafe();
private static final long SEED = U.objectFieldOffset private static final long SEED
(Thread.class, "threadLocalRandomSeed"); = U.objectFieldOffset(Thread.class, "threadLocalRandomSeed");
private static final long PROBE = U.objectFieldOffset private static final long PROBE
(Thread.class, "threadLocalRandomProbe"); = U.objectFieldOffset(Thread.class, "threadLocalRandomProbe");
private static final long SECONDARY = U.objectFieldOffset private static final long SECONDARY
(Thread.class, "threadLocalRandomSecondarySeed"); = U.objectFieldOffset(Thread.class, "threadLocalRandomSecondarySeed");
private static final long THREADLOCALS = U.objectFieldOffset private static final long THREADLOCALS
(Thread.class, "threadLocals"); = U.objectFieldOffset(Thread.class, "threadLocals");
private static final long INHERITABLETHREADLOCALS = U.objectFieldOffset private static final long INHERITABLETHREADLOCALS
(Thread.class, "inheritableThreadLocals"); = U.objectFieldOffset(Thread.class, "inheritableThreadLocals");
private static final long INHERITEDACCESSCONTROLCONTEXT = U.objectFieldOffset private static final long INHERITEDACCESSCONTROLCONTEXT
(Thread.class, "inheritedAccessControlContext"); = U.objectFieldOffset(Thread.class, "inheritedAccessControlContext");
/** Rarely-used holder for the second of a pair of Gaussians */ /** Rarely-used holder for the second of a pair of Gaussians */
private static final ThreadLocal<Double> nextLocalGaussian = private static final ThreadLocal<Double> nextLocalGaussian =

@ -38,6 +38,7 @@ package java.util.concurrent.atomic;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import java.util.function.IntBinaryOperator; import java.util.function.IntBinaryOperator;
import java.util.function.IntUnaryOperator; import java.util.function.IntUnaryOperator;
import jdk.internal.misc.Unsafe;
/** /**
* An {@code int} value that may be updated atomically. See the * An {@code int} value that may be updated atomically. See the
@ -58,8 +59,9 @@ public class AtomicInteger extends Number implements java.io.Serializable {
* This class intended to be implemented using VarHandles, but there * This class intended to be implemented using VarHandles, but there
* are unresolved cyclic startup dependencies. * are unresolved cyclic startup dependencies.
*/ */
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); private static final Unsafe U = Unsafe.getUnsafe();
private static final long VALUE = U.objectFieldOffset(AtomicInteger.class, "value"); private static final long VALUE
= U.objectFieldOffset(AtomicInteger.class, "value");
private volatile int value; private volatile int value;

@ -38,6 +38,7 @@ package java.util.concurrent.atomic;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import java.util.function.LongBinaryOperator; import java.util.function.LongBinaryOperator;
import java.util.function.LongUnaryOperator; import java.util.function.LongUnaryOperator;
import jdk.internal.misc.Unsafe;
/** /**
* A {@code long} value that may be updated atomically. See the * A {@code long} value that may be updated atomically. See the
@ -72,8 +73,9 @@ public class AtomicLong extends Number implements java.io.Serializable {
* This class intended to be implemented using VarHandles, but there * This class intended to be implemented using VarHandles, but there
* are unresolved cyclic startup dependencies. * are unresolved cyclic startup dependencies.
*/ */
private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe(); private static final Unsafe U = Unsafe.getUnsafe();
private static final long VALUE = U.objectFieldOffset(AtomicLong.class, "value"); private static final long VALUE
= U.objectFieldOffset(AtomicLong.class, "value");
private volatile long value; private volatile long value;

@ -226,9 +226,8 @@
* *
* <h2 id="MemoryVisibility">Memory Consistency Properties</h2> * <h2 id="MemoryVisibility">Memory Consistency Properties</h2>
* *
* <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-17.html#jls-17.4.5">
* Chapter 17 of * Chapter 17 of
* <cite>The Java&trade; Language Specification</cite></a> defines the * <cite>The Java&trade; Language Specification</cite> defines the
* <i>happens-before</i> relation on memory operations such as reads and * <i>happens-before</i> relation on memory operations such as reads and
* writes of shared variables. The results of a write by one thread are * writes of shared variables. The results of a write by one thread are
* guaranteed to be visible to a read by another thread only if the write * guaranteed to be visible to a read by another thread only if the write
@ -302,6 +301,8 @@
* *
* </ul> * </ul>
* *
* @jls 17.4.5 Happens-before Order
*
* @since 1.5 * @since 1.5
*/ */
package java.util.concurrent; package java.util.concurrent;

@ -120,8 +120,8 @@ public class Get {
//--------------------- Infrastructure --------------------------- //--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0; static volatile int passed = 0, failed = 0;
static void pass() { passed++; } static void pass() { passed++; }
static void fail() { failed++; (new Error("Failure")).printStackTrace(System.err); } static void fail() { failed++; new Error("Failure").printStackTrace(System.err); }
static void fail(String msg) { failed++; (new Error("Failure: " + msg)).printStackTrace(System.err); } static void fail(String msg) { failed++; new Error("Failure: " + msg).printStackTrace(System.err); }
static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); } static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); }
static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); } static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); }
static void check(boolean cond) { if (cond) pass(); else fail(); } static void check(boolean cond) { if (cond) pass(); else fail(); }

@ -34,6 +34,7 @@
/* /*
* @test * @test
* @bug 6805775 6815766 * @bug 6805775 6815766
* @library /test/lib
* @run main OfferDrainToLoops 100 * @run main OfferDrainToLoops 100
* @summary Test concurrent offer vs. drainTo * @summary Test concurrent offer vs. drainTo
*/ */
@ -47,10 +48,12 @@ import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue; import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import jdk.test.lib.Utils;
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
public class OfferDrainToLoops { public class OfferDrainToLoops {
final long testDurationMillisDefault = 10L * 1000L; static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
final long testDurationMillisDefault = 10_000L;
final long testDurationMillis; final long testDurationMillis;
OfferDrainToLoops(String[] args) { OfferDrainToLoops(String[] args) {
@ -76,7 +79,6 @@ public class OfferDrainToLoops {
System.out.println(q.getClass().getSimpleName()); System.out.println(q.getClass().getSimpleName());
final long testDurationNanos = testDurationMillis * 1000L * 1000L; final long testDurationNanos = testDurationMillis * 1000L * 1000L;
final long quittingTimeNanos = System.nanoTime() + testDurationNanos; final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
final long timeoutMillis = 10L * 1000L;
final SplittableRandom rnd = new SplittableRandom(); final SplittableRandom rnd = new SplittableRandom();
// Poor man's bounded buffer. // Poor man's bounded buffer.
@ -155,13 +157,13 @@ public class OfferDrainToLoops {
}}}; }}};
for (Thread thread : new Thread[] { offerer, drainer, scanner }) { for (Thread thread : new Thread[] { offerer, drainer, scanner }) {
thread.join(timeoutMillis + testDurationMillis); thread.join(LONG_DELAY_MS + testDurationMillis);
if (thread.isAlive()) { if (thread.isAlive()) {
System.err.printf("Hung thread: %s%n", thread.getName()); System.err.printf("Hung thread: %s%n", thread.getName());
failed++; failed++;
for (StackTraceElement e : thread.getStackTrace()) for (StackTraceElement e : thread.getStackTrace())
System.err.println(e); System.err.println(e);
thread.join(timeoutMillis); thread.join(LONG_DELAY_MS);
} }
} }
} }

@ -537,7 +537,7 @@ public class MapCheck {
static void printStats() { static void printStats() {
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
Map.Entry e = (Map.Entry)(it.next()); Map.Entry e = (Map.Entry)(it.next());
Stats stats = ((Stats)(e.getValue())); Stats stats = (Stats)(e.getValue());
int n = stats.number; int n = stats.number;
double t; double t;
if (n > 0) if (n > 0)

@ -99,8 +99,8 @@ public class MapLoops {
nops = Integer.parseInt(args[5]); nops = Integer.parseInt(args[5]);
// normalize probabilities wrt random number generator // normalize probabilities wrt random number generator
removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); removesPerMaxRandom = (int)((double)premove/100.0 * 0x7FFFFFFFL);
insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); insertsPerMaxRandom = (int)((double)pinsert/100.0 * 0x7FFFFFFFL);
System.out.print("Class: " + mapClass.getName()); System.out.print("Class: " + mapClass.getName());
System.out.print(" threads: " + maxThreads); System.out.print(" threads: " + maxThreads);
@ -172,7 +172,7 @@ public class MapLoops {
long time = timer.getTime(); long time = timer.getTime();
long tpo = time / (i * (long)nops); long tpo = time / (i * (long)nops);
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
double secs = (double)(time) / 1000000000.0; double secs = (double)time / 1000000000.0;
System.out.println("\t " + secs + "s run time"); System.out.println("\t " + secs + "s run time");
map.clear(); map.clear();
} }

@ -24,37 +24,40 @@
/* /*
* @test * @test
* @bug 4486658 8010293 * @bug 4486658 8010293
* @summary thread safety of toArray methods of subCollections * @summary thread safety of toArray methods of collection views
* @author Martin Buchholz * @author Martin Buchholz
*/ */
import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class ToArray { public class ToArray {
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
// Execute a number of times to increase the probability of final int runsPerTest = Integer.getInteger("jsr166.runsPerTest", 1);
// failure if there is an issue final int reps = 10 * runsPerTest;
for (int i = 0; i < 16; i++) { for (int i = reps; i--> 0; )
executeTest(); executeTest();
}
} }
static void executeTest() throws Throwable { static void executeTest() throws Throwable {
final Throwable[] throwable = new Throwable[1];
final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>(); final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>();
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
// Number of workers equal to the number of processors final int nCPU = Runtime.getRuntime().availableProcessors();
// Each worker will put globally unique keys into the map final int minWorkers = 2;
final int nWorkers = Runtime.getRuntime().availableProcessors(); final int maxWorkers = Math.max(minWorkers, Math.min(32, nCPU));
final int nWorkers = rnd.nextInt(minWorkers, maxWorkers + 1);
final int sizePerWorker = 1024; final int sizePerWorker = 1024;
final int maxSize = nWorkers * sizePerWorker; final int maxSize = nWorkers * sizePerWorker;
// The foreman keeps checking that the size of the arrays // The foreman busy-checks that the size of the arrays obtained
// obtained from the key and value sets is never less than the // from the keys and values views grows monotonically until it
// previously observed size and is never greater than the maximum size // reaches the maximum size.
// NOTE: these size constraints are not specific to toArray and are // NOTE: these size constraints are not specific to toArray and are
// applicable to any form of traversal of the collection views // applicable to any form of traversal of the collection views
CompletableFuture<?> foreman = CompletableFuture.runAsync(new Runnable() { CompletableFuture<?> foreman = CompletableFuture.runAsync(new Runnable() {
@ -62,44 +65,37 @@ public class ToArray {
private boolean checkProgress(Object[] a) { private boolean checkProgress(Object[] a) {
int size = a.length; int size = a.length;
if (size < prevSize) throw new RuntimeException("WRONG WAY"); if (size < prevSize || size > maxSize)
if (size > maxSize) throw new RuntimeException("OVERSHOOT"); throw new AssertionError(
if (size == maxSize) return true; String.format("prevSize=%d size=%d maxSize=%d",
prevSize, size, maxSize));
prevSize = size; prevSize = size;
return false; return size == maxSize;
} }
@Override
public void run() { public void run() {
try { Integer[] empty = new Integer[0];
Integer[] empty = new Integer[0]; for (;;)
while (true) { if (checkProgress(m.values().toArray())
if (checkProgress(m.values().toArray())) return; & checkProgress(m.keySet().toArray())
if (checkProgress(m.keySet().toArray())) return; & checkProgress(m.values().toArray(empty))
if (checkProgress(m.values().toArray(empty))) return; & checkProgress(m.keySet().toArray(empty)))
if (checkProgress(m.keySet().toArray(empty))) return; return;
}
}
catch (Throwable t) {
throwable[0] = t;
}
} }
}); });
// Create workers // Each worker puts globally unique keys into the map
// Each worker will put globally unique keys into the map List<CompletableFuture<?>> workers =
CompletableFuture<?>[] workers = IntStream.range(0, nWorkers). IntStream.range(0, nWorkers)
mapToObj(w -> CompletableFuture.runAsync(() -> { .mapToObj(w -> (Runnable) () -> {
for (int i = 0, o = w * sizePerWorker; i < sizePerWorker; i++) for (int i = 0, o = w * sizePerWorker; i < sizePerWorker; i++)
m.put(o + i, i); m.put(o + i, i);
})). })
toArray(CompletableFuture<?>[]::new); .map(CompletableFuture::runAsync)
.collect(Collectors.toList());
// Wait for workers and then foreman to complete // Wait for workers and foreman to complete
CompletableFuture.allOf(workers).join(); workers.forEach(CompletableFuture<?>::join);
foreman.join(); foreman.join();
if (throwable[0] != null)
throw throwable[0];
} }
} }

@ -25,7 +25,8 @@
* @test * @test
* @bug 6316155 6595669 6871697 6868712 * @bug 6316155 6595669 6871697 6868712
* @summary Test concurrent offer vs. remove * @summary Test concurrent offer vs. remove
* @run main OfferRemoveLoops 300 * @library /test/lib
* @run main OfferRemoveLoops 100
* @author Martin Buchholz * @author Martin Buchholz
*/ */
@ -43,10 +44,12 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue; import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import jdk.test.lib.Utils;
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
public class OfferRemoveLoops { public class OfferRemoveLoops {
final long testDurationMillisDefault = 10L * 1000L; static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
final long testDurationMillisDefault = 10_000L;
final long testDurationMillis; final long testDurationMillis;
OfferRemoveLoops(String[] args) { OfferRemoveLoops(String[] args) {
@ -75,7 +78,6 @@ public class OfferRemoveLoops {
System.err.println(q.getClass().getSimpleName()); System.err.println(q.getClass().getSimpleName());
final long testDurationNanos = testDurationMillis * 1000L * 1000L; final long testDurationNanos = testDurationMillis * 1000L * 1000L;
final long quittingTimeNanos = System.nanoTime() + testDurationNanos; final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
final long timeoutMillis = 10L * 1000L;
final int maxChunkSize = 1042; final int maxChunkSize = 1042;
final int maxQueueSize = 10 * maxChunkSize; final int maxQueueSize = 10 * maxChunkSize;
final CountDownLatch done = new CountDownLatch(3); final CountDownLatch done = new CountDownLatch(3);
@ -156,7 +158,7 @@ public class OfferRemoveLoops {
done.countDown(); done.countDown();
}}; }};
if (! done.await(timeoutMillis + testDurationMillis, MILLISECONDS)) { if (! done.await(LONG_DELAY_MS + testDurationMillis, MILLISECONDS)) {
for (Thread thread : new Thread[] { offerer, remover, scanner }) { for (Thread thread : new Thread[] { offerer, remover, scanner }) {
if (thread.isAlive()) { if (thread.isAlive()) {
System.err.printf("Hung thread: %s%n", thread.getName()); System.err.printf("Hung thread: %s%n", thread.getName());

@ -103,7 +103,7 @@ public class BlockingTaskExecutor {
*/ */
static class NotificationReceiver { static class NotificationReceiver {
/** Has the notifiee been notified? */ /** Has the notifiee been notified? */
boolean notified = false; boolean notified;
/** /**
* Notify the notification receiver. * Notify the notification receiver.

@ -130,7 +130,7 @@ public final class CancelledFutureLoops {
long endTime = System.nanoTime(); long endTime = System.nanoTime();
long time = endTime - timer.startTime; long time = endTime - timer.startTime;
if (print) { if (print) {
double secs = (double)(time) / 1000000000.0; double secs = (double)time / 1000000000.0;
System.out.println("\t " + secs + "s run time"); System.out.println("\t " + secs + "s run time");
} }

@ -33,6 +33,7 @@
/* /*
* @test * @test
* @library /test/lib
* @run main DoneTimedGetLoops 300 * @run main DoneTimedGetLoops 300
* @summary isDone returning true guarantees that subsequent timed get * @summary isDone returning true guarantees that subsequent timed get
* will never throw TimeoutException. * will never throw TimeoutException.
@ -42,10 +43,12 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import jdk.test.lib.Utils;
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
public class DoneTimedGetLoops { public class DoneTimedGetLoops {
final long testDurationMillisDefault = 10L * 1000L; static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
final long testDurationMillisDefault = 10_000L;
final long testDurationMillis; final long testDurationMillis;
static class PublicFutureTask extends FutureTask<Boolean> { static class PublicFutureTask extends FutureTask<Boolean> {
@ -63,7 +66,6 @@ public class DoneTimedGetLoops {
void test(String[] args) throws Throwable { void test(String[] args) throws Throwable {
final long testDurationNanos = testDurationMillis * 1000L * 1000L; final long testDurationNanos = testDurationMillis * 1000L * 1000L;
final long quittingTimeNanos = System.nanoTime() + testDurationNanos; final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
final long timeoutMillis = 10L * 1000L;
final AtomicReference<PublicFutureTask> normalRef final AtomicReference<PublicFutureTask> normalRef
= new AtomicReference<>(); = new AtomicReference<>();
@ -136,13 +138,13 @@ public class DoneTimedGetLoops {
setterException, setterException,
doneTimedGetNormal, doneTimedGetNormal,
doneTimedGetAbnormal }) { doneTimedGetAbnormal }) {
thread.join(timeoutMillis + testDurationMillis); thread.join(LONG_DELAY_MS + testDurationMillis);
if (thread.isAlive()) { if (thread.isAlive()) {
System.err.printf("Hung thread: %s%n", thread.getName()); System.err.printf("Hung thread: %s%n", thread.getName());
failed++; failed++;
for (StackTraceElement e : thread.getStackTrace()) for (StackTraceElement e : thread.getStackTrace())
System.err.println(e); System.err.println(e);
thread.join(timeoutMillis); thread.join(LONG_DELAY_MS);
} }
} }
} }

@ -43,7 +43,7 @@ import java.util.concurrent.atomic.AtomicLong;
public class FickleRegister { public class FickleRegister {
final AtomicLong count = new AtomicLong(0); final AtomicLong count = new AtomicLong(0);
final long testDurationMillisDefault = 10L * 1000L; final long testDurationMillisDefault = 10_000L;
final long testDurationMillis; final long testDurationMillis;
final long quittingTimeNanos; final long quittingTimeNanos;
final int chunkSize = 1000; final int chunkSize = 1000;

@ -40,7 +40,7 @@
import java.util.concurrent.Phaser; import java.util.concurrent.Phaser;
public class TieredArriveLoops { public class TieredArriveLoops {
final long testDurationMillisDefault = 10L * 1000L; final long testDurationMillisDefault = 10_000L;
final long testDurationMillis; final long testDurationMillis;
final long quittingTimeNanos; final long quittingTimeNanos;

@ -86,7 +86,7 @@ public class GCRetention {
if (q.remove(1000) != null) if (q.remove(1000) != null)
break; break;
System.out.printf( System.out.printf(
"%d/%d unqueued references remaining%n", j, n); "%d/%d unqueued references remaining%n", j + 1, n);
} }
} }
} }

@ -24,6 +24,7 @@
/* @test /* @test
* @bug 5057341 6363898 * @bug 5057341 6363898
* @summary Basic tests for TimeUnit * @summary Basic tests for TimeUnit
* @library /test/lib
* @author Martin Buchholz * @author Martin Buchholz
*/ */
@ -39,12 +40,20 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import jdk.test.lib.Utils;
public class Basic { public class Basic {
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
private static void realMain(String[] args) throws Throwable { private static void realMain(String[] args) throws Throwable {
for (TimeUnit u : TimeUnit.values()) { for (TimeUnit u : TimeUnit.values()) {
@ -71,18 +80,33 @@ public class Basic {
equal(1000L, MILLISECONDS.toMicros(1)); equal(1000L, MILLISECONDS.toMicros(1));
equal(1000L, MICROSECONDS.toNanos(1)); equal(1000L, MICROSECONDS.toNanos(1));
long t0 = System.nanoTime(); //----------------------------------------------------------------
MILLISECONDS.sleep(3); /* See windows bug 6313903, might not sleep */ // TimeUnit.sleep sleeps for at least the specified time.
long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L); // TimeUnit.sleep(x, unit) for x <= 0 does not sleep at all.
System.out.printf("elapsed=%d%n", elapsedMillis); //----------------------------------------------------------------
check(elapsedMillis >= 0); ThreadLocalRandom rnd = ThreadLocalRandom.current();
/* Might not sleep on windows: check(elapsedMillis >= 3); */ int maxTimeoutMillis = rnd.nextInt(1, 12);
check(elapsedMillis < 1000); List<CompletableFuture<?>> workers =
IntStream.range(-1, maxTimeoutMillis + 1)
.mapToObj(timeoutMillis -> (Runnable) () -> {
try {
long startTime = System.nanoTime();
MILLISECONDS.sleep(timeoutMillis);
long elapsedNanos = System.nanoTime() - startTime;
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
check(elapsedNanos >= timeoutNanos);
} catch (InterruptedException fail) {
throw new AssertionError(fail);
}})
.map(CompletableFuture::runAsync)
.collect(Collectors.toList());
workers.forEach(CompletableFuture<?>::join);
//---------------------------------------------------------------- //----------------------------------------------------------------
// Tests for serialized form compatibility with previous release // Tests for serialized form compatibility with previous release
//---------------------------------------------------------------- //----------------------------------------------------------------
byte[] serializedForm = /* Generated using tiger */ byte[] serializedForm = /* Generated using JDK 5 */
{-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.', {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.',
'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e', 'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e',
'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0, 'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0,

@ -103,8 +103,8 @@ public class DoubleAdderDemo {
long total = (long)nthreads * incs; long total = (long)nthreads * incs;
if (sum != (double)total) if (sum != (double)total)
throw new Error(sum + " != " + total); throw new Error(sum + " != " + total);
double secs = (double)time / (1000L * 1000 * 1000); double secs = (double)time / 1000_000_000L;
long rate = total * (1000L) / time; long rate = total * 1000L / time;
System.out.printf("threads:%3d Time: %7.3fsec Incs per microsec: %4d\n", System.out.printf("threads:%3d Time: %7.3fsec Incs per microsec: %4d\n",
nthreads, secs, rate); nthreads, secs, rate);
} }

@ -39,7 +39,7 @@ import static java.util.concurrent.TimeUnit.NANOSECONDS;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject;
@ -1286,19 +1286,31 @@ public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
/** /**
* Tests scenario for * Tests scenario for
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
* ant -Djsr166.tckTestClass=AbstractQueuedLongSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck
*/ */
public void testInterruptedFailingAcquire() throws InterruptedException { public void testInterruptedFailingAcquire() throws Throwable {
final RuntimeException ex = new RuntimeException(); class PleaseThrow extends RuntimeException {}
final PleaseThrow ex = new PleaseThrow();
final AtomicBoolean thrown = new AtomicBoolean();
// A synchronizer only offering a choice of failure modes // A synchronizer only offering a choice of failure modes
class Sync extends AbstractQueuedLongSynchronizer { class Sync extends AbstractQueuedLongSynchronizer {
boolean pleaseThrow; volatile boolean pleaseThrow;
void maybeThrow() {
if (pleaseThrow) {
// assert: tryAcquire methods can throw at most once
if (! thrown.compareAndSet(false, true))
throw new AssertionError();
throw ex;
}
}
@Override protected boolean tryAcquire(long ignored) { @Override protected boolean tryAcquire(long ignored) {
if (pleaseThrow) throw ex; maybeThrow();
return false; return false;
} }
@Override protected long tryAcquireShared(long ignored) { @Override protected long tryAcquireShared(long ignored) {
if (pleaseThrow) throw ex; maybeThrow();
return -1; return -1;
} }
@Override protected boolean tryRelease(long ignored) { @Override protected boolean tryRelease(long ignored) {
@ -1310,30 +1322,87 @@ public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
} }
final Sync s = new Sync(); final Sync s = new Sync();
final boolean acquireInterruptibly = randomBoolean();
final Action[] uninterruptibleAcquireActions = {
() -> s.acquire(1),
() -> s.acquireShared(1),
};
final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
final Action[] interruptibleAcquireActions = {
() -> s.acquireInterruptibly(1),
() -> s.acquireSharedInterruptibly(1),
() -> s.tryAcquireNanos(1, nanosTimeout),
() -> s.tryAcquireSharedNanos(1, nanosTimeout),
};
final Action[] releaseActions = {
() -> s.release(1),
() -> s.releaseShared(1),
};
final Action acquireAction = acquireInterruptibly
? chooseRandomly(interruptibleAcquireActions)
: chooseRandomly(uninterruptibleAcquireActions);
final Action releaseAction
= chooseRandomly(releaseActions);
// From os_posix.cpp:
//
// NOTE that since there is no "lock" around the interrupt and
// is_interrupted operations, there is the possibility that the
// interrupted flag (in osThread) will be "false" but that the
// low-level events will be in the signaled state. This is
// intentional. The effect of this is that Object.wait() and
// LockSupport.park() will appear to have a spurious wakeup, which
// is allowed and not harmful, and the possibility is so rare that
// it is not worth the added complexity to add yet another lock.
final Thread thread = newStartedThread(new CheckedRunnable() { final Thread thread = newStartedThread(new CheckedRunnable() {
public void realRun() { public void realRun() throws Throwable {
try { try {
if (ThreadLocalRandom.current().nextBoolean()) acquireAction.run();
s.acquire(1);
else
s.acquireShared(1);
shouldThrow(); shouldThrow();
} catch (Throwable t) { } catch (InterruptedException possible) {
assertSame(ex, t); assertTrue(acquireInterruptibly);
assertTrue(Thread.interrupted()); assertFalse(Thread.interrupted());
} catch (PleaseThrow possible) {
awaitInterrupted();
} }
}}); }});
waitForThreadToEnterWaitState(thread); for (long startTime = 0L;; ) {
assertSame(thread, s.getFirstQueuedThread()); waitForThreadToEnterWaitState(thread);
assertTrue(s.hasQueuedPredecessors()); if (s.getFirstQueuedThread() == thread
assertTrue(s.hasQueuedThreads()); && s.hasQueuedPredecessors()
assertEquals(1, s.getQueueLength()); && s.hasQueuedThreads()
&& s.getQueueLength() == 1
&& s.hasContended())
break;
if (startTime == 0L)
startTime = System.nanoTime();
else if (millisElapsedSince(startTime) > LONG_DELAY_MS)
fail("timed out waiting for AQS state: "
+ "thread state=" + thread.getState()
+ ", queued threads=" + s.getQueuedThreads());
Thread.yield();
}
s.pleaseThrow = true; s.pleaseThrow = true;
thread.interrupt(); // release and interrupt, in random order
s.release(1); if (randomBoolean()) {
thread.interrupt();
releaseAction.run();
} else {
releaseAction.run();
thread.interrupt();
}
awaitTermination(thread); awaitTermination(thread);
if (! acquireInterruptibly)
assertTrue(thrown.get());
assertNull(s.getFirstQueuedThread());
assertFalse(s.hasQueuedPredecessors());
assertFalse(s.hasQueuedThreads());
assertEquals(0, s.getQueueLength());
assertTrue(s.getQueuedThreads().isEmpty());
assertTrue(s.hasContended());
} }
} }

@ -40,7 +40,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject; import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
@ -1337,19 +1337,31 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
/** /**
* Tests scenario for * Tests scenario for
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw * JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw
* ant -Djsr166.tckTestClass=AbstractQueuedSynchronizerTest -Djsr166.methodFilter=testInterruptedFailingAcquire -Djsr166.runsPerTest=10000 tck
*/ */
public void testInterruptedFailingAcquire() throws InterruptedException { public void testInterruptedFailingAcquire() throws Throwable {
final RuntimeException ex = new RuntimeException(); class PleaseThrow extends RuntimeException {}
final PleaseThrow ex = new PleaseThrow();
final AtomicBoolean thrown = new AtomicBoolean();
// A synchronizer only offering a choice of failure modes // A synchronizer only offering a choice of failure modes
class Sync extends AbstractQueuedSynchronizer { class Sync extends AbstractQueuedSynchronizer {
boolean pleaseThrow; volatile boolean pleaseThrow;
void maybeThrow() {
if (pleaseThrow) {
// assert: tryAcquire methods can throw at most once
if (! thrown.compareAndSet(false, true))
throw new AssertionError();
throw ex;
}
}
@Override protected boolean tryAcquire(int ignored) { @Override protected boolean tryAcquire(int ignored) {
if (pleaseThrow) throw ex; maybeThrow();
return false; return false;
} }
@Override protected int tryAcquireShared(int ignored) { @Override protected int tryAcquireShared(int ignored) {
if (pleaseThrow) throw ex; maybeThrow();
return -1; return -1;
} }
@Override protected boolean tryRelease(int ignored) { @Override protected boolean tryRelease(int ignored) {
@ -1361,30 +1373,87 @@ public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
} }
final Sync s = new Sync(); final Sync s = new Sync();
final boolean acquireInterruptibly = randomBoolean();
final Action[] uninterruptibleAcquireActions = {
() -> s.acquire(1),
() -> s.acquireShared(1),
};
final long nanosTimeout = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
final Action[] interruptibleAcquireActions = {
() -> s.acquireInterruptibly(1),
() -> s.acquireSharedInterruptibly(1),
() -> s.tryAcquireNanos(1, nanosTimeout),
() -> s.tryAcquireSharedNanos(1, nanosTimeout),
};
final Action[] releaseActions = {
() -> s.release(1),
() -> s.releaseShared(1),
};
final Action acquireAction = acquireInterruptibly
? chooseRandomly(interruptibleAcquireActions)
: chooseRandomly(uninterruptibleAcquireActions);
final Action releaseAction
= chooseRandomly(releaseActions);
// From os_posix.cpp:
//
// NOTE that since there is no "lock" around the interrupt and
// is_interrupted operations, there is the possibility that the
// interrupted flag (in osThread) will be "false" but that the
// low-level events will be in the signaled state. This is
// intentional. The effect of this is that Object.wait() and
// LockSupport.park() will appear to have a spurious wakeup, which
// is allowed and not harmful, and the possibility is so rare that
// it is not worth the added complexity to add yet another lock.
final Thread thread = newStartedThread(new CheckedRunnable() { final Thread thread = newStartedThread(new CheckedRunnable() {
public void realRun() { public void realRun() throws Throwable {
try { try {
if (ThreadLocalRandom.current().nextBoolean()) acquireAction.run();
s.acquire(1);
else
s.acquireShared(1);
shouldThrow(); shouldThrow();
} catch (Throwable t) { } catch (InterruptedException possible) {
assertSame(ex, t); assertTrue(acquireInterruptibly);
assertTrue(Thread.interrupted()); assertFalse(Thread.interrupted());
} catch (PleaseThrow possible) {
awaitInterrupted();
} }
}}); }});
waitForThreadToEnterWaitState(thread); for (long startTime = 0L;; ) {
assertSame(thread, s.getFirstQueuedThread()); waitForThreadToEnterWaitState(thread);
assertTrue(s.hasQueuedPredecessors()); if (s.getFirstQueuedThread() == thread
assertTrue(s.hasQueuedThreads()); && s.hasQueuedPredecessors()
assertEquals(1, s.getQueueLength()); && s.hasQueuedThreads()
&& s.getQueueLength() == 1
&& s.hasContended())
break;
if (startTime == 0L)
startTime = System.nanoTime();
else if (millisElapsedSince(startTime) > LONG_DELAY_MS)
fail("timed out waiting for AQS state: "
+ "thread state=" + thread.getState()
+ ", queued threads=" + s.getQueuedThreads());
Thread.yield();
}
s.pleaseThrow = true; s.pleaseThrow = true;
thread.interrupt(); // release and interrupt, in random order
s.release(1); if (randomBoolean()) {
thread.interrupt();
releaseAction.run();
} else {
releaseAction.run();
thread.interrupt();
}
awaitTermination(thread); awaitTermination(thread);
if (! acquireInterruptibly)
assertTrue(thrown.get());
assertNull(s.getFirstQueuedThread());
assertFalse(s.hasQueuedPredecessors());
assertFalse(s.hasQueuedThreads());
assertEquals(0, s.getQueueLength());
assertTrue(s.getQueuedThreads().isEmpty());
assertTrue(s.hasContended());
} }
} }

@ -61,7 +61,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
class Implementation implements CollectionImplementation { class Implementation implements CollectionImplementation {
public Class<?> klazz() { return ArrayBlockingQueue.class; } public Class<?> klazz() { return ArrayBlockingQueue.class; }
public Collection emptyCollection() { public Collection emptyCollection() {
boolean fair = ThreadLocalRandom.current().nextBoolean(); boolean fair = randomBoolean();
return populatedQueue(0, SIZE, 2 * SIZE, fair); return populatedQueue(0, SIZE, 2 * SIZE, fair);
} }
public Object makeElement(int i) { return i; } public Object makeElement(int i) { return i; }
@ -367,7 +367,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(SIZE, q.size()); assertEquals(SIZE, q.size());
@ -409,7 +409,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
assertEquals(0, q.take()); assertEquals(0, q.take());
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(0, q.remainingCapacity()); assertEquals(0, q.remainingCapacity());
@ -431,21 +431,21 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -486,7 +486,7 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -539,29 +539,26 @@ public class ArrayBlockingQueueTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkEmpty(q); checkEmpty(q);

@ -253,7 +253,7 @@ public abstract class BlockingQueueTest extends JSR166TestCase {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
@ -274,7 +274,7 @@ public abstract class BlockingQueueTest extends JSR166TestCase {
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
barrier.await(); barrier.await();
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -296,7 +296,7 @@ public abstract class BlockingQueueTest extends JSR166TestCase {
}}); }});
await(threadStarted); await(threadStarted);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -325,19 +325,19 @@ public abstract class BlockingQueueTest extends JSR166TestCase {
*/ */
public void testTimedPollFromEmptyBlocksInterruptibly() { public void testTimedPollFromEmptyBlocksInterruptibly() {
final BlockingQueue q = emptyCollection(); final BlockingQueue q = emptyCollection();
final CountDownLatch threadStarted = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() { public void realRun() {
threadStarted.countDown(); pleaseInterrupt.countDown();
try { try {
q.poll(2 * LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(threadStarted); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -352,7 +352,7 @@ public abstract class BlockingQueueTest extends JSR166TestCase {
public void realRun() { public void realRun() {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(2 * LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());

@ -940,7 +940,7 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase {
} }
void runAsync(Runnable r1, Runnable r2) { void runAsync(Runnable r1, Runnable r2) {
boolean b = ThreadLocalRandom.current().nextBoolean(); boolean b = randomBoolean();
CompletableFuture<Void> f1 = CompletableFuture.runAsync(b ? r1 : r2); CompletableFuture<Void> f1 = CompletableFuture.runAsync(b ? r1 : r2);
CompletableFuture<Void> f2 = CompletableFuture.runAsync(b ? r2 : r1); CompletableFuture<Void> f2 = CompletableFuture.runAsync(b ? r2 : r1);
f1.join(); f1.join();
@ -1003,10 +1003,6 @@ public class ConcurrentLinkedDequeTest extends JSR166TestCase {
} }
} }
<T> T chooseRandomly(T... choices) {
return choices[ThreadLocalRandom.current().nextInt(choices.length)];
}
/** /**
* Non-traversing Deque operations (that return null) are linearizable. * Non-traversing Deque operations (that return null) are linearizable.
* Don't return null when the deque is observably never empty. * Don't return null when the deque is observably never empty.

@ -36,6 +36,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@ -99,7 +100,7 @@ public class CountDownLatchTest extends JSR166TestCase {
assertEquals(2, l.getCount()); assertEquals(2, l.getCount());
l.countDown(); l.countDown();
assertEquals(1, l.getCount()); assertEquals(1, l.getCount());
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
l.countDown(); l.countDown();
assertEquals(0, l.getCount()); assertEquals(0, l.getCount());
awaitTermination(t); awaitTermination(t);
@ -124,7 +125,7 @@ public class CountDownLatchTest extends JSR166TestCase {
assertEquals(2, l.getCount()); assertEquals(2, l.getCount());
l.countDown(); l.countDown();
assertEquals(1, l.getCount()); assertEquals(1, l.getCount());
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
l.countDown(); l.countDown();
assertEquals(0, l.getCount()); assertEquals(0, l.getCount());
awaitTermination(t); awaitTermination(t);
@ -156,7 +157,7 @@ public class CountDownLatchTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -165,29 +166,30 @@ public class CountDownLatchTest extends JSR166TestCase {
* timed await throws InterruptedException if interrupted before counted down * timed await throws InterruptedException if interrupted before counted down
*/ */
public void testTimedAwait_Interruptible() { public void testTimedAwait_Interruptible() {
final CountDownLatch l = new CountDownLatch(1); final int initialCount = ThreadLocalRandom.current().nextInt(1, 3);
final CountDownLatch l = new CountDownLatch(initialCount);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
l.await(LONG_DELAY_MS, MILLISECONDS); l.await(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
l.await(LONG_DELAY_MS, MILLISECONDS); l.await(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertEquals(1, l.getCount()); assertEquals(initialCount, l.getCount());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -200,7 +202,11 @@ public class CountDownLatchTest extends JSR166TestCase {
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
assertEquals(1, l.getCount()); assertEquals(1, l.getCount());
long startTime = System.nanoTime();
assertFalse(l.await(timeoutMillis(), MILLISECONDS)); assertFalse(l.await(timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
assertEquals(1, l.getCount()); assertEquals(1, l.getCount());
}}); }});

@ -42,7 +42,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.Test; import junit.framework.Test;
@ -322,34 +321,6 @@ public class CyclicBarrierTest extends JSR166TestCase {
awaitTermination(t2); awaitTermination(t2);
} }
/**
* All threads block while a barrier is broken.
*/
public void testReset_Leakage() throws InterruptedException {
final CyclicBarrier c = new CyclicBarrier(2);
final AtomicBoolean done = new AtomicBoolean();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
while (!done.get()) {
try {
while (c.isBroken())
c.reset();
c.await();
shouldThrow();
}
catch (BrokenBarrierException | InterruptedException ok) {}
}}});
for (int i = 0; i < 4; i++) {
delay(timeoutMillis());
t.interrupt();
}
done.set(true);
t.interrupt();
awaitTermination(t);
}
/** /**
* Reset of a non-broken barrier does not break barrier * Reset of a non-broken barrier does not break barrier
*/ */
@ -505,7 +476,7 @@ public class CyclicBarrierTest extends JSR166TestCase {
final ExecutorService e = Executors.newFixedThreadPool(nTasks); final ExecutorService e = Executors.newFixedThreadPool(nTasks);
final Runnable awaiter = () -> { final Runnable awaiter = () -> {
try { try {
if (ThreadLocalRandom.current().nextBoolean()) if (randomBoolean())
barrier.await(); barrier.await();
else else
barrier.await(LONG_DELAY_MS, MILLISECONDS); barrier.await(LONG_DELAY_MS, MILLISECONDS);

@ -332,7 +332,7 @@ public class DelayQueueTest extends JSR166TestCase {
} }
/** /**
* timed offer does not time out * Queue is unbounded, so timed offer never times out
*/ */
public void testTimedOffer() throws InterruptedException { public void testTimedOffer() throws InterruptedException {
final DelayQueue q = new DelayQueue(); final DelayQueue q = new DelayQueue();
@ -384,7 +384,7 @@ public class DelayQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -436,30 +436,27 @@ public class DelayQueueTest extends JSR166TestCase {
final DelayQueue q = populatedQueue(SIZE); final DelayQueue q = populatedQueue(SIZE);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(new PDelay(i), assertEquals(new PDelay(i),
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS)));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkEmpty(q); checkEmpty(q);

@ -156,7 +156,7 @@ public class DoubleAccumulatorTest extends JSR166TestCase {
= new DoubleAccumulator((x, y) -> x + y, 0.0); = new DoubleAccumulator((x, y) -> x + y, 0.0);
final int nThreads = ThreadLocalRandom.current().nextInt(1, 5); final int nThreads = ThreadLocalRandom.current().nextInt(1, 5);
final Phaser phaser = new Phaser(nThreads + 1); final Phaser phaser = new Phaser(nThreads + 1);
final int incs = 1_000_000; final int incs = expensiveTests ? 1_000_000 : 100_000;
final double total = nThreads * incs/2.0 * (incs - 1); // Gauss final double total = nThreads * incs/2.0 * (incs - 1); // Gauss
final Runnable task = () -> { final Runnable task = () -> {
phaser.arriveAndAwaitAdvance(); phaser.arriveAndAwaitAdvance();

@ -38,7 +38,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream; import java.util.stream.Stream;
import junit.framework.Test; import junit.framework.Test;
@ -81,7 +80,7 @@ public class ForkJoinPool9Test extends JSR166TestCase {
Thread currentThread = Thread.currentThread(); Thread currentThread = Thread.currentThread();
Stream.of(systemClassLoader, null).forEach(cl -> { Stream.of(systemClassLoader, null).forEach(cl -> {
if (ThreadLocalRandom.current().nextBoolean()) if (randomBoolean())
// should always be permitted, without effect // should always be permitted, without effect
currentThread.setContextClassLoader(cl); currentThread.setContextClassLoader(cl);
}); });

@ -747,7 +747,7 @@ public class FutureTaskTest extends JSR166TestCase {
/** /**
* get is interruptible * get is interruptible
*/ */
public void testGet_interruptible() { public void testGet_Interruptible() {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
final FutureTask task = new FutureTask(new NoOpCallable()); final FutureTask task = new FutureTask(new NoOpCallable());
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
@ -776,27 +776,28 @@ public class FutureTaskTest extends JSR166TestCase {
/** /**
* timed get is interruptible * timed get is interruptible
*/ */
public void testTimedGet_interruptible() { public void testTimedGet_Interruptible() {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
final FutureTask task = new FutureTask(new NoOpCallable()); final FutureTask task = new FutureTask(new NoOpCallable());
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception { public void realRun() throws Exception {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
task.get(2*LONG_DELAY_MS, MILLISECONDS); task.get(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
task.get(2*LONG_DELAY_MS, MILLISECONDS); task.get(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkNotDone(task); checkNotDone(task);

@ -311,12 +311,13 @@ public class JSR166TestCase extends TestCase {
static volatile TestCase currentTestCase; static volatile TestCase currentTestCase;
// static volatile int currentRun = 0; // static volatile int currentRun = 0;
static { static {
Runnable checkForWedgedTest = new Runnable() { public void run() { Runnable wedgedTestDetector = new Runnable() { public void run() {
// Avoid spurious reports with enormous runsPerTest. // Avoid spurious reports with enormous runsPerTest.
// A single test case run should never take more than 1 second. // A single test case run should never take more than 1 second.
// But let's cap it at the high end too ... // But let's cap it at the high end too ...
final int timeoutMinutes = final int timeoutMinutesMin = Math.max(runsPerTest / 60, 1)
Math.min(15, Math.max(runsPerTest / 60, 1)); * Math.max((int) delayFactor, 1);
final int timeoutMinutes = Math.min(15, timeoutMinutesMin);
for (TestCase lastTestCase = currentTestCase;;) { for (TestCase lastTestCase = currentTestCase;;) {
try { MINUTES.sleep(timeoutMinutes); } try { MINUTES.sleep(timeoutMinutes); }
catch (InterruptedException unexpected) { break; } catch (InterruptedException unexpected) { break; }
@ -336,7 +337,7 @@ public class JSR166TestCase extends TestCase {
} }
lastTestCase = currentTestCase; lastTestCase = currentTestCase;
}}}; }}};
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); Thread thread = new Thread(wedgedTestDetector, "WedgedTestDetector");
thread.setDaemon(true); thread.setDaemon(true);
thread.start(); thread.start();
} }
@ -380,7 +381,7 @@ public class JSR166TestCase extends TestCase {
// Never report first run of any test; treat it as a // Never report first run of any test; treat it as a
// warmup run, notably to trigger all needed classloading, // warmup run, notably to trigger all needed classloading,
if (i > 0) if (i > 0)
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); System.out.printf("%s: %d%n", toString(), elapsedMillis);
} }
} }
@ -683,6 +684,12 @@ public class JSR166TestCase extends TestCase {
public static long MEDIUM_DELAY_MS; public static long MEDIUM_DELAY_MS;
public static long LONG_DELAY_MS; public static long LONG_DELAY_MS;
/**
* A delay significantly longer than LONG_DELAY_MS.
* Use this in a thread that is waited for via awaitTermination(Thread).
*/
public static long LONGER_DELAY_MS;
private static final long RANDOM_TIMEOUT; private static final long RANDOM_TIMEOUT;
private static final long RANDOM_EXPIRED_TIMEOUT; private static final long RANDOM_EXPIRED_TIMEOUT;
private static final TimeUnit RANDOM_TIMEUNIT; private static final TimeUnit RANDOM_TIMEUNIT;
@ -710,6 +717,20 @@ public class JSR166TestCase extends TestCase {
*/ */
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; }
/**
* Returns a random boolean; a "coin flip".
*/
static boolean randomBoolean() {
return ThreadLocalRandom.current().nextBoolean();
}
/**
* Returns a random element from given choices.
*/
<T> T chooseRandomly(T... choices) {
return choices[ThreadLocalRandom.current().nextInt(choices.length)];
}
/** /**
* Returns the shortest timed delay. This can be scaled up for * Returns the shortest timed delay. This can be scaled up for
* slow machines using the jsr166.delay.factor system property, * slow machines using the jsr166.delay.factor system property,
@ -728,6 +749,7 @@ public class JSR166TestCase extends TestCase {
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
LONG_DELAY_MS = SHORT_DELAY_MS * 200; LONG_DELAY_MS = SHORT_DELAY_MS * 200;
LONGER_DELAY_MS = 2 * LONG_DELAY_MS;
} }
private static final long TIMEOUT_DELAY_MS private static final long TIMEOUT_DELAY_MS
@ -766,8 +788,8 @@ public class JSR166TestCase extends TestCase {
*/ */
public void threadRecordFailure(Throwable t) { public void threadRecordFailure(Throwable t) {
System.err.println(t); System.err.println(t);
dumpTestThreads(); if (threadFailure.compareAndSet(null, t))
threadFailure.compareAndSet(null, t); dumpTestThreads();
} }
public void setUp() { public void setUp() {
@ -1088,6 +1110,39 @@ public class JSR166TestCase extends TestCase {
} }
} }
/** Returns true if thread info might be useful in a thread dump. */
static boolean threadOfInterest(ThreadInfo info) {
final String name = info.getThreadName();
String lockName;
if (name == null)
return true;
if (name.equals("Signal Dispatcher")
|| name.equals("WedgedTestDetector"))
return false;
if (name.equals("Reference Handler")) {
// Reference Handler stacktrace changed in JDK-8156500
StackTraceElement[] stackTrace; String methodName;
if ((stackTrace = info.getStackTrace()) != null
&& stackTrace.length > 0
&& (methodName = stackTrace[0].getMethodName()) != null
&& methodName.equals("waitForReferencePendingList"))
return false;
// jdk8 Reference Handler stacktrace
if ((lockName = info.getLockName()) != null
&& lockName.startsWith("java.lang.ref"))
return false;
}
if ((name.equals("Finalizer") || name.equals("Common-Cleaner"))
&& (lockName = info.getLockName()) != null
&& lockName.startsWith("java.lang.ref"))
return false;
if (name.startsWith("ForkJoinPool.commonPool-worker")
&& (lockName = info.getLockName()) != null
&& lockName.startsWith("java.util.concurrent.ForkJoinPool"))
return false;
return true;
}
/** /**
* A debugging tool to print stack traces of most threads, as jstack does. * A debugging tool to print stack traces of most threads, as jstack does.
* Uninteresting threads are filtered out. * Uninteresting threads are filtered out.
@ -1104,23 +1159,9 @@ public class JSR166TestCase extends TestCase {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
System.err.println("------ stacktrace dump start ------"); System.err.println("------ stacktrace dump start ------");
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true))
final String name = info.getThreadName(); if (threadOfInterest(info))
String lockName; System.err.print(info);
if ("Signal Dispatcher".equals(name))
continue;
if ("Reference Handler".equals(name)
&& (lockName = info.getLockName()) != null
&& lockName.startsWith("java.lang.ref.Reference$Lock"))
continue;
if ("Finalizer".equals(name)
&& (lockName = info.getLockName()) != null
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock"))
continue;
if ("checkForWedgedTest".equals(name))
continue;
System.err.print(info);
}
System.err.println("------ stacktrace dump end ------"); System.err.println("------ stacktrace dump end ------");
if (sm != null) System.setSecurityManager(sm); if (sm != null) System.setSecurityManager(sm);
@ -1392,6 +1433,20 @@ public class JSR166TestCase extends TestCase {
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot);
} }
/**
* Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to
* be interrupted. Clears the interrupt status before returning.
*/
void awaitInterrupted() {
for (long startTime = 0L; !Thread.interrupted(); ) {
if (startTime == 0L)
startTime = System.nanoTime();
else if (millisElapsedSince(startTime) > LONG_DELAY_MS)
fail("timed out waiting for thread interrupt");
Thread.yield();
}
}
/** /**
* Returns the number of milliseconds since time given by * Returns the number of milliseconds since time given by
* startNanoTime, which must have been previously returned from a * startNanoTime, which must have been previously returned from a
@ -1401,19 +1456,6 @@ public class JSR166TestCase extends TestCase {
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
} }
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) {
// long startTime = System.nanoTime();
// try {
// r.run();
// } catch (Throwable fail) { threadUnexpectedException(fail); }
// if (millisElapsedSince(startTime) > timeoutMillis/2)
// throw new AssertionError("did not return promptly");
// }
// void assertTerminatesPromptly(Runnable r) {
// assertTerminatesPromptly(LONG_DELAY_MS/2, r);
// }
/** /**
* Checks that timed f.get() returns the expected value, and does not * Checks that timed f.get() returns the expected value, and does not
* wait for the timeout to elapse before returning. * wait for the timeout to elapse before returning.
@ -1448,15 +1490,21 @@ public class JSR166TestCase extends TestCase {
* to terminate (using {@link Thread#join(long)}), else interrupts * to terminate (using {@link Thread#join(long)}), else interrupts
* the thread (in the hope that it may terminate later) and fails. * the thread (in the hope that it may terminate later) and fails.
*/ */
void awaitTermination(Thread t, long timeoutMillis) { void awaitTermination(Thread thread, long timeoutMillis) {
try { try {
t.join(timeoutMillis); thread.join(timeoutMillis);
} catch (InterruptedException fail) { } catch (InterruptedException fail) {
threadUnexpectedException(fail); threadUnexpectedException(fail);
} finally { }
if (t.getState() != Thread.State.TERMINATED) { if (thread.getState() != Thread.State.TERMINATED) {
t.interrupt(); String detail = String.format(
threadFail("timed out waiting for thread to terminate"); "timed out waiting for thread to terminate, thread=%s, state=%s" ,
thread, thread.getState());
try {
threadFail(detail);
} finally {
// Interrupt thread __after__ having reported its stack trace
thread.interrupt();
} }
} }
} }

@ -631,7 +631,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(SIZE, q.size()); assertEquals(SIZE, q.size());
@ -673,7 +673,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
assertEquals(0, q.take()); assertEquals(0, q.take());
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(0, q.remainingCapacity()); assertEquals(0, q.remainingCapacity());
@ -690,26 +690,27 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
q.put(new Object()); q.put(new Object());
q.put(new Object()); q.put(new Object());
long startTime = System.nanoTime(); long startTime = System.nanoTime();
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -750,7 +751,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -802,29 +803,26 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkEmpty(q); checkEmpty(q);
@ -883,7 +881,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(SIZE, q.size()); assertEquals(SIZE, q.size());
@ -918,7 +916,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
assertEquals(capacity - 1, q.take()); assertEquals(capacity - 1, q.take());
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(0, q.remainingCapacity()); assertEquals(0, q.remainingCapacity());
@ -935,26 +933,27 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
q.putFirst(new Object()); q.putFirst(new Object());
q.putFirst(new Object()); q.putFirst(new Object());
long startTime = System.nanoTime(); long startTime = System.nanoTime();
assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offerFirst(new Object(), randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offerFirst(new Object(), LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -986,7 +985,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(threadStarted); await(threadStarted);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -1027,7 +1026,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(threadStarted); await(threadStarted);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -1077,7 +1076,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -1118,29 +1117,26 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); q.pollFirst(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); q.pollFirst(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -1164,7 +1160,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); q.pollFirst(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
@ -1174,6 +1170,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
@ -1182,7 +1179,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
barrier.await(); barrier.await();
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -1240,7 +1237,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(SIZE, q.size()); assertEquals(SIZE, q.size());
@ -1282,7 +1279,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
assertEquals(0, q.take()); assertEquals(0, q.take());
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(0, q.remainingCapacity()); assertEquals(0, q.remainingCapacity());
@ -1299,24 +1296,25 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
q.putLast(new Object()); q.putLast(new Object());
q.putLast(new Object()); q.putLast(new Object());
long startTime = System.nanoTime(); long startTime = System.nanoTime();
assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offerLast(new Object(), randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offerLast(new Object(), LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -1358,7 +1356,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -1399,30 +1397,27 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(SIZE - i - 1, assertEquals(SIZE - i - 1,
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); q.pollLast(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.pollLast(LONG_DELAY_MS, MILLISECONDS); q.pollLast(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.pollLast(LONG_DELAY_MS, MILLISECONDS); q.pollLast(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkEmpty(q); checkEmpty(q);
@ -1447,7 +1442,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
@ -1468,7 +1463,7 @@ public class LinkedBlockingDequeTest extends JSR166TestCase {
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
barrier.await(); barrier.await();
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }

@ -318,7 +318,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(SIZE, q.size()); assertEquals(SIZE, q.size());
@ -360,7 +360,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase {
assertEquals(0, q.take()); assertEquals(0, q.take());
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(0, q.remainingCapacity()); assertEquals(0, q.remainingCapacity());
@ -376,28 +376,28 @@ public class LinkedBlockingQueueTest extends JSR166TestCase {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
q.put(new Object()); q.put(new Object());
q.put(new Object()); q.put(new Object());
long startTime = System.nanoTime(); long startTime = System.nanoTime();
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -438,7 +438,7 @@ public class LinkedBlockingQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -490,29 +490,26 @@ public class LinkedBlockingQueueTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkEmpty(q); checkEmpty(q);

@ -254,7 +254,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -308,29 +308,26 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkEmpty(q); checkEmpty(q);
@ -344,16 +341,14 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
final BlockingQueue<Integer> q = populatedQueue(SIZE); final BlockingQueue<Integer> q = populatedQueue(SIZE);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
for (int i = 0; i < SIZE; ++i) for (int i = 0; i < SIZE; ++i)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(i, (int) q.poll(randomTimeout(), randomTimeUnit()));
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
awaitTermination(t); awaitTermination(t);
@ -982,25 +977,23 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); q.tryTransfer(new Object(), randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); q.tryTransfer(new Object(), LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
checkEmpty(q); checkEmpty(q);

@ -150,7 +150,7 @@ public class LongAccumulatorTest extends JSR166TestCase {
= new LongAccumulator((x, y) -> x + y, 0L); = new LongAccumulator((x, y) -> x + y, 0L);
final int nThreads = ThreadLocalRandom.current().nextInt(1, 5); final int nThreads = ThreadLocalRandom.current().nextInt(1, 5);
final Phaser phaser = new Phaser(nThreads + 1); final Phaser phaser = new Phaser(nThreads + 1);
final int incs = 1_000_000; final int incs = expensiveTests ? 1_000_000 : 100_000;
final long total = nThreads * incs/2L * (incs - 1); // Gauss final long total = nThreads * incs/2L * (incs - 1); // Gauss
final Runnable task = () -> { final Runnable task = () -> {
phaser.arriveAndAwaitAdvance(); phaser.arriveAndAwaitAdvance();

@ -122,6 +122,7 @@ public class MapTest extends JSR166TestCase {
*/ */
public void testBug8186171() { public void testBug8186171() {
if (!impl.supportsSetValue()) return; if (!impl.supportsSetValue()) return;
if (!atLeastJava10()) return; // jdk9 is no longer maintained
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final boolean permitsNullValues = impl.permitsNullValues(); final boolean permitsNullValues = impl.permitsNullValues();
final Object v1 = (permitsNullValues && rnd.nextBoolean()) final Object v1 = (permitsNullValues && rnd.nextBoolean())

@ -480,7 +480,7 @@ public class PhaserTest extends JSR166TestCase {
/** /**
* awaitAdvanceInterruptibly blocks interruptibly * awaitAdvanceInterruptibly blocks interruptibly
*/ */
public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException { public void testAwaitAdvanceInterruptibly_Interruptible() throws InterruptedException {
final Phaser phaser = new Phaser(1); final Phaser phaser = new Phaser(1);
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
@ -505,14 +505,14 @@ public class PhaserTest extends JSR166TestCase {
public void realRun() throws TimeoutException { public void realRun() throws TimeoutException {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); phaser.awaitAdvanceInterruptibly(0, randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); phaser.awaitAdvanceInterruptibly(0, LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
@ -520,8 +520,8 @@ public class PhaserTest extends JSR166TestCase {
await(pleaseInterrupt); await(pleaseInterrupt);
assertState(phaser, 0, 1, 1); assertState(phaser, 0, 1, 1);
assertThreadBlocks(t1, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t1, Thread.State.WAITING);
assertThreadBlocks(t2, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t2, Thread.State.TIMED_WAITING);
t1.interrupt(); t1.interrupt();
t2.interrupt(); t2.interrupt();
awaitTermination(t1); awaitTermination(t1);

@ -346,7 +346,7 @@ public class PriorityBlockingQueueTest extends JSR166TestCase {
} }
/** /**
* timed offer does not time out * Queue is unbounded, so timed offer never times out
*/ */
public void testTimedOffer() { public void testTimedOffer() {
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
@ -397,7 +397,7 @@ public class PriorityBlockingQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -449,29 +449,26 @@ public class PriorityBlockingQueueTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }

@ -707,7 +707,7 @@ public class ScheduledExecutorSubclassTest extends JSR166TestCase {
Runnable waiter = new CheckedRunnable() { public void realRun() { Runnable waiter = new CheckedRunnable() { public void realRun() {
threadsStarted.countDown(); threadsStarted.countDown();
try { try {
MILLISECONDS.sleep(2 * LONG_DELAY_MS); MILLISECONDS.sleep(LONGER_DELAY_MS);
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
ran.getAndIncrement(); ran.getAndIncrement();
}}; }};

@ -666,7 +666,7 @@ public class ScheduledExecutorTest extends JSR166TestCase {
Runnable waiter = new CheckedRunnable() { public void realRun() { Runnable waiter = new CheckedRunnable() { public void realRun() {
threadsStarted.countDown(); threadsStarted.countDown();
try { try {
MILLISECONDS.sleep(2 * LONG_DELAY_MS); MILLISECONDS.sleep(LONGER_DELAY_MS);
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
ran.getAndIncrement(); ran.getAndIncrement();
}}; }};

@ -38,7 +38,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@ -220,24 +219,22 @@ public class SemaphoreTest extends JSR166TestCase {
/** /**
* timed tryAcquire times out * timed tryAcquire times out
*/ */
public void testTryAcquire_timeout() { public void testTryAcquire_timeout() throws InterruptedException {
final boolean fair = ThreadLocalRandom.current().nextBoolean(); final boolean fair = randomBoolean();
final Semaphore s = new Semaphore(0, fair); final Semaphore s = new Semaphore(0, fair);
final long startTime = System.nanoTime(); final long startTime = System.nanoTime();
try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); } assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS));
catch (InterruptedException e) { threadUnexpectedException(e); }
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
} }
/** /**
* timed tryAcquire(N) times out * timed tryAcquire(N) times out
*/ */
public void testTryAcquireN_timeout() { public void testTryAcquireN_timeout() throws InterruptedException {
final boolean fair = ThreadLocalRandom.current().nextBoolean(); final boolean fair = randomBoolean();
final Semaphore s = new Semaphore(2, fair); final Semaphore s = new Semaphore(2, fair);
final long startTime = System.nanoTime(); final long startTime = System.nanoTime();
try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); } assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS));
catch (InterruptedException e) { threadUnexpectedException(e); }
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
} }

@ -45,7 +45,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue; import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadLocalRandom;
import junit.framework.Test; import junit.framework.Test;
@ -166,7 +165,7 @@ public class SynchronousQueueTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(0, q.remainingCapacity()); assertEquals(0, q.remainingCapacity());
@ -207,7 +206,7 @@ public class SynchronousQueueTest extends JSR166TestCase {
catch (InterruptedException e) { threadUnexpectedException(e); } catch (InterruptedException e) { threadUnexpectedException(e); }
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
assertEquals(0, q.remainingCapacity()); assertEquals(0, q.remainingCapacity());
@ -217,32 +216,33 @@ public class SynchronousQueueTest extends JSR166TestCase {
* timed offer times out if elements not taken * timed offer times out if elements not taken
*/ */
public void testTimedOffer() { public void testTimedOffer() {
final boolean fair = ThreadLocalRandom.current().nextBoolean(); final boolean fair = randomBoolean();
final SynchronousQueue q = new SynchronousQueue(fair); final SynchronousQueue q = new SynchronousQueue(fair);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
long startTime = System.nanoTime(); long startTime = System.nanoTime();
assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); q.offer(new Object(), LONGER_DELAY_MS, MILLISECONDS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -272,7 +272,7 @@ public class SynchronousQueueTest extends JSR166TestCase {
* timed poll with nonzero timeout times out if no active putter * timed poll with nonzero timeout times out if no active putter
*/ */
public void testTimedPoll() { public void testTimedPoll() {
final boolean fair = ThreadLocalRandom.current().nextBoolean(); final boolean fair = randomBoolean();
final SynchronousQueue q = new SynchronousQueue(fair); final SynchronousQueue q = new SynchronousQueue(fair);
final long startTime = System.nanoTime(); final long startTime = System.nanoTime();
try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); }
@ -285,7 +285,7 @@ public class SynchronousQueueTest extends JSR166TestCase {
* after offer succeeds; on interruption throws * after offer succeeds; on interruption throws
*/ */
public void testTimedPollWithOffer() { public void testTimedPollWithOffer() {
final boolean fair = ThreadLocalRandom.current().nextBoolean(); final boolean fair = randomBoolean();
final SynchronousQueue q = new SynchronousQueue(fair); final SynchronousQueue q = new SynchronousQueue(fair);
final CountDownLatch pleaseOffer = new CountDownLatch(1); final CountDownLatch pleaseOffer = new CountDownLatch(1);
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
@ -301,7 +301,7 @@ public class SynchronousQueueTest extends JSR166TestCase {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
q.poll(LONG_DELAY_MS, MILLISECONDS); q.poll(randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
@ -323,7 +323,7 @@ public class SynchronousQueueTest extends JSR166TestCase {
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }

@ -796,7 +796,7 @@ public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
Runnable waiter = new CheckedRunnable() { public void realRun() { Runnable waiter = new CheckedRunnable() { public void realRun() {
threadsStarted.countDown(); threadsStarted.countDown();
try { try {
MILLISECONDS.sleep(2 * LONG_DELAY_MS); MILLISECONDS.sleep(LONGER_DELAY_MS);
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
ran.getAndIncrement(); ran.getAndIncrement();
}}; }};
@ -1669,7 +1669,7 @@ public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
l.add(latchAwaitingStringTask(latch)); l.add(latchAwaitingStringTask(latch));
l.add(null); l.add(null);
try { try {
e.invokeAny(l, randomTimeout(), MILLISECONDS); e.invokeAny(l, randomTimeout(), randomTimeUnit());
shouldThrow(); shouldThrow();
} catch (NullPointerException success) {} } catch (NullPointerException success) {}
latch.countDown(); latch.countDown();

@ -699,7 +699,7 @@ public class ThreadPoolExecutorTest extends JSR166TestCase {
Runnable waiter = new CheckedRunnable() { public void realRun() { Runnable waiter = new CheckedRunnable() { public void realRun() {
threadsStarted.countDown(); threadsStarted.countDown();
try { try {
MILLISECONDS.sleep(2 * LONG_DELAY_MS); MILLISECONDS.sleep(LONGER_DELAY_MS);
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
ran.getAndIncrement(); ran.getAndIncrement();
}}; }};

@ -56,243 +56,56 @@ public class TimeUnitTest extends JSR166TestCase {
return new TestSuite(TimeUnitTest.class); return new TestSuite(TimeUnitTest.class);
} }
// (loops to 88888 check increments at all time divisions.) void testConversion(TimeUnit x, TimeUnit y, long n, long expected) {
assertEquals(expected, x.convert(n, y));
switch (x) {
case NANOSECONDS: assertEquals(expected, y.toNanos(n)); break;
case MICROSECONDS: assertEquals(expected, y.toMicros(n)); break;
case MILLISECONDS: assertEquals(expected, y.toMillis(n)); break;
case SECONDS: assertEquals(expected, y.toSeconds(n)); break;
case MINUTES: assertEquals(expected, y.toMinutes(n)); break;
case HOURS: assertEquals(expected, y.toHours(n)); break;
case DAYS: assertEquals(expected, y.toDays(n)); break;
default: throw new AssertionError();
}
if (n > 0) testConversion(x, y, -n, -expected);
}
void testConversion(TimeUnit x, TimeUnit y) {
long ratio = x.toNanos(1)/y.toNanos(1);
assertTrue(ratio > 0);
long[] ns = { 0, 1, 2, Long.MAX_VALUE/ratio, Long.MIN_VALUE/ratio };
for (long n : ns) {
testConversion(y, x, n, n * ratio);
long[] ks = { n * ratio, n * ratio + 1, n * ratio - 1 };
for (long k : ks) {
testConversion(x, y, k, k / ratio);
}
}
}
/** /**
* convert correctly converts sample values across the units * Conversion methods correctly convert sample values
*/ */
public void testConvert() { public void testConversions() {
for (long t = 0; t < 88888; ++t) { // Sanity check
assertEquals(t*60*60*24, assertEquals(1, NANOSECONDS.toNanos(1));
SECONDS.convert(t, DAYS)); assertEquals(1000L * NANOSECONDS.toNanos(1), MICROSECONDS.toNanos(1));
assertEquals(t*60*60, assertEquals(1000L * MICROSECONDS.toNanos(1), MILLISECONDS.toNanos(1));
SECONDS.convert(t, HOURS)); assertEquals(1000L * MILLISECONDS.toNanos(1), SECONDS.toNanos(1));
assertEquals(t*60, assertEquals(60L * SECONDS.toNanos(1), MINUTES.toNanos(1));
SECONDS.convert(t, MINUTES)); assertEquals(60L * MINUTES.toNanos(1), HOURS.toNanos(1));
assertEquals(t, assertEquals(24L * HOURS.toNanos(1), DAYS.toNanos(1));
SECONDS.convert(t, SECONDS));
assertEquals(t,
SECONDS.convert(1000L*t, MILLISECONDS));
assertEquals(t,
SECONDS.convert(1000000L*t, MICROSECONDS));
assertEquals(t,
SECONDS.convert(1000000000L*t, NANOSECONDS));
assertEquals(1000L*t*60*60*24,
MILLISECONDS.convert(t, DAYS));
assertEquals(1000L*t*60*60,
MILLISECONDS.convert(t, HOURS));
assertEquals(1000L*t*60,
MILLISECONDS.convert(t, MINUTES));
assertEquals(1000L*t,
MILLISECONDS.convert(t, SECONDS));
assertEquals(t,
MILLISECONDS.convert(t, MILLISECONDS));
assertEquals(t,
MILLISECONDS.convert(1000L*t, MICROSECONDS));
assertEquals(t,
MILLISECONDS.convert(1000000L*t, NANOSECONDS));
assertEquals(1000000L*t*60*60*24,
MICROSECONDS.convert(t, DAYS));
assertEquals(1000000L*t*60*60,
MICROSECONDS.convert(t, HOURS));
assertEquals(1000000L*t*60,
MICROSECONDS.convert(t, MINUTES));
assertEquals(1000000L*t,
MICROSECONDS.convert(t, SECONDS));
assertEquals(1000L*t,
MICROSECONDS.convert(t, MILLISECONDS));
assertEquals(t,
MICROSECONDS.convert(t, MICROSECONDS));
assertEquals(t,
MICROSECONDS.convert(1000L*t, NANOSECONDS));
assertEquals(1000000000L*t*60*60*24,
NANOSECONDS.convert(t, DAYS));
assertEquals(1000000000L*t*60*60,
NANOSECONDS.convert(t, HOURS));
assertEquals(1000000000L*t*60,
NANOSECONDS.convert(t, MINUTES));
assertEquals(1000000000L*t,
NANOSECONDS.convert(t, SECONDS));
assertEquals(1000000L*t,
NANOSECONDS.convert(t, MILLISECONDS));
assertEquals(1000L*t,
NANOSECONDS.convert(t, MICROSECONDS));
assertEquals(t,
NANOSECONDS.convert(t, NANOSECONDS));
}
for (TimeUnit x : TimeUnit.values()) { for (TimeUnit x : TimeUnit.values()) {
long[] zs = { assertEquals(x.toNanos(1), NANOSECONDS.convert(1, x));
0, 1, -1,
Integer.MAX_VALUE, Integer.MIN_VALUE,
Long.MAX_VALUE, Long.MIN_VALUE,
};
for (long z : zs) assertEquals(z, x.convert(z, x));
} }
}
/** for (TimeUnit x : TimeUnit.values())
* toNanos correctly converts sample values in different units to for (TimeUnit y : TimeUnit.values())
* nanoseconds if (x.toNanos(1) >= y.toNanos(1))
*/ testConversion(x, y);
public void testToNanos() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t*1000000000L*60*60*24,
DAYS.toNanos(t));
assertEquals(t*1000000000L*60*60,
HOURS.toNanos(t));
assertEquals(t*1000000000L*60,
MINUTES.toNanos(t));
assertEquals(1000000000L*t,
SECONDS.toNanos(t));
assertEquals(1000000L*t,
MILLISECONDS.toNanos(t));
assertEquals(1000L*t,
MICROSECONDS.toNanos(t));
assertEquals(t,
NANOSECONDS.toNanos(t));
}
}
/**
* toMicros correctly converts sample values in different units to
* microseconds
*/
public void testToMicros() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t*1000000L*60*60*24,
DAYS.toMicros(t));
assertEquals(t*1000000L*60*60,
HOURS.toMicros(t));
assertEquals(t*1000000L*60,
MINUTES.toMicros(t));
assertEquals(1000000L*t,
SECONDS.toMicros(t));
assertEquals(1000L*t,
MILLISECONDS.toMicros(t));
assertEquals(t,
MICROSECONDS.toMicros(t));
assertEquals(t,
NANOSECONDS.toMicros(t*1000L));
}
}
/**
* toMillis correctly converts sample values in different units to
* milliseconds
*/
public void testToMillis() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t*1000L*60*60*24,
DAYS.toMillis(t));
assertEquals(t*1000L*60*60,
HOURS.toMillis(t));
assertEquals(t*1000L*60,
MINUTES.toMillis(t));
assertEquals(1000L*t,
SECONDS.toMillis(t));
assertEquals(t,
MILLISECONDS.toMillis(t));
assertEquals(t,
MICROSECONDS.toMillis(t*1000L));
assertEquals(t,
NANOSECONDS.toMillis(t*1000000L));
}
}
/**
* toSeconds correctly converts sample values in different units to
* seconds
*/
public void testToSeconds() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t*60*60*24,
DAYS.toSeconds(t));
assertEquals(t*60*60,
HOURS.toSeconds(t));
assertEquals(t*60,
MINUTES.toSeconds(t));
assertEquals(t,
SECONDS.toSeconds(t));
assertEquals(t,
MILLISECONDS.toSeconds(t*1000L));
assertEquals(t,
MICROSECONDS.toSeconds(t*1000000L));
assertEquals(t,
NANOSECONDS.toSeconds(t*1000000000L));
}
}
/**
* toMinutes correctly converts sample values in different units to
* minutes
*/
public void testToMinutes() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t*60*24,
DAYS.toMinutes(t));
assertEquals(t*60,
HOURS.toMinutes(t));
assertEquals(t,
MINUTES.toMinutes(t));
assertEquals(t,
SECONDS.toMinutes(t*60));
assertEquals(t,
MILLISECONDS.toMinutes(t*1000L*60));
assertEquals(t,
MICROSECONDS.toMinutes(t*1000000L*60));
assertEquals(t,
NANOSECONDS.toMinutes(t*1000000000L*60));
}
}
/**
* toHours correctly converts sample values in different units to
* hours
*/
public void testToHours() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t*24,
DAYS.toHours(t));
assertEquals(t,
HOURS.toHours(t));
assertEquals(t,
MINUTES.toHours(t*60));
assertEquals(t,
SECONDS.toHours(t*60*60));
assertEquals(t,
MILLISECONDS.toHours(t*1000L*60*60));
assertEquals(t,
MICROSECONDS.toHours(t*1000000L*60*60));
assertEquals(t,
NANOSECONDS.toHours(t*1000000000L*60*60));
}
}
/**
* toDays correctly converts sample values in different units to
* days
*/
public void testToDays() {
for (long t = 0; t < 88888; ++t) {
assertEquals(t,
DAYS.toDays(t));
assertEquals(t,
HOURS.toDays(t*24));
assertEquals(t,
MINUTES.toDays(t*60*24));
assertEquals(t,
SECONDS.toDays(t*60*60*24));
assertEquals(t,
MILLISECONDS.toDays(t*1000L*60*60*24));
assertEquals(t,
MICROSECONDS.toDays(t*1000000L*60*60*24));
assertEquals(t,
NANOSECONDS.toDays(t*1000000000L*60*60*24));
}
} }
/** /**
@ -494,14 +307,21 @@ public class TimeUnitTest extends JSR166TestCase {
* toString returns name of unit * toString returns name of unit
*/ */
public void testToString() { public void testToString() {
assertEquals("NANOSECONDS", NANOSECONDS.toString());
assertEquals("MICROSECONDS", MICROSECONDS.toString());
assertEquals("MILLISECONDS", MILLISECONDS.toString());
assertEquals("SECONDS", SECONDS.toString()); assertEquals("SECONDS", SECONDS.toString());
assertEquals("MINUTES", MINUTES.toString());
assertEquals("HOURS", HOURS.toString());
assertEquals("DAYS", DAYS.toString());
} }
/** /**
* name returns name of unit * name returns name of unit
*/ */
public void testName() { public void testName() {
assertEquals("SECONDS", SECONDS.name()); for (TimeUnit x : TimeUnit.values())
assertEquals(x.toString(), x.name());
} }
/** /**
@ -512,10 +332,8 @@ public class TimeUnitTest extends JSR166TestCase {
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
Object o = new Object(); Object o = new Object();
TimeUnit tu = MILLISECONDS;
try { try {
tu.timedWait(o, LONG_DELAY_MS); MILLISECONDS.timedWait(o, LONGER_DELAY_MS);
threadShouldThrow(); threadShouldThrow();
} catch (IllegalMonitorStateException success) {} } catch (IllegalMonitorStateException success) {}
}}); }});
@ -531,12 +349,11 @@ public class TimeUnitTest extends JSR166TestCase {
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
Object o = new Object(); Object o = new Object();
TimeUnit tu = MILLISECONDS;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
synchronized (o) { synchronized (o) {
tu.timedWait(o, LONG_DELAY_MS); MILLISECONDS.timedWait(o, LONGER_DELAY_MS);
} }
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
@ -545,7 +362,7 @@ public class TimeUnitTest extends JSR166TestCase {
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
synchronized (o) { synchronized (o) {
tu.timedWait(o, LONG_DELAY_MS); MILLISECONDS.timedWait(o, LONGER_DELAY_MS);
} }
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
@ -553,7 +370,7 @@ public class TimeUnitTest extends JSR166TestCase {
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
@ -565,28 +382,27 @@ public class TimeUnitTest extends JSR166TestCase {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
Thread.sleep(LONG_DELAY_MS); Thread.sleep(LONGER_DELAY_MS);
}}); }});
final Thread t = newStartedThread(new CheckedRunnable() { final Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
TimeUnit tu = MILLISECONDS;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
tu.timedJoin(s, LONG_DELAY_MS); MILLISECONDS.timedJoin(s, LONGER_DELAY_MS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
tu.timedJoin(s, LONG_DELAY_MS); MILLISECONDS.timedJoin(s, LONGER_DELAY_MS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
s.interrupt(); s.interrupt();
@ -594,34 +410,45 @@ public class TimeUnitTest extends JSR166TestCase {
} }
/** /**
* timedSleep throws InterruptedException when interrupted * timeUnit.sleep throws InterruptedException when interrupted
*/ */
public void testTimedSleep_Interruptible() { public void testTimedSleep_Interruptible() {
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() { Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException { public void realRun() throws InterruptedException {
TimeUnit tu = MILLISECONDS;
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
try { try {
tu.sleep(LONG_DELAY_MS); MILLISECONDS.sleep(LONGER_DELAY_MS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
pleaseInterrupt.countDown(); pleaseInterrupt.countDown();
try { try {
tu.sleep(LONG_DELAY_MS); MILLISECONDS.sleep(LONGER_DELAY_MS);
shouldThrow(); shouldThrow();
} catch (InterruptedException success) {} } catch (InterruptedException success) {}
assertFalse(Thread.interrupted()); assertFalse(Thread.interrupted());
}}); }});
await(pleaseInterrupt); await(pleaseInterrupt);
assertThreadBlocks(t, Thread.State.TIMED_WAITING); if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
t.interrupt(); t.interrupt();
awaitTermination(t); awaitTermination(t);
} }
/**
* timeUnit.sleep(x) for x <= 0 does not sleep at all.
*/
public void testTimedSleep_nonPositive() throws InterruptedException {
boolean interrupt = randomBoolean();
if (interrupt) Thread.currentThread().interrupt();
randomTimeUnit().sleep(0L);
randomTimeUnit().sleep(-1L);
randomTimeUnit().sleep(Long.MIN_VALUE);
if (interrupt) assertTrue(Thread.interrupted());
}
/** /**
* a deserialized/reserialized unit is the same instance * a deserialized/reserialized unit is the same instance
*/ */