本文整理汇总了Java中java.util.concurrent.locks.Condition类的典型用法代码示例。如果您正苦于以下问题:Java Condition类的具体用法?Java Condition怎么用?Java Condition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Condition类属于java.util.concurrent.locks包,在下文中一共展示了Condition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: NIOConnection
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
NIOConnection(final short p_ownNodeId, final short p_destination, final int p_bufferSize, final int p_flowControlWindowSize,
final float p_flowControlWindowThreshold, final IncomingBufferQueue p_incomingBufferQueue, final MessageHeaderPool p_messageHeaderPool,
final MessageDirectory p_messageDirectory, final RequestMap p_requestMap, final MessageHandlers p_messageHandlers, final BufferPool p_bufferPool,
final AbstractExporterPool p_exporterPool, final NIOSelector p_nioSelector, final NodeMap p_nodeMap, final ReentrantLock p_lock,
final Condition p_cond) {
super(p_ownNodeId);
NIOFlowControl flowControl = new NIOFlowControl(p_destination, p_flowControlWindowSize, p_flowControlWindowThreshold, p_nioSelector, this);
NIOOutgoingRingBuffer outgoingBuffer = new NIOOutgoingRingBuffer(p_bufferSize, p_exporterPool);
NIOPipeIn pipeIn =
new NIOPipeIn(p_ownNodeId, p_destination, p_messageHeaderPool, flowControl, p_messageDirectory, p_requestMap, p_messageHandlers, p_bufferPool,
p_incomingBufferQueue, this);
NIOPipeOut pipeOut = new NIOPipeOut(p_ownNodeId, p_destination, p_bufferSize, flowControl, outgoingBuffer, p_nioSelector, p_nodeMap, this);
setPipes(pipeIn, pipeOut);
m_nioSelector = p_nioSelector;
m_connectionCondLock = p_lock;
m_connectionCond = p_cond;
}
示例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: exceptionIsThrownAndEventIsCleanedUpIfAwaitTimesOut
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
@Test
public void exceptionIsThrownAndEventIsCleanedUpIfAwaitTimesOut() throws TimeoutException, InterruptedException {
SynchronizationEvent synchronizationEvent = mock(SynchronizationEvent.class);
when(synchronizationEvent.isSatisfied()).thenReturn(false);
Condition condition = mock(Condition.class);
// condition#await returns false if time was exceeded
when(condition.await(timeout, TimeUnit.SECONDS)).thenReturn(false);
Lock lock = mock(Lock.class);
when(lock.newCondition()).thenReturn(condition);
SignalizationBasedEventSynchronizer signalizationBasedEventSynchronizer = new SignalizationBasedEventSynchronizer(timeout, lock);
expectedException.expect(TimeoutException.class);
signalizationBasedEventSynchronizer.awaitUntil(synchronizationEvent);
}
示例4: ifIllegalStateExceptionIsThrownIsIsLocked
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
@Test
public void ifIllegalStateExceptionIsThrownIsIsLocked() throws TimeoutException, InterruptedException {
SynchronizationEvent synchronizationEvent = mock(SynchronizationEvent.class);
when(synchronizationEvent.isSatisfied()).thenReturn(false);
Condition condition = mock(Condition.class);
doAnswer(invocationOnMock -> {
Thread.sleep(2000);
return false;
}).when(condition).await(timeout, TimeUnit.SECONDS);
Lock lock = mock(Lock.class);
when(lock.newCondition()).thenReturn(condition);
doNothing().doThrow(new IllegalMonitorStateException()).when(lock).lock();
SignalizationBasedEventSynchronizer signalizationBasedEventSynchronizer = new SignalizationBasedEventSynchronizer(timeout, lock);
Thread signalizingThread = new Thread(new SignalizingRunnable(signalizationBasedEventSynchronizer, synchronizationEvent));
expectedException.expect(TimeoutException.class);
signalizingThread.start();
signalizationBasedEventSynchronizer.awaitUntil(synchronizationEvent);
signalizingThread.join();
}
示例5: lockAll
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
public UnlockAll lockAll() {
iLock.lock();
try {
iLog.debug("Locking all ...");
while (iAllLocked != null)
iAllLocked.awaitUninterruptibly();
iAllLocked = iLock.newCondition();
while (!iIndividualLocks.isEmpty()) {
Condition otherCondition = iIndividualLocks.values().iterator().next();
otherCondition.awaitUninterruptibly();
}
iLog.debug("Locked: all");
return new UnlockAll();
} finally {
iLock.unlock();
}
}
示例6: lock
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
public Unlock lock(Collection<Long> ids) {
iLock.lock();
try {
if (ids == null || ids.isEmpty()) return new Unlock(ids);
iLog.debug("Locking " + ids + " ...");
Condition otherCondition = null;
while ((otherCondition = hasLock(ids)) != null)
otherCondition.awaitUninterruptibly();
Condition myCondition = iLock.newCondition();
for (Long id: ids)
iIndividualLocks.put(id, myCondition);
iLog.debug("Locked: " + ids);
return new Unlock(ids);
} finally {
iLock.unlock();
}
}
示例7: EventQueue
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
public EventQueue() {
for (int i = 0; i < NUM_PRIORITIES; i++) {
queues[i] = new Queue();
}
/*
* NOTE: if you ever have to start the associated event dispatch
* thread at this point, be aware of the following problem:
* If this EventQueue instance is created in
* SunToolkit.createNewAppContext() the started dispatch thread
* may call AppContext.getAppContext() before createNewAppContext()
* completes thus causing mess in thread group to appcontext mapping.
*/
appContext = AppContext.getAppContext();
pushPopLock = (Lock)appContext.get(AppContext.EVENT_QUEUE_LOCK_KEY);
pushPopCond = (Condition)appContext.get(AppContext.EVENT_QUEUE_COND_KEY);
}
示例8: AppContext
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
/**
* Constructor for AppContext. This method is <i>not</i> public,
* nor should it ever be used as such. The proper way to construct
* an AppContext is through the use of SunToolkit.createNewAppContext.
* A ThreadGroup is created for the new AppContext, a Thread is
* created within that ThreadGroup, and that Thread calls
* SunToolkit.createNewAppContext before calling anything else.
* That creates both the new AppContext and its EventQueue.
*
* @param threadGroup The ThreadGroup for the new AppContext
* @see sun.awt.SunToolkit
* @since 1.2
*/
AppContext(ThreadGroup threadGroup) {
numAppContexts.incrementAndGet();
this.threadGroup = threadGroup;
threadGroup2appContext.put(threadGroup, this);
this.contextClassLoader =
AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}
});
// Initialize push/pop lock and its condition to be used by all the
// EventQueues within this AppContext
Lock eventQueuePushPopLock = new ReentrantLock();
put(EVENT_QUEUE_LOCK_KEY, eventQueuePushPopLock);
Condition eventQueuePushPopCond = eventQueuePushPopLock.newCondition();
put(EVENT_QUEUE_COND_KEY, eventQueuePushPopCond);
}
示例9: test
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
void test(ScheduledThreadPoolExecutor p) throws Throwable {
Runnable dummy = new Runnable() { public void run() {
throw new AssertionError("shouldn't get here"); }};
BlockingQueue q = p.getQueue();
ReentrantLock lock = getField(q, "lock");
Condition available = getField(q, "available");
equal(0, p.getPoolSize());
equal(0, p.getLargestPoolSize());
equal(0L, p.getTaskCount());
equal(0L, p.getCompletedTaskCount());
p.schedule(dummy, 1L, HOURS);
// Ensure one pool thread actually waits in timed queue poll
awaitHasWaiters(lock, available, LONG_DELAY_MS);
equal(1, p.getPoolSize());
equal(1, p.getLargestPoolSize());
equal(1L, p.getTaskCount());
equal(0L, p.getCompletedTaskCount());
}
示例10: testAwait
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
public void testAwait(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
locked.countDown();
c.await();
lock.unlock();
}});
await(locked);
lock.lock();
assertHasWaiters(lock, c, t);
c.signal();
assertHasNoWaiters(lock, c);
assertTrue(t.isAlive());
lock.unlock();
awaitTermination(t);
}
示例11: await
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
/**
* Awaits condition "indefinitely" using the specified AwaitMethod.
*/
void await(Condition c, AwaitMethod awaitMethod)
throws InterruptedException {
long timeoutMillis = 2 * LONG_DELAY_MS;
switch (awaitMethod) {
case await:
c.await();
break;
case awaitTimed:
assertTrue(c.await(timeoutMillis, MILLISECONDS));
break;
case awaitNanos:
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
long nanosRemaining = c.awaitNanos(timeoutNanos);
assertTrue(nanosRemaining > timeoutNanos / 2);
assertTrue(nanosRemaining <= timeoutNanos);
break;
case awaitUntil:
assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
break;
default:
throw new AssertionError();
}
}
示例12: testGetWaitQueueLength
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
public void testGetWaitQueueLength(boolean fair) {
final PublicReentrantReadWriteLock lock =
new PublicReentrantReadWriteLock(fair);
final Condition c = lock.writeLock().newCondition();
final CountDownLatch locked = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
assertEquals(0, lock.getWaitQueueLength(c));
locked.countDown();
c.await();
lock.writeLock().unlock();
}});
await(locked);
lock.writeLock().lock();
assertHasWaiters(lock, c, t);
assertEquals(1, lock.getWaitQueueLength(c));
c.signal();
assertHasNoWaiters(lock, c);
assertEquals(0, lock.getWaitQueueLength(c));
lock.writeLock().unlock();
awaitTermination(t);
}
示例13: testAwait
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
public void testAwait(boolean fair) {
final PublicReentrantReadWriteLock lock =
new PublicReentrantReadWriteLock(fair);
final Condition c = lock.writeLock().newCondition();
final CountDownLatch locked = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
locked.countDown();
c.await();
lock.writeLock().unlock();
}});
await(locked);
lock.writeLock().lock();
assertHasWaiters(lock, c, t);
c.signal();
assertHasNoWaiters(lock, c);
assertTrue(t.isAlive());
lock.writeLock().unlock();
awaitTermination(t);
}
示例14: signal
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
private static void signal(Lock lock, Condition condition, Runnable runnable) {
boolean interrupted = Thread.interrupted();
lock.lock();
try {
runnable.run();
condition.signal();
} finally {
lock.unlock();
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
示例15: ImminentEpochEvent
import java.util.concurrent.locks.Condition; //导入依赖的package包/类
/**
* Creates a new instance, which will begin at the specified epoch and efficiently accommodate waiting for epochs up
* to (current epoch) + (horizon).
*
* @param horizon how far in the future an epoch can be waited for without sacrificing efficiency.
* @param epoch the initial epoch; must be at least -1
*/
public ImminentEpochEvent(int horizon, long epoch) {
if (epoch < -1) {
throw new IndexOutOfBoundsException("Initial epoch cannot be less than -1");
}
_locks = new ReentrantLock[horizon];
_conditions = new Condition[horizon];
_epoch = new AtomicLong(epoch);
for (int i = 0; i < _locks.length; i++) {
_locks[i] = new ReentrantLock();
_conditions[i] = _locks[i].newCondition();
}
}