Java Code Examples for java.util.concurrent.locks.Condition#signalAll()
The following examples show how to use
java.util.concurrent.locks.Condition#signalAll() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionManagerWatcherThreadTest.java From rabbitmq-cdi with MIT License | 6 votes |
@Test void testEstablishConnectionSuccessfullButLostAfterSomeTime() throws InterruptedException { ReentrantLock lock = new ReentrantLock(); Condition condition = lock.newCondition(); ConnectionManager connectionManagerMock = mock(ConnectionManager.class); when(connectionManagerMock.tryToEstablishConnection()).thenReturn(true); when(connectionManagerMock.getState()).thenReturn(ConnectionState.NEVER_CONNECTED); ConnectionManagerWatcherThread sut = new ConnectionManagerWatcherThread(lock, condition, connectionManagerMock, 50); sut.start(); Thread.sleep(300); assertTrue(sut.isAlive()); verify(connectionManagerMock).tryToEstablishConnection(); assertEquals(State.WAITING, sut.getState()); try { lock.lock(); condition.signalAll(); } finally { lock.unlock(); } Thread.sleep(200); verify(connectionManagerMock, times(2)).tryToEstablishConnection(); assertEquals(State.WAITING, sut.getState()); killThreadAndVerifyState(sut); }
Example 2
Source File: MqttTestClient.java From diozero with MIT License | 6 votes |
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { if (topic.equals(MqttProviderConstants.RESPONSE_TOPIC)) { // TODO How to handle different response types? DiozeroProtos.Response response = DiozeroProtos.Response.parseFrom(message.getPayload()); Logger.info("Got response message: {}", response); String correlation_id = response.getCorrelationId(); responses.put(correlation_id, response); Condition condition = conditions.remove(correlation_id); if (condition == null) { Logger.error("No condition for correlation id {}", correlation_id); } else { lock.lock(); try { condition.signalAll(); } finally { lock.unlock(); } } } else { Logger.warn("Unrecognised topic {}", topic); } }
Example 3
Source File: ReentrantLockTest.java From j2objc with Apache License 2.0 | 6 votes |
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); }
Example 4
Source File: MetaStoreProxyServer.java From waggle-dance with Apache License 2.0 | 6 votes |
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(() -> { 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(); }
Example 5
Source File: ReentrantLockTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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); }
Example 6
Source File: ReentrantLockTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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); }
Example 7
Source File: ConsumerContainerWatcherThreadTest.java From rabbitmq-cdi with MIT License | 5 votes |
@Test void testNoConnectionEstablishedAfterSomeTime() throws InterruptedException { ReentrantLock lock = new ReentrantLock(); Condition noConnectionCondition = lock.newCondition(); when(consumerContainerMock.isConnectionAvailable()).thenReturn(false); when(consumerContainerMock.ensureConsumersAreActive()).thenReturn(true); ConsumerContainerWatcherThread consumerContainerWatcherThread = new ConsumerContainerWatcherThread(consumerContainerMock, 50, lock, noConnectionCondition); consumerContainerWatcherThread.start(); Thread.sleep(300); assertTrue(consumerContainerWatcherThread.isAlive()); assertEquals(State.WAITING, consumerContainerWatcherThread.getState()); verify(consumerContainerMock, never()).ensureConsumersAreActive(); when(consumerContainerMock.isConnectionAvailable()).thenReturn(true); try { lock.lock(); noConnectionCondition.signalAll(); } finally { lock.unlock(); } Thread.sleep(100); verify(consumerContainerMock).ensureConsumersAreActive(); assertEquals(State.WAITING, consumerContainerWatcherThread.getState()); killThreadAndCheckState(consumerContainerWatcherThread); }
Example 8
Source File: MultiLock.java From unitime with Apache License 2.0 | 5 votes |
public void unlockAll() { iLock.lock(); try { iLog.debug("Unlocking all ..."); Condition allLocked = iAllLocked; iAllLocked = null; allLocked.signalAll(); iLog.debug("Unlocked: all"); } finally { iLock.unlock(); } }
Example 9
Source File: TestStateSystemProvider.java From tracecompass with Eclipse Public License 2.0 | 5 votes |
/** * Signal for the next event to be processed. Calling this method makes * sense only if {@link #setThrottling(boolean)} has been set to true */ public void signalNextEvent() { fLock.lock(); try { Condition cond = fNextEventSignal; if (cond != null) { cond.signalAll(); } } finally { fLock.unlock(); } }
Example 10
Source File: Locks.java From joyqueue with Apache License 2.0 | 5 votes |
/** * 信号通知 * * @param lock 锁 * @param condition 信号量 */ public static void signalAll(final Lock lock, final Condition condition) { lock.lock(); try { condition.signalAll(); } finally { lock.unlock(); } }
Example 11
Source File: MultiLock.java From unitime with Apache License 2.0 | 5 votes |
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(); } }
Example 12
Source File: ReentrantLockTest.java From j2objc with Apache License 2.0 | 5 votes |
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); }
Example 13
Source File: Locks.java From joyqueue with Apache License 2.0 | 5 votes |
/** * 信号通知 * * @param lock 锁 * @param conditions 信号量 */ public static void signalAll(final Lock lock, final Condition... conditions) { lock.lock(); try { for (Condition condition : conditions) { condition.signalAll(); } } finally { lock.unlock(); } }
Example 14
Source File: ReentrantLockTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public void testAwaitUninterruptibly(boolean fair) { final ReentrantLock lock = new ReentrantLock(fair); final Condition c = 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(); c.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.unlock(); }}); Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() { // Interrupt during awaitUninterruptibly lock.lock(); pleaseInterrupt.countDown(); c.awaitUninterruptibly(); assertTrue(Thread.interrupted()); lock.unlock(); }}); await(pleaseInterrupt); lock.lock(); lock.unlock(); t2.interrupt(); assertThreadStaysAlive(t1); assertTrue(t2.isAlive()); lock.lock(); c.signalAll(); lock.unlock(); awaitTermination(t1); awaitTermination(t2); }
Example 15
Source File: ShortCircuitCache.java From hadoop with Apache License 2.0 | 4 votes |
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(); } }
Example 16
Source File: ReentrantLockTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
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); }
Example 17
Source File: ReentrantLockTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
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); }
Example 18
Source File: RLJBarJUnitTest.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
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(); }
Example 19
Source File: ShortCircuitCache.java From big-c with Apache License 2.0 | 4 votes |
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(); } }
Example 20
Source File: ReentrantLockTest.java From j2objc with Apache License 2.0 | 4 votes |
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); }