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();
}
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();
}
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();
}
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();
}
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);
}
}
Aggregations