7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
Reviewed-by: dholmes, chegar, mduigou
diff --git a/test/java/util/WeakHashMap/GCDuringIteration.java b/test/java/util/WeakHashMap/GCDuringIteration.java
index 938322b..482d044 100644
--- a/test/java/util/WeakHashMap/GCDuringIteration.java
+++ b/test/java/util/WeakHashMap/GCDuringIteration.java
@@ -33,18 +33,17 @@
import java.util.concurrent.CountDownLatch;
public class GCDuringIteration {
- static void finalizeTillYouDrop() {
- System.gc(); // Enqueue all finalizables
+ private static void waitForFinalizersToRun() {
+ for (int i = 0; i < 2; i++)
+ tryWaitForFinalizersToRun();
+ }
- System.runFinalization(); // Drain finalizer queue
-
- // There may be a straggler finalizable object still being
- // finalized by the dedicated finalizer thread. Enqueue one
- // more finalizable object, and wait for it to be finalized.
- final CountDownLatch latch = new CountDownLatch(1);
- new Object() { protected void finalize() { latch.countDown(); }};
+ private static void tryWaitForFinalizersToRun() {
System.gc();
- try { latch.await(); }
+ final CountDownLatch fin = new CountDownLatch(1);
+ new Object() { protected void finalize() { fin.countDown(); }};
+ System.gc();
+ try { fin.await(); }
catch (InterruptedException ie) { throw new Error(ie); }
}
@@ -101,7 +100,9 @@
{
int first = firstValue(map);
final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
- foos[first] = null; finalizeTillYouDrop();
+ foos[first] = null;
+ for (int i = 0; i < 10 && map.size() != first; i++)
+ tryWaitForFinalizersToRun();
equal(map.size(), first);
checkIterator(it, first-1);
equal(map.size(), first);
@@ -113,11 +114,14 @@
final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
it.next(); // protects first entry
System.out.println(map.values());
- foos[first] = null; finalizeTillYouDrop();
+ foos[first] = null;
+ tryWaitForFinalizersToRun()
equal(map.size(), first+1);
System.out.println(map.values());
checkIterator(it, first-1);
- finalizeTillYouDrop(); // first entry no longer protected
+ // first entry no longer protected
+ for (int i = 0; i < 10 && map.size() != first; i++)
+ tryWaitForFinalizersToRun();
equal(map.size(), first);
equal(firstValue(map), first-1);
}
@@ -127,12 +131,15 @@
final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
it.next(); // protects first entry
System.out.println(map.values());
- foos[first] = foos[first-1] = null; finalizeTillYouDrop();
+ foos[first] = foos[first-1] = null;
+ tryWaitForFinalizersToRun();
equal(map.size(), first);
equal(firstValue(map), first);
System.out.println(map.values());
checkIterator(it, first-2);
- finalizeTillYouDrop(); // first entry no longer protected
+ // first entry no longer protected
+ for (int i = 0; i < 10 && map.size() != first-1; i++)
+ tryWaitForFinalizersToRun();
equal(map.size(), first-1);
equal(firstValue(map), first-2);
}
@@ -143,12 +150,15 @@
it.next(); // protects first entry
it.hasNext(); // protects second entry
System.out.println(map.values());
- foos[first] = foos[first-1] = null; finalizeTillYouDrop();
+ foos[first] = foos[first-1] = null;
+ tryWaitForFinalizersToRun();
equal(firstValue(map), first);
equal(map.size(), first+1);
System.out.println(map.values());
checkIterator(it, first-1);
- finalizeTillYouDrop(); // first entry no longer protected
+ // first entry no longer protected
+ for (int i = 0; i < 10 && map.size() != first-1; i++)
+ tryWaitForFinalizersToRun();
equal(map.size(), first-1);
equal(firstValue(map), first-2);
}
@@ -158,13 +168,16 @@
final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
it.next(); // protects first entry
System.out.println(map.values());
- foos[first] = foos[first-1] = null; finalizeTillYouDrop();
+ foos[first] = foos[first-1] = null;
+ tryWaitForFinalizersToRun();
it.remove();
equal(firstValue(map), first-2);
equal(map.size(), first-1);
System.out.println(map.values());
checkIterator(it, first-2);
- finalizeTillYouDrop();
+ // first entry no longer protected
+ for (int i = 0; i < 10 && map.size() != first-1; i++)
+ tryWaitForFinalizersToRun();
equal(map.size(), first-1);
equal(firstValue(map), first-2);
}
@@ -176,12 +189,14 @@
it.remove();
it.hasNext(); // protects second entry
System.out.println(map.values());
- foos[first] = foos[first-1] = null; finalizeTillYouDrop();
+ foos[first] = foos[first-1] = null;
+ tryWaitForFinalizersToRun();
equal(firstValue(map), first-1);
equal(map.size(), first);
System.out.println(map.values());
checkIterator(it, first-1);
- finalizeTillYouDrop();
+ for (int i = 0; i < 10 && map.size() != first-1; i++)
+ tryWaitForFinalizersToRun();
equal(map.size(), first-1);
equal(firstValue(map), first-2);
}
@@ -191,12 +206,13 @@
final Iterator<Map.Entry<Foo,Integer>> it = map.entrySet().iterator();
it.hasNext(); // protects first entry
Arrays.fill(foos, null);
- finalizeTillYouDrop();
+ tryWaitForFinalizersToRun();
equal(map.size(), 1);
System.out.println(map.values());
equal(it.next().getValue(), first);
check(! it.hasNext());
- finalizeTillYouDrop();
+ for (int i = 0; i < 10 && map.size() != 0; i++)
+ tryWaitForFinalizersToRun();
equal(map.size(), 0);
check(map.isEmpty());
}
diff --git a/test/java/util/concurrent/BlockingQueue/CancelledProducerConsumerLoops.java b/test/java/util/concurrent/BlockingQueue/CancelledProducerConsumerLoops.java
index c12a57e..34f0722 100644
--- a/test/java/util/concurrent/BlockingQueue/CancelledProducerConsumerLoops.java
+++ b/test/java/util/concurrent/BlockingQueue/CancelledProducerConsumerLoops.java
@@ -34,7 +34,7 @@
/*
* @test
* @bug 4486658
- * @compile CancelledProducerConsumerLoops.java
+ * @compile -source 1.5 CancelledProducerConsumerLoops.java
* @run main/timeout=7000 CancelledProducerConsumerLoops
* @summary Checks for responsiveness of blocking queues to cancellation.
* Runs under the assumption that ITERS computations require more than
@@ -119,48 +119,24 @@
}
}
- static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
- LTQasSQ() { super(); }
- public void put(T x) {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- private final static long serialVersionUID = 42;
- }
-
- static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
- HalfSyncLTQ() { super(); }
- public void put(T x) {
- if (ThreadLocalRandom.current().nextBoolean())
- super.put(x);
- else {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- }
- private final static long serialVersionUID = 42;
- }
-
static void oneTest(int pairs, int iters) throws Exception {
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
+ oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
oneRun(new SynchronousQueue<Integer>(), pairs, iters / 8);
- /* TODO: unbounded queue implementations are prone to OOME
+ /* PriorityBlockingQueue is unbounded
oneRun(new PriorityBlockingQueue<Integer>(iters / 2 * pairs), pairs, iters / 4);
- oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
- oneRun(new LTQasSQ<Integer>(), pairs, iters);
- oneRun(new HalfSyncLTQ<Integer>(), pairs, iters);
*/
}
- static abstract class Stage implements Callable<Integer> {
+ abstract static class Stage implements Callable<Integer> {
final BlockingQueue<Integer> queue;
final CyclicBarrier barrier;
final int iters;
- Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
+ Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
queue = q;
barrier = b;
this.iters = iters;
diff --git a/test/java/util/concurrent/BlockingQueue/MultipleProducersSingleConsumerLoops.java b/test/java/util/concurrent/BlockingQueue/MultipleProducersSingleConsumerLoops.java
index 1f85c51..d57939f 100644
--- a/test/java/util/concurrent/BlockingQueue/MultipleProducersSingleConsumerLoops.java
+++ b/test/java/util/concurrent/BlockingQueue/MultipleProducersSingleConsumerLoops.java
@@ -34,7 +34,7 @@
/*
* @test
* @bug 4486658
- * @compile MultipleProducersSingleConsumerLoops.java
+ * @compile -source 1.5 MultipleProducersSingleConsumerLoops.java
* @run main/timeout=3600 MultipleProducersSingleConsumerLoops
* @summary multiple producers and single consumer using blocking queues
*/
@@ -87,35 +87,11 @@
throw new Error();
}
- static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
- LTQasSQ() { super(); }
- public void put(T x) {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- private final static long serialVersionUID = 42;
- }
-
- static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
- HalfSyncLTQ() { super(); }
- public void put(T x) {
- if (ThreadLocalRandom.current().nextBoolean())
- super.put(x);
- else {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- }
- private final static long serialVersionUID = 42;
- }
-
static void oneTest(int producers, int iters) throws Exception {
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), producers, iters);
oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), producers, iters);
oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), producers, iters);
oneRun(new LinkedTransferQueue<Integer>(), producers, iters);
- oneRun(new LTQasSQ<Integer>(), producers, iters);
- oneRun(new HalfSyncLTQ<Integer>(), producers, iters);
// Don't run PBQ since can legitimately run out of memory
// if (print)
@@ -129,11 +105,11 @@
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), producers, iters);
}
- static abstract class Stage implements Runnable {
+ abstract static class Stage implements Runnable {
final int iters;
final BlockingQueue<Integer> queue;
final CyclicBarrier barrier;
- Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
+ Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
queue = q;
barrier = b;
this.iters = iters;
diff --git a/test/java/util/concurrent/BlockingQueue/ProducerConsumerLoops.java b/test/java/util/concurrent/BlockingQueue/ProducerConsumerLoops.java
index f2b4a5b..9f18ab5 100644
--- a/test/java/util/concurrent/BlockingQueue/ProducerConsumerLoops.java
+++ b/test/java/util/concurrent/BlockingQueue/ProducerConsumerLoops.java
@@ -34,7 +34,7 @@
/*
* @test
* @bug 4486658
- * @compile ProducerConsumerLoops.java
+ * @compile -source 1.5 ProducerConsumerLoops.java
* @run main/timeout=3600 ProducerConsumerLoops
* @summary multiple producers and consumers using blocking queues
*/
@@ -87,35 +87,11 @@
throw new Error();
}
- static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
- LTQasSQ() { super(); }
- public void put(T x) {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- private final static long serialVersionUID = 42;
- }
-
- static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
- HalfSyncLTQ() { super(); }
- public void put(T x) {
- if (ThreadLocalRandom.current().nextBoolean())
- super.put(x);
- else {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- }
- private final static long serialVersionUID = 42;
- }
-
static void oneTest(int pairs, int iters) throws Exception {
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), pairs, iters);
oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), pairs, iters);
oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), pairs, iters);
oneRun(new LinkedTransferQueue<Integer>(), pairs, iters);
- oneRun(new LTQasSQ<Integer>(), pairs, iters);
- oneRun(new HalfSyncLTQ<Integer>(), pairs, iters);
oneRun(new PriorityBlockingQueue<Integer>(), pairs, iters);
oneRun(new SynchronousQueue<Integer>(), pairs, iters);
@@ -126,11 +102,11 @@
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), pairs, iters);
}
- static abstract class Stage implements Runnable {
+ abstract static class Stage implements Runnable {
final int iters;
final BlockingQueue<Integer> queue;
final CyclicBarrier barrier;
- Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
+ Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
queue = q;
barrier = b;
this.iters = iters;
diff --git a/test/java/util/concurrent/BlockingQueue/SingleProducerMultipleConsumerLoops.java b/test/java/util/concurrent/BlockingQueue/SingleProducerMultipleConsumerLoops.java
index 1550bb2..85699bc 100644
--- a/test/java/util/concurrent/BlockingQueue/SingleProducerMultipleConsumerLoops.java
+++ b/test/java/util/concurrent/BlockingQueue/SingleProducerMultipleConsumerLoops.java
@@ -34,7 +34,7 @@
/*
* @test
* @bug 4486658
- * @compile SingleProducerMultipleConsumerLoops.java
+ * @compile -source 1.5 SingleProducerMultipleConsumerLoops.java
* @run main/timeout=600 SingleProducerMultipleConsumerLoops
* @summary check ordering for blocking queues with 1 producer and multiple consumers
*/
@@ -73,35 +73,11 @@
throw new Error();
}
- static final class LTQasSQ<T> extends LinkedTransferQueue<T> {
- LTQasSQ() { super(); }
- public void put(T x) {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- private final static long serialVersionUID = 42;
- }
-
- static final class HalfSyncLTQ<T> extends LinkedTransferQueue<T> {
- HalfSyncLTQ() { super(); }
- public void put(T x) {
- if (ThreadLocalRandom.current().nextBoolean())
- super.put(x);
- else {
- try { super.transfer(x); }
- catch (InterruptedException ex) { throw new Error(); }
- }
- }
- private final static long serialVersionUID = 42;
- }
-
static void oneTest(int consumers, int iters) throws Exception {
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY), consumers, iters);
oneRun(new LinkedBlockingQueue<Integer>(CAPACITY), consumers, iters);
oneRun(new LinkedBlockingDeque<Integer>(CAPACITY), consumers, iters);
oneRun(new LinkedTransferQueue<Integer>(), consumers, iters);
- oneRun(new LTQasSQ<Integer>(), consumers, iters);
- oneRun(new HalfSyncLTQ<Integer>(), consumers, iters);
oneRun(new PriorityBlockingQueue<Integer>(), consumers, iters);
oneRun(new SynchronousQueue<Integer>(), consumers, iters);
if (print)
@@ -110,12 +86,12 @@
oneRun(new ArrayBlockingQueue<Integer>(CAPACITY, true), consumers, iters);
}
- static abstract class Stage implements Runnable {
+ abstract static class Stage implements Runnable {
final int iters;
final BlockingQueue<Integer> queue;
final CyclicBarrier barrier;
volatile int result;
- Stage (BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
+ Stage(BlockingQueue<Integer> q, CyclicBarrier b, int iters) {
queue = q;
barrier = b;
this.iters = iters;
diff --git a/test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java b/test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java
index 78ce8d7..dabb2b9 100644
--- a/test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java
+++ b/test/java/util/concurrent/ConcurrentQueues/IteratorWeakConsistency.java
@@ -53,7 +53,9 @@
test(new LinkedTransferQueue());
// Other concurrent queues (e.g. ArrayBlockingQueue) do not
// currently have weakly consistent iterators.
- // test(new ArrayBlockingQueue(20));
+ // As of 2010-09, ArrayBlockingQueue passes this test, but
+ // does not fully implement weak consistency.
+ test(new ArrayBlockingQueue(20));
}
void test(Queue q) {
diff --git a/test/java/util/concurrent/Executors/AutoShutdown.java b/test/java/util/concurrent/Executors/AutoShutdown.java
index e6f7c1a..fb4976f 100644
--- a/test/java/util/concurrent/Executors/AutoShutdown.java
+++ b/test/java/util/concurrent/Executors/AutoShutdown.java
@@ -24,6 +24,7 @@
/*
* @test
* @bug 6399443
+ * @run main/othervm AutoShutdown
* @summary Check for auto-shutdown and gc of singleThreadExecutors
* @author Martin Buchholz
*/
diff --git a/test/java/util/concurrent/Phaser/Basic.java b/test/java/util/concurrent/Phaser/Basic.java
index e394cee..a358873 100644
--- a/test/java/util/concurrent/Phaser/Basic.java
+++ b/test/java/util/concurrent/Phaser/Basic.java
@@ -52,15 +52,16 @@
check(phaser.isTerminated());
int unarriverParties = phaser.getUnarrivedParties();
int registeredParties = phaser.getRegisteredParties();
- equal(phaser.arrive(), -1);
- equal(phaser.arriveAndDeregister(), -1);
- equal(phaser.arriveAndAwaitAdvance(), -1);
- equal(phaser.bulkRegister(10), -1);
- equal(phaser.getPhase(), -1);
- equal(phaser.register(), -1);
+ int phase = phaser.getPhase();
+ check(phase < 0);
+ equal(phase, phaser.arrive());
+ equal(phase, phaser.arriveAndDeregister());
+ equal(phase, phaser.arriveAndAwaitAdvance());
+ equal(phase, phaser.bulkRegister(10));
+ equal(phase, phaser.register());
try {
- equal(phaser.awaitAdvanceInterruptibly(0), -1);
- equal(phaser.awaitAdvanceInterruptibly(0, 10, SECONDS), -1);
+ equal(phase, phaser.awaitAdvanceInterruptibly(0));
+ equal(phase, phaser.awaitAdvanceInterruptibly(0, 10, SECONDS));
} catch (Exception ie) {
unexpected(ie);
}
@@ -94,10 +95,9 @@
}
int phase = atTheStartingGate.getPhase();
equal(phase, atTheStartingGate.arrive());
- int AwaitPhase = atTheStartingGate.awaitAdvanceInterruptibly(phase,
- 10,
- SECONDS);
- if (expectNextPhase) check(AwaitPhase == (phase + 1));
+ int awaitPhase = atTheStartingGate.awaitAdvanceInterruptibly
+ (phase, 10, SECONDS);
+ if (expectNextPhase) check(awaitPhase == (phase + 1));
pass();
} catch (Throwable t) {
@@ -271,18 +271,19 @@
// Phaser is terminated while threads are waiting
//----------------------------------------------------------------
try {
- Phaser phaser = new Phaser(3);
- Iterator<Awaiter> awaiters = awaiterIterator(phaser);
for (int i = 0; i < 4; i++) {
+ Phaser phaser = new Phaser(3);
+ Iterator<Awaiter> awaiters = awaiterIterator(phaser);
Arriver a1 = awaiters.next(); a1.start();
Arriver a2 = awaiters.next(); a2.start();
toTheStartingGate();
while (phaser.getArrivedParties() < 2) Thread.yield();
+ equal(0, phaser.getPhase());
phaser.forceTermination();
a1.join();
a2.join();
- check(a1.phase == -1);
- check(a2.phase == -1);
+ equal(0 + Integer.MIN_VALUE, a1.phase);
+ equal(0 + Integer.MIN_VALUE, a2.phase);
int arrivedParties = phaser.getArrivedParties();
checkTerminated(phaser);
equal(phaser.getArrivedParties(), arrivedParties);
diff --git a/test/java/util/concurrent/Phaser/FickleRegister.java b/test/java/util/concurrent/Phaser/FickleRegister.java
new file mode 100644
index 0000000..9ce91c5
--- /dev/null
+++ b/test/java/util/concurrent/Phaser/FickleRegister.java
@@ -0,0 +1,150 @@
+/*
+ * 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 Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ */
+
+/*
+ * @test
+ * @summary stress test for register/arriveAndDeregister
+ * @run main FickleRegister 300
+ */
+
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
+
+public class FickleRegister {
+ final AtomicLong count = new AtomicLong(0);
+ final long testDurationMillisDefault = 10L * 1000L;
+ final long testDurationMillis;
+ final long quittingTimeNanos;
+ final int chunkSize = 1000;
+
+ FickleRegister(String[] args) {
+ testDurationMillis = (args.length > 0) ?
+ Long.valueOf(args[0]) : testDurationMillisDefault;
+ quittingTimeNanos = System.nanoTime() +
+ testDurationMillis * 1000L * 1000L;
+ }
+
+ class Runner extends CheckedRunnable {
+ final Phaser p;
+ Runner(Phaser phaser) { p = phaser; }
+ public void realRun() {
+ int prevPhase = -1;
+ for (int k = 1;; k++) {
+ for (int i = 0; i < chunkSize; i++) {
+ int phase = p.register();
+ if (phase < 0) break;
+ check(phase > prevPhase);
+ prevPhase = phase;
+ equal(phase, p.arriveAndDeregister());
+ check(phase < p.awaitAdvance(phase));
+ }
+ if (System.nanoTime() - quittingTimeNanos > 0) {
+ count.getAndAdd(k * chunkSize);
+ break;
+ }
+ }
+ }
+ }
+
+ void test(String[] args) throws Throwable {
+ final Phaser parent = new Phaser() {
+ protected boolean onAdvance(int phase, int parties) {
+ return false;
+ }
+ };
+
+ final Phaser child1 = new Phaser(parent);
+ final Phaser child2 = new Phaser(parent);
+ final Phaser subchild1 = new Phaser(child1);
+ final Phaser subchild2 = new Phaser(child2);
+ final Phaser[] phasers = {
+ parent, child1, child2, subchild1, subchild2
+ };
+
+ int reps = 4;
+ ArrayList<Thread> threads = new ArrayList<Thread>();
+ for (int j = 0; j < reps; ++j) {
+ threads.add(new Thread(new Runner(subchild1)));
+ threads.add(new Thread(new Runner(child1)));
+ threads.add(new Thread(new Runner(parent)));
+ threads.add(new Thread(new Runner(child2)));
+ threads.add(new Thread(new Runner(subchild2)));
+ }
+
+ for (Thread thread : threads)
+ thread.start();
+
+ for (Thread thread : threads)
+ thread.join();
+
+ System.out.println("Parent: " + parent);
+ System.out.println("Child1: " + child1);
+ System.out.println("Child2: " + child2);
+ System.out.println("Subchild1: " + subchild1);
+ System.out.println("Subchild2: " + subchild2);
+ System.out.println("Iterations:" + count.get());
+
+ for (Phaser phaser : phasers) {
+ check(phaser.getPhase() > 0);
+ equal(0, phaser.getRegisteredParties());
+ equal(0, phaser.getUnarrivedParties());
+ equal(parent.getPhase(), phaser.getPhase());
+ }
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ volatile int passed = 0, failed = 0;
+ void pass() {passed++;}
+ void fail() {failed++; Thread.dumpStack();}
+ void fail(String msg) {System.err.println(msg); fail();}
+ void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ void check(boolean cond) {if (cond) pass(); else fail();}
+ void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ new FickleRegister(args).instanceMain(args);}
+ public void instanceMain(String[] args) throws Throwable {
+ try {test(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
+
+ abstract class CheckedRunnable implements Runnable {
+ protected abstract void realRun() throws Throwable;
+
+ public final void run() {
+ try {realRun();} catch (Throwable t) {unexpected(t);}
+ }
+ }
+}
diff --git a/test/java/util/concurrent/Phaser/PhaseOverflow.java b/test/java/util/concurrent/Phaser/PhaseOverflow.java
new file mode 100644
index 0000000..da8b536
--- /dev/null
+++ b/test/java/util/concurrent/Phaser/PhaseOverflow.java
@@ -0,0 +1,158 @@
+/*
+ * 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 and Doug Lea with assistance from
+ * members of JCP JSR-166 Expert Group and released to the public
+ * domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ */
+
+/*
+ * @test
+ * @summary Test Phaser phase integer overflow behavior
+ */
+
+import java.util.concurrent.Phaser;
+import java.lang.reflect.Field;
+
+public class PhaseOverflow {
+ Field stateField;
+
+ void checkState(Phaser phaser,
+ int phase, int parties, int unarrived) {
+ equal(phase, phaser.getPhase());
+ equal(parties, phaser.getRegisteredParties());
+ equal(unarrived, phaser.getUnarrivedParties());
+ }
+
+ void test(String[] args) throws Throwable {
+ stateField = Phaser.class.getDeclaredField("state");
+ stateField.setAccessible(true);
+ testLeaf();
+ testTiered();
+ }
+
+ void testLeaf() throws Throwable {
+ Phaser phaser = new Phaser();
+ // this is extremely dependent on internal representation
+ stateField.setLong(phaser, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
+ checkState(phaser, Integer.MAX_VALUE - 1, 0, 0);
+ phaser.register();
+ checkState(phaser, Integer.MAX_VALUE - 1, 1, 1);
+ phaser.arrive();
+ checkState(phaser, Integer.MAX_VALUE, 1, 1);
+ phaser.arrive();
+ checkState(phaser, 0, 1, 1);
+ phaser.arrive();
+ checkState(phaser, 1, 1, 1);
+ }
+
+ int phaseInc(int phase) { return (phase + 1) & Integer.MAX_VALUE; }
+
+ void testTiered() throws Throwable {
+ Phaser root = new Phaser();
+ // this is extremely dependent on internal representation
+ stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
+ checkState(root, Integer.MAX_VALUE - 1, 0, 0);
+ Phaser p1 = new Phaser(root, 1);
+ checkState(root, Integer.MAX_VALUE - 1, 1, 1);
+ checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
+ Phaser p2 = new Phaser(root, 2);
+ checkState(root, Integer.MAX_VALUE - 1, 2, 2);
+ checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
+ int ph = Integer.MAX_VALUE - 1;
+ for (int k = 0; k < 5; k++) {
+ checkState(root, ph, 2, 2);
+ checkState(p1, ph, 1, 1);
+ checkState(p2, ph, 2, 2);
+ p1.arrive();
+ checkState(root, ph, 2, 1);
+ checkState(p1, ph, 1, 0);
+ checkState(p2, ph, 2, 2);
+ p2.arrive();
+ checkState(root, ph, 2, 1);
+ checkState(p1, ph, 1, 0);
+ checkState(p2, ph, 2, 1);
+ p2.arrive();
+ ph = phaseInc(ph);
+ checkState(root, ph, 2, 2);
+ checkState(p1, ph, 1, 1);
+ checkState(p2, ph, 2, 2);
+ }
+ equal(3, ph);
+ }
+
+ void xtestTiered() throws Throwable {
+ Phaser root = new Phaser();
+ stateField.setLong(root, ((Integer.MAX_VALUE - 1L) << 32) | 1L);
+ checkState(root, Integer.MAX_VALUE - 1, 0, 0);
+ Phaser p1 = new Phaser(root, 1);
+ checkState(root, Integer.MAX_VALUE - 1, 1, 1);
+ checkState(p1, Integer.MAX_VALUE - 1, 1, 1);
+ Phaser p2 = new Phaser(root, 2);
+ checkState(root, Integer.MAX_VALUE - 1, 2, 2);
+ checkState(p2, Integer.MAX_VALUE - 1, 2, 2);
+ int ph = Integer.MAX_VALUE - 1;
+ for (int k = 0; k < 5; k++) {
+ checkState(root, ph, 2, 2);
+ checkState(p1, ph, 1, 1);
+ checkState(p2, ph, 2, 2);
+ p1.arrive();
+ checkState(root, ph, 2, 1);
+ checkState(p1, ph, 1, 0);
+ checkState(p2, ph, 2, 2);
+ p2.arrive();
+ checkState(root, ph, 2, 1);
+ checkState(p1, ph, 1, 0);
+ checkState(p2, ph, 2, 1);
+ p2.arrive();
+ ph = phaseInc(ph);
+ checkState(root, ph, 2, 2);
+ checkState(p1, ph, 1, 1);
+ checkState(p2, ph, 2, 2);
+ }
+ equal(3, ph);
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ volatile int passed = 0, failed = 0;
+ void pass() {passed++;}
+ void fail() {failed++; Thread.dumpStack();}
+ void fail(String msg) {System.err.println(msg); fail();}
+ void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ void check(boolean cond) {if (cond) pass(); else fail();}
+ void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ new PhaseOverflow().instanceMain(args);}
+ public void instanceMain(String[] args) throws Throwable {
+ try {test(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
+}
diff --git a/test/java/util/concurrent/Phaser/TieredArriveLoops.java b/test/java/util/concurrent/Phaser/TieredArriveLoops.java
new file mode 100644
index 0000000..33a03f0
--- /dev/null
+++ b/test/java/util/concurrent/Phaser/TieredArriveLoops.java
@@ -0,0 +1,117 @@
+/*
+ * 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 Doug Lea with assistance from members of JCP JSR-166
+ * Expert Group and released to the public domain, as explained at
+ * http://creativecommons.org/licenses/publicdomain
+ */
+
+/*
+ * @test
+ * @summary stress test for arrivals in a tiered phaser
+ * @run main TieredArriveLoops 300
+ */
+import java.util.*;
+import java.util.concurrent.*;
+
+public class TieredArriveLoops {
+ final long testDurationMillisDefault = 10L * 1000L;
+ final long testDurationMillis;
+ final long quittingTimeNanos;
+
+ TieredArriveLoops(String[] args) {
+ testDurationMillis = (args.length > 0) ?
+ Long.valueOf(args[0]) : testDurationMillisDefault;
+ quittingTimeNanos = System.nanoTime() +
+ testDurationMillis * 1000L * 1000L;
+ }
+
+ Runnable runner(final Phaser p) {
+ return new CheckedRunnable() { public void realRun() {
+ int prevPhase = p.register();
+ while (!p.isTerminated()) {
+ int phase = p.awaitAdvance(p.arrive());
+ if (phase < 0)
+ return;
+ equal(phase, (prevPhase + 1) & Integer.MAX_VALUE);
+ int ph = p.getPhase();
+ check(ph < 0 || ph == phase);
+ prevPhase = phase;
+ }
+ }};
+ }
+
+ void test(String[] args) throws Throwable {
+ final Phaser parent = new Phaser();
+ final Phaser child1 = new Phaser(parent);
+ final Phaser child2 = new Phaser(parent);
+
+ Thread t1 = new Thread(runner(child1));
+ Thread t2 = new Thread(runner(child2));
+ t1.start();
+ t2.start();
+
+ for (int prevPhase = 0, phase; ; prevPhase = phase) {
+ phase = child2.getPhase();
+ check(phase >= prevPhase);
+ if (System.nanoTime() - quittingTimeNanos > 0) {
+ System.err.printf("phase=%d%n", phase);
+ child1.forceTermination();
+ break;
+ }
+ }
+
+ t1.join();
+ t2.join();
+ }
+
+ //--------------------- Infrastructure ---------------------------
+ volatile int passed = 0, failed = 0;
+ void pass() {passed++;}
+ void fail() {failed++; Thread.dumpStack();}
+ void fail(String msg) {System.err.println(msg); fail();}
+ void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ void check(boolean cond) {if (cond) pass(); else fail();}
+ void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ new TieredArriveLoops(args).instanceMain(args);}
+ public void instanceMain(String[] args) throws Throwable {
+ try {test(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
+
+ abstract class CheckedRunnable implements Runnable {
+ protected abstract void realRun() throws Throwable;
+
+ public final void run() {
+ try {realRun();} catch (Throwable t) {unexpected(t);}
+ }
+ }
+}
diff --git a/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java b/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
index 6bd37fe..547cd21 100644
--- a/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
+++ b/test/java/util/concurrent/ThreadPoolExecutor/CoreThreadTimeOut.java
@@ -31,47 +31,79 @@
import java.util.concurrent.*;
public class CoreThreadTimeOut {
- static volatile int passed = 0, failed = 0;
- static void pass() { passed++; }
- static void fail() { failed++; Thread.dumpStack(); }
- static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
- static void check(boolean cond) { if (cond) pass(); else fail(); }
- static void equal(Object x, Object y) {
- if (x == null ? y == null : x.equals(y)) pass();
- else {System.out.println(x + " not equal to " + y); fail(); }}
- static int countExecutorThreads() {
+ static class IdentifiableThreadFactory implements ThreadFactory {
+ static ThreadFactory defaultThreadFactory
+ = Executors.defaultThreadFactory();
+
+ public Thread newThread(Runnable r) {
+ Thread t = defaultThreadFactory.newThread(r);
+ t.setName("CoreThreadTimeOut-" + t.getName());
+ return t;
+ }
+ }
+
+ int countExecutorThreads() {
Thread[] threads = new Thread[Thread.activeCount()+100];
Thread.enumerate(threads);
int count = 0;
for (Thread t : threads)
- if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+"))
+ if (t != null &&
+ t.getName().matches
+ ("CoreThreadTimeOut-pool-[0-9]+-thread-[0-9]+"))
count++;
return count;
}
- public static void main(String[] args) throws Throwable {
+ long millisElapsedSince(long t0) {
+ return (System.nanoTime() - t0) / (1000L * 1000L);
+ }
+
+ void test(String[] args) throws Throwable {
final int threadCount = 10;
+ final int timeoutMillis = 30;
BlockingQueue<Runnable> q
= new ArrayBlockingQueue<Runnable>(2*threadCount);
ThreadPoolExecutor tpe
= new ThreadPoolExecutor(threadCount, threadCount,
- 30, TimeUnit.MILLISECONDS,
- q);
+ timeoutMillis, TimeUnit.MILLISECONDS,
+ q, new IdentifiableThreadFactory());
equal(tpe.getCorePoolSize(), threadCount);
check(! tpe.allowsCoreThreadTimeOut());
tpe.allowCoreThreadTimeOut(true);
check(tpe.allowsCoreThreadTimeOut());
equal(countExecutorThreads(), 0);
+ long t0 = System.nanoTime();
for (int i = 0; i < threadCount; i++)
tpe.submit(new Runnable() { public void run() {}});
- equal(countExecutorThreads(), threadCount);
- Thread.sleep(500);
+ int count = countExecutorThreads();
+ if (millisElapsedSince(t0) < timeoutMillis)
+ equal(count, threadCount);
+ while (countExecutorThreads() > 0 &&
+ millisElapsedSince(t0) < 10 * 1000);
equal(countExecutorThreads(), 0);
tpe.shutdown();
check(tpe.allowsCoreThreadTimeOut());
+ check(tpe.awaitTermination(10, TimeUnit.SECONDS));
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new Exception("Some tests failed");
}
+
+ //--------------------- Infrastructure ---------------------------
+ volatile int passed = 0, failed = 0;
+ void pass() {passed++;}
+ void fail() {failed++; Thread.dumpStack();}
+ void fail(String msg) {System.err.println(msg); fail();}
+ void unexpected(Throwable t) {failed++; t.printStackTrace();}
+ void check(boolean cond) {if (cond) pass(); else fail();}
+ void equal(Object x, Object y) {
+ if (x == null ? y == null : x.equals(y)) pass();
+ else fail(x + " not equal to " + y);}
+ public static void main(String[] args) throws Throwable {
+ new CoreThreadTimeOut().instanceMain(args);}
+ public void instanceMain(String[] args) throws Throwable {
+ try {test(args);} catch (Throwable t) {unexpected(t);}
+ System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
+ if (failed > 0) throw new AssertionError("Some tests failed");}
}