use of java.util.concurrent.locks.Condition in project jstorm by alibaba.
the class NettyClientAsync method releaseFlowCtrlsForRemoteAddr.
private void releaseFlowCtrlsForRemoteAddr(String remoteAddr) {
Set<Integer> targetTasks = remoteAddrToTasks.get(remoteAddr);
if (targetTasks != null) {
try {
flowCtrlLock.lock();
for (Integer taskId : targetTasks) {
Condition condition = targetTasksUnderFlowCtrl.remove(taskId);
if (condition != null) {
try {
lock.lock();
condition.signalAll();
} finally {
lock.unlock();
}
}
}
} finally {
flowCtrlLock.unlock();
}
}
}
use of java.util.concurrent.locks.Condition in project jstorm by alibaba.
the class NettyClientAsync method handleResponse.
@Override
public void handleResponse(Channel channel, Object msg) {
if (msg == null) {
return;
}
TaskMessage message = (TaskMessage) msg;
short type = message.get_type();
if (type == TaskMessage.BACK_PRESSURE_REQUEST) {
byte[] messageData = message.message();
ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE + 1);
buffer.put(messageData);
buffer.flip();
boolean startFlowCtrl = buffer.get() == 1 ? true : false;
int targetTaskId = buffer.getInt();
//LOG.debug("Received flow ctrl ({}) for target task-{}", startFlowCtrl, targetTaskId);
Set<Integer> targetTasks = remoteAddrToTasks.get(channel.getRemoteAddress().toString());
if (targetTasks != null) {
synchronized (targetTasks) {
if (!targetTasks.contains(targetTaskId)) {
targetTasks.add(targetTaskId);
}
}
} else {
LOG.warn("TargetTasks set was not initialized correctly!");
Set<Integer> taskSet = new HashSet<Integer>();
taskSet.add(targetTaskId);
remoteAddrToTasks.put(channel.getRemoteAddress().toString(), taskSet);
}
try {
flowCtrlLock.lock();
if (startFlowCtrl) {
targetTasksUnderFlowCtrl.put(targetTaskId, lock.newCondition());
//LOG.debug("Start flow ctrl for target task-{}", targetTaskId);
} else {
Condition condition = targetTasksUnderFlowCtrl.remove(targetTaskId);
if (condition != null) {
try {
lock.lock();
condition.signalAll();
} finally {
lock.unlock();
}
}
MessageBatch cache = null;
synchronized (targetTasksCache) {
if (targetTasksCache.get(targetTaskId) != null) {
cache = targetTasksCache.remove(targetTaskId);
}
}
if (cache != null) {
pushBatch(cache);
}
}
} finally {
flowCtrlLock.unlock();
}
} else {
LOG.warn("Unexpected message (type={}) was received from task {}", type, message.task());
}
}
use of java.util.concurrent.locks.Condition in project CoreNLP by stanfordnlp.
the class StanfordCoreNLPClient method annotate.
/**
* {@inheritDoc}
*
* This method creates an async call to the server, and blocks until the server
* has finished annotating the object.
*/
@Override
public void annotate(Annotation annotation) {
final Lock lock = new ReentrantLock();
final Condition annotationDone = lock.newCondition();
annotate(Collections.singleton(annotation), 1, (Annotation annInput) -> {
try {
lock.lock();
annotationDone.signal();
} finally {
lock.unlock();
}
});
try {
lock.lock();
// Only wait for one callback to complete; only annotating one document
annotationDone.await();
} catch (InterruptedException e) {
log.info("Interrupt while waiting for annotation to return");
} finally {
lock.unlock();
}
}
use of java.util.concurrent.locks.Condition in project guava by hceylan.
the class Monitor method waitUninterruptibly.
@GuardedBy("lock")
private boolean waitUninterruptibly(Guard guard, long timeoutNanos, boolean signalBeforeWaiting) {
if (!guard.isSatisfied()) {
long startNanos = System.nanoTime();
if (signalBeforeWaiting) {
signalConditionsOfSatisfiedGuards(null);
}
boolean interruptIgnored = false;
try {
incrementWaiters(guard);
try {
final Condition condition = guard.condition;
long remainingNanos = timeoutNanos;
do {
if (remainingNanos <= 0) {
return false;
}
try {
remainingNanos = condition.awaitNanos(remainingNanos);
} catch (InterruptedException ignored) {
try {
signalConditionsOfSatisfiedGuards(guard);
} catch (Throwable throwable) {
Thread.currentThread().interrupt();
throw Throwables.propagate(throwable);
}
interruptIgnored = true;
remainingNanos = (timeoutNanos - (System.nanoTime() - startNanos));
}
} while (!guard.isSatisfied());
} finally {
decrementWaiters(guard);
}
} finally {
if (interruptIgnored) {
Thread.currentThread().interrupt();
}
}
}
return true;
}
use of java.util.concurrent.locks.Condition in project yyl_example by Relucent.
the class LockConditionTest method main.
public static void main(String[] args) {
int workCount = 3;
Lock lock = new ReentrantLock();
Condition[] conditions = new Condition[workCount];
for (int i = 0; i < workCount; i++) {
conditions[i] = lock.newCondition();
}
for (int i = 0; i < workCount; i++) {
new Worker(lock, conditions, i).start();
}
}
Aggregations