8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
Reviewed-by: martin, psandoz
--- a/src/java.base/share/classes/java/util/ArrayDeque.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/ArrayDeque.java Tue Apr 10 11:37:18 2018 -0700
@@ -208,7 +208,7 @@
*/
public ArrayDeque(Collection<? extends E> c) {
this(c.size());
- addAll(c);
+ copyElements(c);
}
/**
@@ -322,8 +322,12 @@
final int s, needed;
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
grow(needed);
+ copyElements(c);
+ return size() > s;
+ }
+
+ private void copyElements(Collection<? extends E> c) {
c.forEach(this::addLast);
- return size() > s;
}
/**
--- a/src/java.base/share/classes/java/util/concurrent/CompletableFuture.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/CompletableFuture.java Tue Apr 10 11:37:18 2018 -0700
@@ -2883,7 +2883,7 @@
STACK = l.findVarHandle(CompletableFuture.class, "stack", Completion.class);
NEXT = l.findVarHandle(Completion.class, "next", Completion.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java Tue Apr 10 11:37:18 2018 -0700
@@ -6383,7 +6383,7 @@
ABASE = U.arrayBaseOffset(Node[].class);
int scale = U.arrayIndexScale(Node[].class);
if ((scale & (scale - 1)) != 0)
- throw new Error("array index scale not a power of two");
+ throw new ExceptionInInitializerError("array index scale not a power of two");
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java Tue Apr 10 11:37:18 2018 -0700
@@ -1671,7 +1671,7 @@
NEXT = l.findVarHandle(Node.class, "next", Node.class);
ITEM = l.findVarHandle(Node.class, "item", Object.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
--- a/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java Tue Apr 10 11:37:18 2018 -0700
@@ -1069,7 +1069,7 @@
ITEM = l.findVarHandle(Node.class, "item", Object.class);
NEXT = l.findVarHandle(Node.class, "next", Node.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
--- a/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java Tue Apr 10 11:37:18 2018 -0700
@@ -3412,7 +3412,7 @@
VAL = l.findVarHandle(Node.class, "val", Object.class);
RIGHT = l.findVarHandle(Index.class, "right", Index.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
--- a/src/java.base/share/classes/java/util/concurrent/CountedCompleter.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/CountedCompleter.java Tue Apr 10 11:37:18 2018 -0700
@@ -775,7 +775,7 @@
PENDING = l.findVarHandle(CountedCompleter.class, "pending", int.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
--- a/src/java.base/share/classes/java/util/concurrent/Exchanger.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/Exchanger.java Tue Apr 10 11:37:18 2018 -0700
@@ -641,7 +641,7 @@
MATCH = l.findVarHandle(Node.class, "match", Object.class);
AA = MethodHandles.arrayElementVarHandle(Node[].class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
--- a/src/java.base/share/classes/java/util/concurrent/ForkJoinTask.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/ForkJoinTask.java Tue Apr 10 11:37:18 2018 -0700
@@ -1540,7 +1540,7 @@
MethodHandles.Lookup l = MethodHandles.lookup();
STATUS = l.findVarHandle(ForkJoinTask.class, "status", int.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
--- a/src/java.base/share/classes/java/util/concurrent/FutureTask.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/FutureTask.java Tue Apr 10 11:37:18 2018 -0700
@@ -526,7 +526,7 @@
RUNNER = l.findVarHandle(FutureTask.class, "runner", Thread.class);
WAITERS = l.findVarHandle(FutureTask.class, "waiters", WaitNode.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/LinkedTransferQueue.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/LinkedTransferQueue.java Tue Apr 10 11:37:18 2018 -0700
@@ -1739,7 +1739,7 @@
NEXT = l.findVarHandle(Node.class, "next", Node.class);
WAITER = l.findVarHandle(Node.class, "waiter", Thread.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/Phaser.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/Phaser.java Tue Apr 10 11:37:18 2018 -0700
@@ -1137,7 +1137,7 @@
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = l.findVarHandle(Phaser.class, "state", long.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java Tue Apr 10 11:37:18 2018 -0700
@@ -1014,7 +1014,7 @@
"allocationSpinLock",
int.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
--- a/src/java.base/share/classes/java/util/concurrent/SubmissionPublisher.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/SubmissionPublisher.java Tue Apr 10 11:37:18 2018 -0700
@@ -1096,7 +1096,7 @@
if (cap > 0) {
boolean added;
if (n >= cap && cap < maxCapacity) // resize
- added = growAndoffer(item, a, t);
+ added = growAndOffer(item, a, t);
else if (n >= cap || unowned) // need volatile CAS
added = QA.compareAndSet(a, i, null, item);
else { // can use release mode
@@ -1115,7 +1115,7 @@
* Tries to expand buffer and add item, returning true on
* success. Currently fails only if out of memory.
*/
- final boolean growAndoffer(T item, Object[] a, int t) {
+ final boolean growAndOffer(T item, Object[] a, int t) {
int cap = 0, newCap = 0;
Object[] newArray = null;
if (a != null && (cap = a.length) > 0 && (newCap = cap << 1) > 0) {
@@ -1466,7 +1466,7 @@
long.class);
QA = MethodHandles.arrayElementVarHandle(Object[].class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/SynchronousQueue.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/SynchronousQueue.java Tue Apr 10 11:37:18 2018 -0700
@@ -293,7 +293,7 @@
SMATCH = l.findVarHandle(SNode.class, "match", SNode.class);
SNEXT = l.findVarHandle(SNode.class, "next", SNode.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
@@ -516,7 +516,7 @@
MethodHandles.Lookup l = MethodHandles.lookup();
SHEAD = l.findVarHandle(TransferStack.class, "head", SNode.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
@@ -583,7 +583,7 @@
QITEM = l.findVarHandle(QNode.class, "item", Object.class);
QNEXT = l.findVarHandle(QNode.class, "next", QNode.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
@@ -830,7 +830,7 @@
QCLEANME = l.findVarHandle(TransferQueue.class, "cleanMe",
QNode.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
--- a/src/java.base/share/classes/java/util/concurrent/atomic/AtomicBoolean.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/atomic/AtomicBoolean.java Tue Apr 10 11:37:18 2018 -0700
@@ -56,7 +56,7 @@
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
--- a/src/java.base/share/classes/java/util/concurrent/atomic/AtomicMarkableReference.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/atomic/AtomicMarkableReference.java Tue Apr 10 11:37:18 2018 -0700
@@ -199,7 +199,7 @@
PAIR = l.findVarHandle(AtomicMarkableReference.class, "pair",
Pair.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
--- a/src/java.base/share/classes/java/util/concurrent/atomic/AtomicReference.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/atomic/AtomicReference.java Tue Apr 10 11:37:18 2018 -0700
@@ -56,7 +56,7 @@
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(AtomicReference.class, "value", Object.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
--- a/src/java.base/share/classes/java/util/concurrent/atomic/AtomicStampedReference.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/atomic/AtomicStampedReference.java Tue Apr 10 11:37:18 2018 -0700
@@ -199,7 +199,7 @@
PAIR = l.findVarHandle(AtomicStampedReference.class, "pair",
Pair.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
--- a/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java Tue Apr 10 11:37:18 2018 -0700
@@ -144,7 +144,7 @@
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(Cell.class, "value", long.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
@@ -396,13 +396,13 @@
try {
return MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup());
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}});
THREAD_PROBE = l.findVarHandle(Thread.class,
"threadLocalRandomProbe", int.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
--- a/src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java Tue Apr 10 11:37:18 2018 -0700
@@ -1830,7 +1830,7 @@
HEAD = l.findVarHandle(AbstractQueuedLongSynchronizer.class, "head", Node.class);
TAIL = l.findVarHandle(AbstractQueuedLongSynchronizer.class, "tail", Node.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedSynchronizer.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedSynchronizer.java Tue Apr 10 11:37:18 2018 -0700
@@ -555,7 +555,7 @@
THREAD = l.findVarHandle(Node.class, "thread", Thread.class);
WAITSTATUS = l.findVarHandle(Node.class, "waitStatus", int.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
@@ -2308,7 +2308,7 @@
HEAD = l.findVarHandle(AbstractQueuedSynchronizer.class, "head", Node.class);
TAIL = l.findVarHandle(AbstractQueuedSynchronizer.class, "tail", Node.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
--- a/src/java.base/share/classes/java/util/concurrent/locks/StampedLock.java Tue Apr 10 11:33:29 2018 -0700
+++ b/src/java.base/share/classes/java/util/concurrent/locks/StampedLock.java Tue Apr 10 11:37:18 2018 -0700
@@ -1614,7 +1614,7 @@
WNEXT = l.findVarHandle(WNode.class, "next", WNode.class);
WCOWAIT = l.findVarHandle(WNode.class, "cowait", WNode.class);
} catch (ReflectiveOperationException e) {
- throw new Error(e);
+ throw new ExceptionInInitializerError(e);
}
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/util/concurrent/ConcurrentHashMap/WhiteBox.java Tue Apr 10 11:37:18 2018 -0700
@@ -0,0 +1,155 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * This file is available under and governed by the GNU General Public
+ * License version 2 only, as published by the Free Software Foundation.
+ * However, the following notice accompanied the original version of this
+ * file:
+ *
+ * Written by Martin Buchholz with assistance from members of JCP
+ * JSR-166 Expert Group and released to the public domain, as
+ * explained at http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+/*
+ * @test
+ * @modules java.base/java.util.concurrent:open
+ * @run testng WhiteBox
+ * @summary White box tests of implementation details
+ */
+
+import static org.testng.Assert.*;
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+
+@Test
+public class WhiteBox {
+ final ThreadLocalRandom rnd = ThreadLocalRandom.current();
+ final VarHandle TABLE, NEXTTABLE, SIZECTL;
+
+ WhiteBox() throws ReflectiveOperationException {
+ Class<?> mClass = ConcurrentHashMap.class;
+ String nodeClassName = mClass.getName() + "$Node";
+ Class<?> nodeClass = Class.forName(nodeClassName);
+ Class<?> nodeArrayClass = Class.forName("[L" + nodeClassName + ";");
+ MethodHandles.Lookup lookup
+ = MethodHandles.privateLookupIn(mClass, MethodHandles.lookup());
+ TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass);
+ NEXTTABLE = lookup.findVarHandle(mClass, "nextTable", nodeArrayClass);
+ SIZECTL = lookup.findVarHandle(mClass, "sizeCtl", int.class);
+ }
+
+ Object[] table(ConcurrentHashMap m) { return (Object[]) TABLE.getVolatile(m); }
+ Object[] nextTable(ConcurrentHashMap m) { return (Object[]) NEXTTABLE.getVolatile(m); }
+ int sizeCtl(ConcurrentHashMap m) { return (int) SIZECTL.getVolatile(m); }
+
+ @Test
+ public void defaultConstructor() {
+ ConcurrentHashMap m = new ConcurrentHashMap();
+ assertNull(table(m));
+ assertEquals(sizeCtl(m), 0);
+ assertResizeNotInProgress(m);
+ }
+
+ @Test
+ public void shouldNotResizeWhenInitialCapacityProvided() {
+ int initialCapacity = rnd.nextInt(1, 100);
+ Object[] initialTable = null;
+ ConcurrentHashMap m = new ConcurrentHashMap(initialCapacity);
+
+ // table is lazily initialized
+ assertNull(table(m));
+ int expectedInitialTableLength = sizeCtl(m);
+
+ assertInvariants(m);
+ for (int i = 0; i < initialCapacity; i++) {
+ m.put(i * 100 + rnd.nextInt(100), i);
+ if (i == 0)
+ initialTable = table(m);
+ else
+ assertSame(table(m), initialTable);
+ assertInvariants(m);
+ }
+ assertEquals(initialTable.length, expectedInitialTableLength);
+ }
+
+ byte[] serialBytes(Object o) {
+ try {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(bos);
+ oos.writeObject(o);
+ oos.flush();
+ oos.close();
+ return bos.toByteArray();
+ } catch (Exception fail) {
+ throw new AssertionError(fail);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ <T> T serialClone(T o) {
+ try {
+ ObjectInputStream ois = new ObjectInputStream
+ (new ByteArrayInputStream(serialBytes(o)));
+ T clone = (T) ois.readObject();
+ assertNotSame(o, clone);
+ assertSame(o.getClass(), clone.getClass());
+ return clone;
+ } catch (Exception fail) {
+ throw new AssertionError(fail);
+ }
+ }
+
+ @Test
+ public void testSerialization() {
+ assertInvariants(serialClone(new ConcurrentHashMap()));
+
+ ConcurrentHashMap m = new ConcurrentHashMap(rnd.nextInt(100));
+ m.put(1, 1);
+ ConcurrentHashMap clone = serialClone(m);
+ assertInvariants(clone);
+ assertNotSame(table(m), table(clone));
+ assertEquals(m, clone);
+ assertResizeNotInProgress(m);
+ assertResizeNotInProgress(clone);
+ }
+
+ /** Checks conditions which should always be true. */
+ void assertInvariants(ConcurrentHashMap m) {
+ if (!m.isEmpty())
+ assertNotNull(table(m));
+ }
+
+ void assertResizeNotInProgress(ConcurrentHashMap m) {
+ assertTrue(sizeCtl(m) >= 0);
+ assertNull(nextTable(m));
+ }
+}
--- a/test/jdk/java/util/concurrent/ConcurrentLinkedQueue/WhiteBox.java Tue Apr 10 11:33:29 2018 -0700
+++ b/test/jdk/java/util/concurrent/ConcurrentLinkedQueue/WhiteBox.java Tue Apr 10 11:37:18 2018 -0700
@@ -338,6 +338,7 @@
}
}
+ @Test
public void testSerialization() {
ConcurrentLinkedQueue q = serialClone(new ConcurrentLinkedQueue());
assertInvariants(q);
--- a/test/jdk/java/util/concurrent/Executors/PrivilegedCallables.java Tue Apr 10 11:33:29 2018 -0700
+++ b/test/jdk/java/util/concurrent/Executors/PrivilegedCallables.java Tue Apr 10 11:37:18 2018 -0700
@@ -101,8 +101,7 @@
final Policy policy = new Policy();
Policy.setPolicy(policy);
policy.setPermissions(new RuntimePermission("getClassLoader"),
- new RuntimePermission("setContextClassLoader"),
- new RuntimePermission("stopThread"));
+ new RuntimePermission("setContextClassLoader"));
System.setSecurityManager(new SecurityManager());
testPrivileged();
--- a/test/jdk/java/util/concurrent/LinkedTransferQueue/WhiteBox.java Tue Apr 10 11:33:29 2018 -0700
+++ b/test/jdk/java/util/concurrent/LinkedTransferQueue/WhiteBox.java Tue Apr 10 11:37:18 2018 -0700
@@ -361,6 +361,7 @@
}
}
+ @Test
public void testSerialization() {
LinkedTransferQueue q = serialClone(new LinkedTransferQueue());
assertInvariants(q);