Search in sources :

Example 6 with Condition

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();
        }
    }
}
Also used : Condition(java.util.concurrent.locks.Condition)

Example 7 with Condition

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());
    }
}
Also used : Condition(java.util.concurrent.locks.Condition) ByteBuffer(java.nio.ByteBuffer) TaskMessage(backtype.storm.messaging.TaskMessage) HashSet(java.util.HashSet)

Example 8 with Condition

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();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 9 with Condition

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;
}
Also used : Condition(java.util.concurrent.locks.Condition) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 10 with Condition

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();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

Condition (java.util.concurrent.locks.Condition)34 ReentrantLock (java.util.concurrent.locks.ReentrantLock)20 Lock (java.util.concurrent.locks.Lock)14 Test (org.junit.Test)9 HttpResponse (org.apache.http.HttpResponse)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ByteBuffer (java.nio.ByteBuffer)3 User (com.lonepulse.robozombie.model.User)2 InvocationException (com.lonepulse.robozombie.proxy.InvocationException)2 UUID (java.util.UUID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 Event (org.apache.ignite.events.Event)2 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)2 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)2 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)2 Test (org.testng.annotations.Test)2 TaskMessage (backtype.storm.messaging.TaskMessage)1 AbstractCacheListener (co.paralleluniverse.galaxy.AbstractCacheListener)1