Search in sources :

Example 41 with Condition

use of java.util.concurrent.locks.Condition in project jo-client-platform by jo-source.

the class ServerExecutionCallback method userQuestion.

@Override
public UserQuestionResult userQuestion(final String question) {
    final ValueHolder<UserQuestionResult> result = new ValueHolder<UserQuestionResult>();
    final Lock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    interimRequestCallback.request(new IInterimResponseCallback<Object>() {

        @Override
        public void response(final Object response) {
            final UserQuestionResult userQuestionResult = (UserQuestionResult) response;
            result.set(userQuestionResult);
            lock.lock();
            condition.signal();
            lock.unlock();
        }
    }, new UserQuestionRequest(question));
    lock.lock();
    condition.awaitUninterruptibly();
    lock.unlock();
    return result.get();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) UserQuestionResult(org.jowidgets.cap.common.api.execution.UserQuestionResult) ValueHolder(org.jowidgets.util.ValueHolder) UserQuestionRequest(org.jowidgets.cap.remoting.common.UserQuestionRequest) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 42 with Condition

use of java.util.concurrent.locks.Condition in project knime-core by knime.

the class NodeContextTest method testExecutorServiceWithContext.

/**
 * Checks if {@link ThreadUtils#executorServiceWithContext(ExecutorService)} works as expected.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testExecutorServiceWithContext() throws Exception {
    List<NodeContainer> containers = new ArrayList<NodeContainer>(wfm.getNodeContainers());
    // Table Creator
    NodeContext.pushContext(containers.get(0));
    final AtomicReference<NodeContext> ref = new AtomicReference<NodeContext>();
    final ReentrantLock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            ref.set(NodeContext.getContext());
            lock.lock();
            try {
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    };
    ExecutorService executor = ThreadUtils.executorServiceWithContext(executorService);
    lock.lock();
    try {
        executor.execute(runnable);
        condition.await();
    } finally {
        lock.unlock();
    }
    assertThat("Context inheritance via wrapped ExecutorService.execute does now work properly", ref.get(), is(NodeContext.getContext()));
    runnable = new Runnable() {

        @Override
        public void run() {
            ref.set(NodeContext.getContext());
        }
    };
    executor.submit(runnable).get();
    assertThat("Context inheritance via wrapped ExecutorService.submit(Runnable) does now work properly", ref.get(), is(NodeContext.getContext()));
    executor.submit(runnable, new Integer(1)).get();
    assertThat("Context inheritance via wrapped ExecutorService.submit(Runnable) does now work properly", ref.get(), is(NodeContext.getContext()));
    Callable<NodeContext> callable = new Callable<NodeContext>() {

        @Override
        public NodeContext call() {
            return NodeContext.getContext();
        }
    };
    Future<NodeContext> future = executor.submit(callable);
    assertThat("Context inheritance via wrapped ExecutorService.submit(Callable) does now work properly", future.get(), is(NodeContext.getContext()));
    future = executor.invokeAll(Collections.singletonList(callable)).get(0);
    assertThat("Context inheritance via wrapped ExecutorService.invokeAll does now work properly", future.get(), is(NodeContext.getContext()));
    future = executor.invokeAll(Collections.singletonList(callable), 1, TimeUnit.SECONDS).get(0);
    assertThat("Context inheritance via wrapped ExecutorService.invokeAll does now work properly", future.get(), is(NodeContext.getContext()));
    NodeContext ctx = executor.invokeAny(Collections.singletonList(callable));
    assertThat("Context inheritance via wrapped ExecutorService.invokeAny does now work properly", ctx, is(NodeContext.getContext()));
    ctx = executor.invokeAny(Collections.singletonList(callable), 1, TimeUnit.SECONDS);
    assertThat("Context inheritance via wrapped ExecutorService.invokeAny does now work properly", ctx, is(NodeContext.getContext()));
    NodeContext.removeLastContext();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 43 with Condition

use of java.util.concurrent.locks.Condition in project knime-core by knime.

the class NodeContextTest method testSwingWorkerWithContext.

/**
 * Tests if {@link SwingWorkerWithContext} works as expected.
 *
 * @throws Exception if something goes wrong
 */
