提交 e532d028 编写于 作者: D dl

8173909: Miscellaneous changes imported from jsr166 CVS 2017-03

Reviewed-by: martin, psandoz
上级 22d2412e
......@@ -779,8 +779,7 @@ public final class SplittableRandom {
* @return a stream of pseudorandom {@code double} values,
* each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code streamSize} is
* less than zero
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* less than zero, or {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
*/
public DoubleStream doubles(long streamSize, double randomNumberOrigin,
......
......@@ -1226,6 +1226,7 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E>
} else {
nextIndex = NONE;
nextItem = null;
if (lastRet == REMOVED) detach();
}
} finally {
lock.unlock();
......
......@@ -699,8 +699,7 @@ public class ThreadLocalRandom extends Random {
* @return a stream of pseudorandom {@code double} values,
* each with the given origin (inclusive) and bound (exclusive)
* @throws IllegalArgumentException if {@code streamSize} is
* less than zero
* @throws IllegalArgumentException if {@code randomNumberOrigin}
* less than zero, or {@code randomNumberOrigin}
* is greater than or equal to {@code randomNumberBound}
* @since 1.8
*/
......
......@@ -53,16 +53,14 @@ import jdk.internal.vm.annotation.ReservedStackAccess;
*
* <dl>
* <dt><b><i>Non-fair mode (default)</i></b>
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif">
* When constructed as non-fair (the default), the order of entry
* <dd>When constructed as non-fair (the default), the order of entry
* to the read and write lock is unspecified, subject to reentrancy
* constraints. A nonfair lock that is continuously contended may
* indefinitely postpone one or more reader or writer threads, but
* will normally have higher throughput than a fair lock.
*
* <dt><b><i>Fair mode</i></b>
* <dd style="font-family:'DejaVu Sans', Arial, Helvetica, sans-serif">
* When constructed as fair, threads contend for entry using an
* <dd>When constructed as fair, threads contend for entry using an
* approximately arrival-order policy. When the currently held lock
* is released, either the longest-waiting single writer thread will
* be assigned the write lock, or if there is a group of reader threads
......
......@@ -1326,20 +1326,60 @@ public class JSR166TestCase extends TestCase {
startTime = System.nanoTime();
else if (millisElapsedSince(startTime) > timeoutMillis) {
threadAssertTrue(thread.isAlive());
return;
fail("timed out waiting for thread to enter wait state");
}
Thread.yield();
}
}
/**
* Spin-waits up to the specified number of milliseconds for the given
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING,
* and additionally satisfy the given condition.
*/
void waitForThreadToEnterWaitState(
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) {
long startTime = 0L;
for (;;) {
Thread.State s = thread.getState();
if (s == Thread.State.BLOCKED ||
s == Thread.State.WAITING ||
s == Thread.State.TIMED_WAITING) {
try {
if (waitingForGodot.call())
return;
} catch (Throwable fail) { threadUnexpectedException(fail); }
}
else if (s == Thread.State.TERMINATED)
fail("Unexpected thread termination");
else if (startTime == 0L)
startTime = System.nanoTime();
else if (millisElapsedSince(startTime) > timeoutMillis) {
threadAssertTrue(thread.isAlive());
fail("timed out waiting for thread to enter wait state");
}
Thread.yield();
}
}
/**
* Waits up to LONG_DELAY_MS for the given thread to enter a wait
* state: BLOCKED, WAITING, or TIMED_WAITING.
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
*/
void waitForThreadToEnterWaitState(Thread thread) {
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
}
/**
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING,
* and additionally satisfy the given condition.
*/
void waitForThreadToEnterWaitState(
Thread thread, Callable<Boolean> waitingForGodot) {
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot);
}
/**
* Returns the number of milliseconds since time given by
* startNanoTime, which must have been previously returned from a
......
......@@ -42,6 +42,7 @@ import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
......@@ -766,9 +767,11 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
}});
threadStarted.await();
waitForThreadToEnterWaitState(t);
assertEquals(1, q.getWaitingConsumerCount());
assertTrue(q.hasWaitingConsumer());
Callable<Boolean> oneConsumer
= new Callable<Boolean>() { public Boolean call() {
return q.hasWaitingConsumer()
&& q.getWaitingConsumerCount() == 1; }};
waitForThreadToEnterWaitState(t, oneConsumer);
assertTrue(q.offer(one));
assertEquals(0, q.getWaitingConsumerCount());
......@@ -790,7 +793,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
/**
* transfer waits until a poll occurs. The transfered element
* is returned by this associated poll.
* is returned by the associated poll.
*/
public void testTransfer2() throws InterruptedException {
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
......@@ -804,8 +807,11 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
}});
threadStarted.await();
waitForThreadToEnterWaitState(t);
assertEquals(1, q.size());
Callable<Boolean> oneElement
= new Callable<Boolean>() { public Boolean call() {
return !q.isEmpty() && q.size() == 1; }};
waitForThreadToEnterWaitState(t, oneElement);
assertSame(five, q.poll());
checkEmpty(q);
awaitTermination(t);
......@@ -868,7 +874,7 @@ public class LinkedTransferQueueTest extends JSR166TestCase {
/**
* transfer waits until a take occurs. The transfered element
* is returned by this associated take.
* is returned by the associated take.
*/
public void testTransfer5() throws InterruptedException {
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>();
......
......@@ -550,7 +550,7 @@ public class PhaserTest extends JSR166TestCase {
}});
await(pleaseArrive);
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
waitForThreadToEnterWaitState(t);
assertEquals(0, phaser.arrive());
awaitTermination(t);
......@@ -578,7 +578,7 @@ public class PhaserTest extends JSR166TestCase {
}});
await(pleaseArrive);
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
waitForThreadToEnterWaitState(t);
t.interrupt();
assertEquals(0, phaser.arrive());
awaitTermination(t);
......@@ -594,20 +594,20 @@ public class PhaserTest extends JSR166TestCase {
public void testArriveAndAwaitAdvanceAfterInterrupt() {
final Phaser phaser = new Phaser();
assertEquals(0, phaser.register());
final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
final CountDownLatch pleaseArrive = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
Thread.currentThread().interrupt();
assertEquals(0, phaser.register());
pleaseInterrupt.countDown();
pleaseArrive.countDown();
assertTrue(Thread.currentThread().isInterrupted());
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertTrue(Thread.currentThread().isInterrupted());
assertTrue(Thread.interrupted());
}});
await(pleaseInterrupt);
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
await(pleaseArrive);
waitForThreadToEnterWaitState(t);
Thread.currentThread().interrupt();
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertTrue(Thread.interrupted());
......@@ -628,11 +628,11 @@ public class PhaserTest extends JSR166TestCase {
assertFalse(Thread.currentThread().isInterrupted());
pleaseInterrupt.countDown();
assertEquals(1, phaser.arriveAndAwaitAdvance());
assertTrue(Thread.currentThread().isInterrupted());
assertTrue(Thread.interrupted());
}});
await(pleaseInterrupt);
waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
waitForThreadToEnterWaitState(t);
t.interrupt();
Thread.currentThread().interrupt();
assertEquals(1, phaser.arriveAndAwaitAdvance());
......@@ -807,7 +807,7 @@ public class PhaserTest extends JSR166TestCase {
assertEquals(THREADS, phaser.getArrivedParties());
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
for (Thread thread : threads)
waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS);
waitForThreadToEnterWaitState(thread);
for (Thread thread : threads)
assertTrue(thread.isAlive());
assertState(phaser, 0, THREADS + 1, 1);
......
......@@ -299,7 +299,6 @@ public class StampedLockTest extends JSR166TestCase {
* interruptible operations throw InterruptedException when pre-interrupted
*/
public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
Action[] interruptibleLockActions = {
......@@ -364,7 +363,6 @@ public class StampedLockTest extends JSR166TestCase {
* interruptible operations throw InterruptedException when write locked and interrupted
*/
public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
......@@ -387,7 +385,6 @@ public class StampedLockTest extends JSR166TestCase {
* interruptible operations throw InterruptedException when read locked and interrupted
*/
public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long s = lock.readLock();
......@@ -506,29 +503,32 @@ public class StampedLockTest extends JSR166TestCase {
}
/**
* A writelock succeeds only after a reading thread unlocks
* writeLock() succeeds only after a reading thread unlocks
*/
public void testWriteAfterReadLock() throws InterruptedException {
final CountDownLatch running = new CountDownLatch(1);
final CountDownLatch aboutToLock = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long rs = lock.readLock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
running.countDown();
aboutToLock.countDown();
long s = lock.writeLock();
assertTrue(lock.isWriteLocked());
assertFalse(lock.isReadLocked());
lock.unlockWrite(s);
}});
running.await();
waitForThreadToEnterWaitState(t, MEDIUM_DELAY_MS);
aboutToLock.await();
waitForThreadToEnterWaitState(t);
assertFalse(lock.isWriteLocked());
assertTrue(lock.isReadLocked());
lock.unlockRead(rs);
awaitTermination(t);
assertFalse(lock.isWriteLocked());
assertUnlocked(lock);
}
/**
* A writelock succeeds only after reading threads unlock
* writeLock() succeeds only after reading threads unlock
*/
public void testWriteAfterMultipleReadLocks() {
final StampedLock lock = new StampedLock();
......@@ -551,35 +551,36 @@ public class StampedLockTest extends JSR166TestCase {
assertFalse(lock.isWriteLocked());
lock.unlockRead(s);
awaitTermination(t2);
assertFalse(lock.isWriteLocked());
assertUnlocked(lock);
}
/**
* Readlocks succeed only after a writing thread unlocks
* readLock() succeed only after a writing thread unlocks
*/
public void testReadAfterWriteLock() {
final StampedLock lock = new StampedLock();
final CountDownLatch threadsStarted = new CountDownLatch(2);
final long s = lock.writeLock();
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() {
threadsStarted.countDown();
long rs = lock.readLock();
lock.unlockRead(rs);
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
final Runnable acquireReleaseReadLock = new CheckedRunnable() {
public void realRun() {
threadsStarted.countDown();
long rs = lock.readLock();
assertTrue(lock.isReadLocked());
assertFalse(lock.isWriteLocked());
lock.unlockRead(rs);
}});
}};
Thread t1 = newStartedThread(acquireReleaseReadLock);
Thread t2 = newStartedThread(acquireReleaseReadLock);
await(threadsStarted);
waitForThreadToEnterWaitState(t1, MEDIUM_DELAY_MS);
waitForThreadToEnterWaitState(t2, MEDIUM_DELAY_MS);
waitForThreadToEnterWaitState(t1);
waitForThreadToEnterWaitState(t2);
assertTrue(lock.isWriteLocked());
assertFalse(lock.isReadLocked());
releaseWriteLock(lock, s);
awaitTermination(t1);
awaitTermination(t2);
assertUnlocked(lock);
}
/**
......@@ -765,22 +766,24 @@ public class StampedLockTest extends JSR166TestCase {
*/
public void testValidateOptimisticWriteLocked2()
throws InterruptedException {
final CountDownLatch running = new CountDownLatch(1);
final CountDownLatch locked = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
final long p = assertValid(lock, lock.tryOptimisticRead());
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLockInterruptibly();
running.countDown();
locked.countDown();
lock.writeLockInterruptibly();
}});
running.await();
locked.await();
assertFalse(lock.validate(p));
assertEquals(0L, lock.tryOptimisticRead());
waitForThreadToEnterWaitState(t);
t.interrupt();
awaitTermination(t);
assertTrue(lock.isWriteLocked());
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册