@Override public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException { // try to empty the task queue Runnable task; if (threadSafeQueue) { task = taskQueue.poll(); } else { synchronized (taskQueue) { task = taskQueue.poll(); } } if (task != null) { return task; } // block and wait for a task to be submitted return super.poll(timeout, unit); } }
/** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ public int drainTo(Collection<? super E> c, int maxElements) { if (c == null) throw new NullPointerException(); if (c == this) throw new IllegalArgumentException(); int n = 0; for (E e; n < maxElements && (e = poll()) != null;) { c.add(e); ++n; } return n; }
/** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ public int drainTo(Collection<? super E> c) { if (c == null) throw new NullPointerException(); if (c == this) throw new IllegalArgumentException(); int n = 0; for (E e; (e = poll()) != null;) { c.add(e); ++n; } return n; }
/** * Receives a response from this thread. * * @throws TimeoutException if this thread does not offer a response within a reasonable amount of * time * @throws AssertionFailedError if the given method name does not match the name of the method * this thread has called most recently */ private Response getResponse(String methodName) throws Exception { Response response = responseQueue.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); if (response == null) { throw new TimeoutException(); } assertEquals(methodName, response.methodName); return response; }
/** * Causes this thread to call the named method, and asserts that this thread thereby waits on the * given condition-like object. The lock-like object must have a method equivalent to {@link * java.util.concurrent.locks.ReentrantLock#hasWaiters(java.util.concurrent.locks.Condition)}, * except that the method parameter must accept whatever condition-like object is passed into this * method. */ public void callAndAssertWaits(String methodName, Object conditionLikeObject) throws Exception { checkNotNull(methodName); checkNotNull(conditionLikeObject); // TODO: Restore the following line when Monitor.hasWaiters() no longer acquires the lock. // assertEquals(false, invokeMethod("hasWaiters", conditionLikeObject)); sendRequest(methodName, conditionLikeObject); Thread.sleep(DUE_DILIGENCE_MILLIS); assertEquals(true, invokeMethod("hasWaiters", conditionLikeObject)); assertNull(responseQueue.poll()); }
/** * Causes this thread to call the named method, and asserts that this thread becomes blocked on * the lock-like object. The lock-like object must have a method equivalent to {@link * java.util.concurrent.locks.ReentrantLock#hasQueuedThread(Thread)}. */ public void callAndAssertBlocks(String methodName, Object... arguments) throws Exception { checkNotNull(methodName); checkNotNull(arguments); assertEquals(false, invokeMethod("hasQueuedThread", this)); sendRequest(methodName, arguments); Thread.sleep(DUE_DILIGENCE_MILLIS); assertEquals(true, invokeMethod("hasQueuedThread", this)); assertNull(responseQueue.poll()); }
Integer startedPort = queue.poll(20, TimeUnit.SECONDS); if (startedPort == null) { throw new RuntimeException("Exceeded timeout while waiting for webserver to start.");
private String fetchCodeForNextRequest() { try { GenerationRequest request = requestQueue.poll(TIMEOUT_SECONDS, TimeUnit.SECONDS); Preconditions.checkState(request != null, "Timed out waiting for next request"); elementFuture = request.resultFuture; annotationType = request.annotationType; return request.code; } catch (InterruptedException e) { Thread.currentThread().interrupt(); return null; } }
@Override protected Object handleRequestMessage(Message<?> requestMessage) { Object key = this.correlationStrategy.getCorrelationKey(requestMessage); if (key == null) { throw new MessagingException(requestMessage, "Correlation Strategy returned null"); } Thread existing = this.inProcess.putIfAbsent(key, Thread.currentThread()); if (existing != null) { throw new MessagingException(requestMessage, "Correlation key (" + key + ") is already in use by " + existing.getName()); } SynchronousQueue<Message<?>> syncQueue = createOrObtainQueue(key); try { Message<?> releaseMessage = syncQueue.poll(this.timeout, TimeUnit.MILLISECONDS); if (releaseMessage != null) { return processRelease(key, requestMessage, releaseMessage); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new MessageHandlingException(requestMessage, "Interrupted while waiting for release", e); } finally { this.inProcess.remove(key); this.suspensions.remove(key); } return null; }
/** * wait the given time for a new connection * @param timeout - the time to wait * @param unit - the {@link TimeUnit} * @return a new {@link UDTSession} * @throws InterruptedException */ protected UDTSession accept(long timeout, TimeUnit unit)throws InterruptedException{ return sessionHandoff.poll(timeout, unit); }
public Service<?, ?> waitForServiceFound(final long timeout, final TimeUnit unit) { try { return foundServices.poll(timeout, unit); } catch (final InterruptedException e) { logger.warn("Interrupted when waiting for a service"); return null; } }
processChannel.getProcessNoticeableFuture(); SPI spi = synchronousQueue.poll( spiConfiguration.getRegisterTimeout(), TimeUnit.MILLISECONDS);
public Thread poll() { Entry e = queue.poll(); if (e == null) return null; metric.addNano(System.nanoTime() - e.creationTime); return e.thread; }
public Thread poll() { Entry e = queue.poll(); if (e == null) return null; metric.addNano(System.nanoTime() - e.creationTime); return e.thread; }
public T get(long timeout) { T t = null; try { t = queue.poll(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { log.error("get synchronous obj error", e); } return t; }
public Thread poll() { Entry e = queue.poll(); if (e == null) return null; metric.addNano(System.nanoTime() - e.creationTime); return e.thread; }
public Thread poll() { Entry e = queue.poll(); if (e == null) return null; metric.addNano(System.nanoTime() - e.creationTime); return e.thread; }
@Test public void subscription() throws Exception { TestApplicationContext context = TestUtils.createTestApplicationContext(); SynchronousQueue<String> queue = new SynchronousQueue<>(); TestBean testBean = new TestBean(queue); QueueChannel channel = new QueueChannel(); context.registerChannel("channel", channel); Message<String> message = new GenericMessage<>("testing"); channel.send(message); assertNull(queue.poll()); MethodInvokingMessageHandler handler = new MethodInvokingMessageHandler(testBean, "foo"); handler.setBeanFactory(context); PollingConsumer endpoint = new PollingConsumer(channel, handler); endpoint.setTrigger(new PeriodicTrigger(10)); context.registerEndpoint("testEndpoint", endpoint); context.refresh(); String result = queue.poll(2000, TimeUnit.MILLISECONDS); assertNotNull(result); assertEquals("testing", result); context.close(); }
private void waitFor(int count) throws InterruptedException { int completed = 0; while (completed < count) { String s = completingJob.poll(5, TimeUnit.SECONDS); if (s == null) fail("Hang?"); completed++; completedJobs.add(s); } System.out.println(completedJobs); }
public void concreteWriteReadTest(){ String tainted = TelephonyManager.getDeviceId(); SynchronousQueue<String> q = new SynchronousQueue<String>(); q.add(tainted); //not implemented for SynchronousQueue: q.element(); String taintedElement3 = q.poll(); ConnectionManager cm = new ConnectionManager(); cm.publish(taintedElement3); }