@Test
public void testSwingWorkerWithContext() throws Exception {
    List<NodeContainer> containers = new ArrayList<NodeContainer>(wfm.getNodeContainers());
    // Table Creator
    NodeContext.pushContext(containers.get(0));
    final AtomicReference<NodeContext> refInBackground = new AtomicReference<NodeContext>();
    final AtomicReference<NodeContext> refInDone = new AtomicReference<NodeContext>();
    final AtomicReference<NodeContext> refInProcess = new AtomicReference<NodeContext>();
    final ReentrantLock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    SwingWorker<NodeContext, NodeContext> worker = new SwingWorkerWithContext<NodeContext, NodeContext>() {

        @Override
        protected NodeContext doInBackgroundWithContext() throws Exception {
            refInBackground.set(NodeContext.getContext());
            publish(NodeContext.getContext());
            return NodeContext.getContext();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void doneWithContext() {
            lock.lock();
            try {
                refInDone.set(NodeContext.getContext());
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void processWithContext(final List<NodeContext> chunks) {
            refInProcess.set(NodeContext.getContext());
        }
    };
    lock.lock();
    try {
        worker.execute();
        condition.await();
    } finally {
        lock.unlock();
    }
    assertThat("Wrong node context in doInBackgroundWithContext", refInBackground.get(), is(sameInstance(NodeContext.getContext())));
    assertThat("Wrong node context in doneWithContext", refInDone.get(), is(sameInstance(NodeContext.getContext())));
    assertThat("Wrong node context in processWithContext", refInProcess.get(), is(sameInstance(NodeContext.getContext())));
    NodeContext.removeLastContext();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) List(java.util.List) SwingWorkerWithContext(org.knime.core.util.SwingWorkerWithContext) Test(org.junit.Test)

Example 44 with Condition

use of java.util.concurrent.locks.Condition in project knime-core by knime.

the class NodeContextTest method testExecutorWithContext.

/**
 * Checks if {@link ThreadUtils#executorWithContext(java.util.concurrent.Executor)} works as expected.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testExecutorWithContext() throws Exception {
    List<NodeContainer> containers = new ArrayList<NodeContainer>(wfm.getNodeContainers());
    // Table Creator
    NodeContext.pushContext(containers.get(0));
    final AtomicReference<NodeContext> ref = new AtomicReference<NodeContext>();
    final ReentrantLock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            ref.set(NodeContext.getContext());
            lock.lock();
            try {
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    };
    Executor executor = ThreadUtils.executorWithContext(executorService);
    lock.lock();
    try {
        executor.execute(runnable);
        condition.await();
    } finally {
        lock.unlock();
    }
    assertThat("Context inheritance via wrapped Executor does now work properly", ref.get(), is(NodeContext.getContext()));
    NodeContext.removeLastContext();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 45 with Condition

use of java.util.concurrent.locks.Condition in project knime-core by knime.

the class WorkflowTestCase method waitWhile.

protected void waitWhile(final NodeContainer nc, final Hold hold) throws Exception {
    if (!hold.shouldHold()) {
        return;
    }
    final ReentrantLock lock = nc instanceof WorkflowManager ? ((WorkflowManager) nc).getReentrantLockInstance() : nc.getParent().getReentrantLockInstance();
    final Condition condition = lock.newCondition();
    NodeStateChangeListener l = new NodeStateChangeListener() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void stateChanged(final NodeStateEvent state) {
            lock.lock();
            try {
                m_logger.info("Received " + state);
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    };
    lock.lock();
    nc.addNodeStateChangeListener(l);
    try {
        while (hold.shouldHold()) {
            int secToWait = hold.getSecondsToWaitAtMost();
            if (secToWait > 0) {
                condition.await(secToWait, TimeUnit.SECONDS);
                break;
            } else {
                condition.await(5, TimeUnit.SECONDS);
            }
        }
    } finally {
        lock.unlock();
        nc.removeNodeStateChangeListener(l);
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition)

Aggregations

Condition (java.util.concurrent.locks.Condition)45 ReentrantLock (java.util.concurrent.locks.ReentrantLock)30 Lock (java.util.concurrent.locks.Lock)18 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)6 HttpResponse (org.apache.http.HttpResponse)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ByteBuffer (java.nio.ByteBuffer)3 User (com.lonepulse.robozombie.model.User)2 InvocationException (com.lonepulse.robozombie.proxy.InvocationException)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Executor (java.util.concurrent.Executor)2 Test (org.testng.annotations.Test)2