8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21

Reviewed-by: martin, chegar, shade
This commit is contained in:
Doug Lea 2016-09-23 13:24:33 -07:00
parent edc7565f51
commit c7cf1788ed
20 changed files with 112 additions and 99 deletions

@ -1191,7 +1191,7 @@ public class ForkJoinPool extends AbstractExecutorService {
* Default idle timeout value (in milliseconds) for the thread
* triggering quiescence to park waiting for new work
*/
private static final long DEFAULT_KEEPALIVE = 60000L;
private static final long DEFAULT_KEEPALIVE = 60_000L;
/**
* Undershoot tolerance for idle timeouts
@ -2303,7 +2303,6 @@ public class ForkJoinPool extends AbstractExecutorService {
throw new NullPointerException();
long ms = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP);
String prefix = "ForkJoinPool-" + nextPoolId() + "-worker-";
int corep = Math.min(Math.max(corePoolSize, parallelism), MAX_CAP);
long c = ((((long)(-corep) << TC_SHIFT) & TC_MASK) |
(((long)(-parallelism) << RC_SHIFT) & RC_MASK));
@ -2315,8 +2314,8 @@ public class ForkJoinPool extends AbstractExecutorService {
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
n = (n + 1) << 1; // power of two, including space for submission queues
this.workerNamePrefix = "ForkJoinPool-" + nextPoolId() + "-worker-";
this.workQueues = new WorkQueue[n];
this.workerNamePrefix = prefix;
this.factory = factory;
this.ueh = handler;
this.saturate = saturate;
@ -2327,11 +2326,19 @@ public class ForkJoinPool extends AbstractExecutorService {
checkPermission();
}
private Object newInstanceFromSystemProperty(String property)
throws ReflectiveOperationException {
String className = System.getProperty(property);
return (className == null)
? null
: ClassLoader.getSystemClassLoader().loadClass(className)
.getConstructor().newInstance();
}
/**
* Constructor for common pool using parameters possibly
* overridden by system properties
*/
@SuppressWarnings("deprecation") // Class.newInstance
private ForkJoinPool(byte forCommonPoolOnly) {
int parallelism = -1;
ForkJoinWorkerThreadFactory fac = null;
@ -2339,18 +2346,12 @@ public class ForkJoinPool extends AbstractExecutorService {
try { // ignore exceptions in accessing/parsing properties
String pp = System.getProperty
("java.util.concurrent.ForkJoinPool.common.parallelism");
String fp = System.getProperty
("java.util.concurrent.ForkJoinPool.common.threadFactory");
String hp = System.getProperty
("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
if (pp != null)
parallelism = Integer.parseInt(pp);
if (fp != null)
fac = ((ForkJoinWorkerThreadFactory)ClassLoader.
getSystemClassLoader().loadClass(fp).newInstance());
if (hp != null)
handler = ((UncaughtExceptionHandler)ClassLoader.
getSystemClassLoader().loadClass(hp).newInstance());
fac = (ForkJoinWorkerThreadFactory) newInstanceFromSystemProperty(
"java.util.concurrent.ForkJoinPool.common.threadFactory");
handler = (UncaughtExceptionHandler) newInstanceFromSystemProperty(
"java.util.concurrent.ForkJoinPool.common.exceptionHandler");
} catch (Exception ignore) {
}
@ -2373,8 +2374,8 @@ public class ForkJoinPool extends AbstractExecutorService {
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16;
n = (n + 1) << 1;
this.workQueues = new WorkQueue[n];
this.workerNamePrefix = "ForkJoinPool.commonPool-worker-";
this.workQueues = new WorkQueue[n];
this.factory = fac;
this.ueh = handler;
this.saturate = null;

@ -35,6 +35,9 @@
package java.util.concurrent.atomic;
import static java.lang.Double.doubleToRawLongBits;
import static java.lang.Double.longBitsToDouble;
import java.io.Serializable;
import java.util.function.DoubleBinaryOperator;
@ -91,7 +94,7 @@ public class DoubleAccumulator extends Striped64 implements Serializable {
public DoubleAccumulator(DoubleBinaryOperator accumulatorFunction,
double identity) {
this.function = accumulatorFunction;
base = this.identity = Double.doubleToRawLongBits(identity);
base = this.identity = doubleToRawLongBits(identity);
}
/**
@ -101,18 +104,19 @@ public class DoubleAccumulator extends Striped64 implements Serializable {
*/
public void accumulate(double x) {
Cell[] as; long b, v, r; int m; Cell a;
if ((as = cells) != null ||
(r = Double.doubleToRawLongBits
(function.applyAsDouble
(Double.longBitsToDouble(b = base), x))) != b && !casBase(b, r)) {
if ((as = cells) != null
|| ((r = doubleToRawLongBits
(function.applyAsDouble(longBitsToDouble(b = base), x))) != b
&& !casBase(b, r))) {
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 ||
(a = as[getProbe() & m]) == null ||
!(uncontended =
(r = Double.doubleToRawLongBits
(function.applyAsDouble
(Double.longBitsToDouble(v = a.value), x))) == v ||
a.cas(v, r)))
if (as == null
|| (m = as.length - 1) < 0
|| (a = as[getProbe() & m]) == null
|| !(uncontended =
((r = doubleToRawLongBits
(function.applyAsDouble
(longBitsToDouble(v = a.value), x))) == v)
|| a.cas(v, r)))
doubleAccumulate(x, function, uncontended);
}
}
@ -128,12 +132,12 @@ public class DoubleAccumulator extends Striped64 implements Serializable {
*/
public double get() {
Cell[] as = cells;
double result = Double.longBitsToDouble(base);
double result = longBitsToDouble(base);
if (as != null) {
for (Cell a : as)
if (a != null)
result = function.applyAsDouble
(result, Double.longBitsToDouble(a.value));
(result, longBitsToDouble(a.value));
}
return result;
}
@ -168,12 +172,12 @@ public class DoubleAccumulator extends Striped64 implements Serializable {
*/
public double getThenReset() {
Cell[] as = cells;
double result = Double.longBitsToDouble(base);
double result = longBitsToDouble(base);
base = identity;
if (as != null) {
for (Cell a : as) {
if (a != null) {
double v = Double.longBitsToDouble(a.value);
double v = longBitsToDouble(a.value);
a.reset(identity);
result = function.applyAsDouble(result, v);
}
@ -267,9 +271,9 @@ public class DoubleAccumulator extends Striped64 implements Serializable {
* held by this proxy
*/
private Object readResolve() {
double d = Double.longBitsToDouble(identity);
double d = longBitsToDouble(identity);
DoubleAccumulator a = new DoubleAccumulator(function, d);
a.base = Double.doubleToRawLongBits(value);
a.base = doubleToRawLongBits(value);
return a;
}
}

@ -103,14 +103,16 @@ public class LongAccumulator extends Striped64 implements Serializable {
*/
public void accumulate(long x) {
Cell[] as; long b, v, r; int m; Cell a;
if ((as = cells) != null ||
(r = function.applyAsLong(b = base, x)) != b && !casBase(b, r)) {
if ((as = cells) != null
|| ((r = function.applyAsLong(b = base, x)) != b
&& !casBase(b, r))) {
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 ||
(a = as[getProbe() & m]) == null ||
!(uncontended =
(r = function.applyAsLong(v = a.value, x)) == v ||
a.cas(v, r)))
if (as == null
|| (m = as.length - 1) < 0
|| (a = as[getProbe() & m]) == null
|| !(uncontended =
(r = function.applyAsLong(v = a.value, x)) == v
|| a.cas(v, r)))
longAccumulate(x, function, uncontended);
}
}

@ -33,7 +33,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
@ -41,10 +40,8 @@ import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import static org.testng.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertSame;
public class EmptyNavigableMap {

@ -41,10 +41,9 @@ import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import static org.testng.Assert.fail;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
public class EmptyNavigableSet {

@ -28,8 +28,14 @@
* @author Martin Buchholz
*/
import java.util.*;
import java.util.concurrent.*;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.LinkedBlockingDeque;
public class ChorusLine {
private interface Tweaker {

@ -28,7 +28,11 @@
* @author Martin Buchholz
*/
import java.util.*;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
public class ForgetMeNot {
private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {

@ -37,7 +37,13 @@
* @summary Checks that a priority queue returns elements in sorted order across various operations
*/
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.PriorityQueue;
public class PriorityQueueSort {

@ -28,8 +28,17 @@
* @author Martin Buchholz
*/
import java.util.*;
import java.util.concurrent.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
public class RemoveContains {
static volatile int passed = 0, failed = 0;

@ -31,8 +31,6 @@
*/
import static java.util.concurrent.Executors.defaultThreadFactory;
import static java.util.concurrent.Executors.newFixedThreadPool;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

@ -303,7 +303,7 @@ public class AtomicIntegerArrayTest extends JSR166TestCase {
class Counter extends CheckedRunnable {
final AtomicIntegerArray aa;
volatile int counts;
int decs;
Counter(AtomicIntegerArray a) { aa = a; }
public void realRun() {
for (;;) {
@ -314,7 +314,7 @@ public class AtomicIntegerArrayTest extends JSR166TestCase {
if (v != 0) {
done = false;
if (aa.compareAndSet(i, v, v - 1))
++counts;
decs++;
}
}
if (done)
@ -334,13 +334,11 @@ public class AtomicIntegerArrayTest extends JSR166TestCase {
aa.set(i, countdown);
Counter c1 = new Counter(aa);
Counter c2 = new Counter(aa);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
t1.start();
t2.start();
Thread t1 = newStartedThread(c1);
Thread t2 = newStartedThread(c2);
t1.join();
t2.join();
assertEquals(c1.counts+c2.counts, SIZE * countdown);
assertEquals(c1.decs + c2.decs, SIZE * countdown);
}
/**

@ -302,7 +302,7 @@ public class AtomicLongArrayTest extends JSR166TestCase {
class Counter extends CheckedRunnable {
final AtomicLongArray aa;
volatile long counts;
int decs;
Counter(AtomicLongArray a) { aa = a; }
public void realRun() {
for (;;) {
@ -313,7 +313,7 @@ public class AtomicLongArrayTest extends JSR166TestCase {
if (v != 0) {
done = false;
if (aa.compareAndSet(i, v, v - 1))
++counts;
decs++;
}
}
if (done)
@ -333,13 +333,11 @@ public class AtomicLongArrayTest extends JSR166TestCase {
aa.set(i, countdown);
Counter c1 = new Counter(aa);
Counter c2 = new Counter(aa);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
t1.start();
t2.start();
Thread t1 = newStartedThread(c1);
Thread t2 = newStartedThread(c2);
t1.join();
t2.join();
assertEquals(c1.counts+c2.counts, SIZE * countdown);
assertEquals(c1.decs + c2.decs, SIZE * countdown);
}
/**

@ -1024,7 +1024,7 @@ public class ConcurrentSkipListMapTest extends JSR166TestCase {
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
NavigableMap<Integer, Integer> result =
(NavigableMap<Integer, Integer>) cl.newInstance();
(NavigableMap<Integer, Integer>) cl.getConstructor().newInstance();
assertEquals(0, result.size());
assertFalse(result.keySet().iterator().hasNext());
return result;

@ -725,7 +725,8 @@ public class ConcurrentSkipListSetTest extends JSR166TestCase {
}
static NavigableSet<Integer> newSet(Class cl) throws Exception {
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
NavigableSet<Integer> result =
(NavigableSet<Integer>) cl.getConstructor().newInstance();
assertEquals(0, result.size());
assertFalse(result.iterator().hasNext());
return result;

@ -40,6 +40,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.Test;
import junit.framework.TestSuite;
@ -52,11 +53,6 @@ public class CyclicBarrierTest extends JSR166TestCase {
return new TestSuite(CyclicBarrierTest.class);
}
private volatile int countAction;
private class MyAction implements Runnable {
public void run() { ++countAction; }
}
/**
* Spin-waits till the number of waiters == numberOfWaiters.
*/
@ -114,14 +110,16 @@ public class CyclicBarrierTest extends JSR166TestCase {
* The supplied barrier action is run at barrier
*/
public void testBarrierAction() throws Exception {
countAction = 0;
CyclicBarrier b = new CyclicBarrier(1, new MyAction());
final AtomicInteger count = new AtomicInteger(0);
final Runnable incCount = new Runnable() { public void run() {
count.getAndIncrement(); }};
CyclicBarrier b = new CyclicBarrier(1, incCount);
assertEquals(1, b.getParties());
assertEquals(0, b.getNumberWaiting());
b.await();
b.await();
assertEquals(0, b.getNumberWaiting());
assertEquals(2, countAction);
assertEquals(2, count.get());
}
/**

@ -121,10 +121,8 @@ public class DelayQueueTest extends JSR166TestCase {
}
public boolean equals(Object other) {
return equals((NanoDelay)other);
}
public boolean equals(NanoDelay other) {
return other.trigger == trigger;
return (other instanceof NanoDelay) &&
this.trigger == ((NanoDelay)other).trigger;
}
// suppress [overrides] javac warning

@ -51,6 +51,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import junit.framework.AssertionFailedError;
@ -84,13 +85,6 @@ public class ForkJoinPoolTest extends JSR166TestCase {
// Some classes to test extension and factory methods
static class MyHandler implements Thread.UncaughtExceptionHandler {
volatile int catches = 0;
public void uncaughtException(Thread t, Throwable e) {
++catches;
}
}
static class MyError extends Error {}
// to test handlers
@ -101,9 +95,9 @@ public class ForkJoinPoolTest extends JSR166TestCase {
static class FailingThreadFactory
implements ForkJoinPool.ForkJoinWorkerThreadFactory {
volatile int calls = 0;
final AtomicInteger calls = new AtomicInteger(0);
public ForkJoinWorkerThread newThread(ForkJoinPool p) {
if (++calls > 1) return null;
if (calls.incrementAndGet() > 1) return null;
return new FailingFJWSubclass(p);
}
}

@ -537,15 +537,14 @@ public class ScheduledExecutorTest extends JSR166TestCase {
* isShutdown is false before shutdown, true after
*/
public void testIsShutdown() {
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
try {
assertFalse(p.isShutdown());
assertFalse(p.isShutdown());
try (PoolCleaner cleaner = cleaner(p)) {
try {
p.shutdown();
assertTrue(p.isShutdown());
} catch (SecurityException ok) {}
}
finally {
try { p.shutdown(); } catch (SecurityException ok) { return; }
}
assertTrue(p.isShutdown());
}
/**

@ -829,7 +829,7 @@ public class TreeMapTest extends JSR166TestCase {
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
NavigableMap<Integer, Integer> result
= (NavigableMap<Integer, Integer>) cl.newInstance();
= (NavigableMap<Integer, Integer>) cl.getConstructor().newInstance();
assertEquals(0, result.size());
assertFalse(result.keySet().iterator().hasNext());
return result;

@ -722,7 +722,8 @@ public class TreeSetTest extends JSR166TestCase {
}
static NavigableSet<Integer> newSet(Class cl) throws Exception {
NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
NavigableSet<Integer> result =
(NavigableSet<Integer>) cl.getConstructor().newInstance();
assertEquals(0, result.size());
assertFalse(result.iterator().hasNext());
return result;