本文整理汇总了Java中java.util.concurrent.locks.Condition.signalAll方法的典型用法代码示例。如果您正苦于以下问题:Java Condition.signalAll方法的具体用法?Java Condition.signalAll怎么用?Java Condition.signalAll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.locks.Condition
的用法示例。
在下文中一共展示了Condition.signalAll方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAwaitUninterruptibly
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testAwaitUninterruptibly(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition condition = lock.newCondition();
final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() {
// Interrupt before awaitUninterruptibly
lock.lock();
pleaseInterrupt.countDown();
Thread.currentThread().interrupt();
condition.awaitUninterruptibly();
assertTrue(Thread.interrupted());
lock.unlock();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() {
// Interrupt during awaitUninterruptibly
lock.lock();
pleaseInterrupt.countDown();
condition.awaitUninterruptibly();
assertTrue(Thread.interrupted());
lock.unlock();
}});
await(pleaseInterrupt);
t2.interrupt();
lock.lock();
lock.unlock();
assertThreadBlocks(t1, Thread.State.WAITING);
assertThreadBlocks(t2, Thread.State.WAITING);
lock.lock();
condition.signalAll();
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
示例2: testSignalAll
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch pleaseSignal = new CountDownLatch(2);
class Awaiter extends CheckedRunnable {
public void realRun() throws InterruptedException {
lock.lock();
pleaseSignal.countDown();
await(c, awaitMethod);
lock.unlock();
}
}
Thread t1 = newStartedThread(new Awaiter());
Thread t2 = newStartedThread(new Awaiter());
await(pleaseSignal);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
示例3: unlockAll
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void unlockAll() {
iLock.lock();
try {
iLog.debug("Unlocking all ...");
Condition allLocked = iAllLocked;
iAllLocked = null;
allLocked.signalAll();
iLog.debug("Unlocked: all");
} finally {
iLock.unlock();
}
}
示例4: unlock
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
private void unlock(Collection<Long> ids) {
iLock.lock();
try {
if (ids == null || ids.isEmpty()) return;
iLog.debug("Unlocking " + ids + " ...");
Condition myCondition = null;
for (Long id: ids)
myCondition = iIndividualLocks.remove(id);
if (myCondition != null)
myCondition.signalAll();
iLog.debug("Unlocked: " + ids);
} finally {
iLock.unlock();
}
}
示例5: testSignalAll_IMSE
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testSignalAll_IMSE(boolean fair) {
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
final Condition c = lock.writeLock().newCondition();
try {
c.signalAll();
shouldThrow();
} catch (IllegalMonitorStateException success) {}
}
示例6: testAwaitUninterruptibly
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testAwaitUninterruptibly(boolean fair) {
final Lock lock = new ReentrantReadWriteLock(fair).writeLock();
final Condition condition = lock.newCondition();
final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() {
// Interrupt before awaitUninterruptibly
lock.lock();
pleaseInterrupt.countDown();
Thread.currentThread().interrupt();
condition.awaitUninterruptibly();
assertTrue(Thread.interrupted());
lock.unlock();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() {
// Interrupt during awaitUninterruptibly
lock.lock();
pleaseInterrupt.countDown();
condition.awaitUninterruptibly();
assertTrue(Thread.interrupted());
lock.unlock();
}});
await(pleaseInterrupt);
t2.interrupt();
lock.lock();
lock.unlock();
assertThreadBlocks(t1, Thread.State.WAITING);
assertThreadBlocks(t2, Thread.State.WAITING);
lock.lock();
condition.signalAll();
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
示例7: testAwaitLockCount
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testAwaitLockCount(boolean fair) {
final PublicReentrantReadWriteLock lock =
new PublicReentrantReadWriteLock(fair);
final Condition c = lock.writeLock().newCondition();
final CountDownLatch locked = new CountDownLatch(2);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
assertWriteLockedByMoi(lock);
assertEquals(1, lock.writeLock().getHoldCount());
locked.countDown();
c.await();
assertWriteLockedByMoi(lock);
assertEquals(1, lock.writeLock().getHoldCount());
lock.writeLock().unlock();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
lock.writeLock().lock();
assertWriteLockedByMoi(lock);
assertEquals(2, lock.writeLock().getHoldCount());
locked.countDown();
c.await();
assertWriteLockedByMoi(lock);
assertEquals(2, lock.writeLock().getHoldCount());
lock.writeLock().unlock();
lock.writeLock().unlock();
}});
await(locked);
lock.writeLock().lock();
assertHasWaiters(lock, c, t1, t2);
c.signalAll();
assertHasNoWaiters(lock, c);
lock.writeLock().unlock();
awaitTermination(t1);
awaitTermination(t2);
}
示例8: testAwaitLockCount
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testAwaitLockCount(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch pleaseSignal = new CountDownLatch(2);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertLockedByMoi(lock);
assertEquals(1, lock.getHoldCount());
pleaseSignal.countDown();
c.await();
assertLockedByMoi(lock);
assertEquals(1, lock.getHoldCount());
lock.unlock();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
lock.lock();
assertLockedByMoi(lock);
assertEquals(2, lock.getHoldCount());
pleaseSignal.countDown();
c.await();
assertLockedByMoi(lock);
assertEquals(2, lock.getHoldCount());
lock.unlock();
lock.unlock();
}});
await(pleaseSignal);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertEquals(1, lock.getHoldCount());
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
}
示例9: signalOtherThreadsToStart
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
private void signalOtherThreadsToStart(
final TServer server,
final Lock startLock,
final Condition startCondition,
final AtomicBoolean startedServing) {
// A simple thread to wait until the server has started and then signal the other threads to
// begin
Thread t = new Thread() {
@Override
public void run() {
do {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOG.warn("Signalling thread was interuppted: " + e.getMessage());
}
} while (!server.isServing());
startLock.lock();
try {
startedServing.set(true);
startCondition.signalAll();
} finally {
startLock.unlock();
}
}
};
t.start();
}
示例10: run
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void run() {
Object key = null;
final ReentrantLock dr = RLJBarJUnitTest.DeathRow;
final ReentrantLock bar = RLJBarJUnitTest.bar;
final ReentrantLock end = RLJBarJUnitTest.End;
final Condition endCondition = RLJBarJUnitTest.EndCondition;
if (RLJBarJUnitTest.OneKey)
key = new Integer(0); // per-thread v. per iteration
// The barrier has a number of interesting effects:
// 1. It enforces full LWP provisioning on T1.
// (nearly all workers park concurrently).
// 2. It gives the C2 compiler thread(s) a chance to run.
// By transiently quiescing the workings the C2 threads
// might avoid starvation.
//
try {
bar.lock();
try {
++RLJBarJUnitTest.nUp;
if (RLJBarJUnitTest.nUp == RLJBarJUnitTest.nThreads) {
if (RLJBarJUnitTest.quiesce != 0) {
RLJBarJUnitTest.barCondition.await(RLJBarJUnitTest.quiesce * 1000000,
TimeUnit.NANOSECONDS);
}
RLJBarJUnitTest.epoch = System.currentTimeMillis();
RLJBarJUnitTest.barCondition.signalAll();
// System.out.print ("Consensus ") ;
}
if (RLJBarJUnitTest.UseBar) {
while (RLJBarJUnitTest.nUp != RLJBarJUnitTest.nThreads) {
RLJBarJUnitTest.barCondition.await();
}
}
} finally {
bar.unlock();
}
} catch (Exception ex) {
System.out.println("Exception in barrier: " + ex);
}
// Main execution time ... the code being timed ...
// HashTable.get() is highly contended (serial).
for (int loop = 1; loop < 100000; loop++) {
if (!RLJBarJUnitTest.OneKey)
key = new Integer(0);
buddiesOnline.get(key);
}
// Mutator epilog:
// The following code determines if the test will/wont include (measure)
// thread death time.
end.lock();
try {
++RLJBarJUnitTest.nDead;
if (RLJBarJUnitTest.nDead == RLJBarJUnitTest.nUp) {
// System.out.print((System.currentTimeMillis()-RLJBar.epoch) + " ms") ;
endCondition.signalAll();
}
} finally {
end.unlock();
}
dr.lock();
dr.unlock();
}
示例11: getOrCreateClientMmap
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
ClientMmap getOrCreateClientMmap(ShortCircuitReplica replica,
boolean anchored) {
Condition newCond;
lock.lock();
try {
while (replica.mmapData != null) {
if (replica.mmapData instanceof MappedByteBuffer) {
ref(replica);
MappedByteBuffer mmap = (MappedByteBuffer)replica.mmapData;
return new ClientMmap(replica, mmap, anchored);
} else if (replica.mmapData instanceof Long) {
long lastAttemptTimeMs = (Long)replica.mmapData;
long delta = Time.monotonicNow() - lastAttemptTimeMs;
if (delta < mmapRetryTimeoutMs) {
if (LOG.isTraceEnabled()) {
LOG.trace(this + ": can't create client mmap for " +
replica + " because we failed to " +
"create one just " + delta + "ms ago.");
}
return null;
}
if (LOG.isTraceEnabled()) {
LOG.trace(this + ": retrying client mmap for " + replica +
", " + delta + " ms after the previous failure.");
}
} else if (replica.mmapData instanceof Condition) {
Condition cond = (Condition)replica.mmapData;
cond.awaitUninterruptibly();
} else {
Preconditions.checkState(false, "invalid mmapData type %s",
replica.mmapData.getClass().getName());
}
}
newCond = lock.newCondition();
replica.mmapData = newCond;
} finally {
lock.unlock();
}
MappedByteBuffer map = replica.loadMmapInternal();
lock.lock();
try {
if (map == null) {
replica.mmapData = Long.valueOf(Time.monotonicNow());
newCond.signalAll();
return null;
} else {
outstandingMmapCount++;
replica.mmapData = map;
ref(replica);
newCond.signalAll();
return new ClientMmap(replica, map, anchored);
}
} finally {
lock.unlock();
}
}
示例12: testGetWaitingThreads
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testGetWaitingThreads(boolean fair) {
final PublicReentrantReadWriteLock lock =
new PublicReentrantReadWriteLock(fair);
final Condition c = lock.writeLock().newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
locked1.countDown();
c.await();
lock.writeLock().unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
assertFalse(lock.getWaitingThreads(c).isEmpty());
locked2.countDown();
c.await();
lock.writeLock().unlock();
}});
lock.writeLock().lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
lock.writeLock().unlock();
t1.start();
await(locked1);
t2.start();
await(locked2);
lock.writeLock().lock();
assertTrue(lock.hasWaiters(c));
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertTrue(lock.getWaitingThreads(c).contains(t2));
assertEquals(2, lock.getWaitingThreads(c).size());
c.signalAll();
assertHasNoWaiters(lock, c);
lock.writeLock().unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}
示例13: testGetWaitQueueLength
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testGetWaitQueueLength(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertFalse(lock.hasWaiters(c));
assertEquals(0, lock.getWaitQueueLength(c));
locked1.countDown();
c.await();
lock.unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertTrue(lock.hasWaiters(c));
assertEquals(1, lock.getWaitQueueLength(c));
locked2.countDown();
c.await();
lock.unlock();
}});
lock.lock();
assertEquals(0, lock.getWaitQueueLength(c));
lock.unlock();
t1.start();
await(locked1);
lock.lock();
assertHasWaiters(lock, c, t1);
assertEquals(1, lock.getWaitQueueLength(c));
lock.unlock();
t2.start();
await(locked2);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertEquals(2, lock.getWaitQueueLength(c));
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}
示例14: testGetWaitingThreads
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testGetWaitingThreads(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
locked1.countDown();
c.await();
lock.unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertFalse(lock.getWaitingThreads(c).isEmpty());
locked2.countDown();
c.await();
lock.unlock();
}});
lock.lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
lock.unlock();
t1.start();
await(locked1);
lock.lock();
assertHasWaiters(lock, c, t1);
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertFalse(lock.getWaitingThreads(c).contains(t2));
assertEquals(1, lock.getWaitingThreads(c).size());
lock.unlock();
t2.start();
await(locked2);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertTrue(lock.getWaitingThreads(c).contains(t2));
assertEquals(2, lock.getWaitingThreads(c).size());
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}