use of java.util.concurrent.ConcurrentLinkedDeque in project mapdb by jankotek.
the class ConcurrentLinkedDequeTest method testOffer.
/**
* offer(x) succeeds
*/
public void testOffer() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
assertTrue(q.offer(zero));
assertTrue(q.offer(one));
assertSame(zero, q.peekFirst());
assertSame(one, q.peekLast());
}
use of java.util.concurrent.ConcurrentLinkedDeque in project mapdb by jankotek.
the class ConcurrentLinkedDequeTest method testPush.
/**
* peekFirst() returns element inserted with push
*/
public void testPush() {
ConcurrentLinkedDeque q = populatedDeque(3);
q.pollLast();
q.push(four);
assertSame(four, q.peekFirst());
}
use of java.util.concurrent.ConcurrentLinkedDeque in project hbase by apache.
the class TestBoundedByteBufferPool method testBufferSizeGrowWithMultiThread.
@Test
public void testBufferSizeGrowWithMultiThread() throws Exception {
final ConcurrentLinkedDeque<ByteBuffer> bufferQueue = new ConcurrentLinkedDeque<>();
int takeBufferThreadsCount = 30;
int putBufferThreadsCount = 1;
Thread[] takeBufferThreads = new Thread[takeBufferThreadsCount];
for (int i = 0; i < takeBufferThreadsCount; i++) {
takeBufferThreads[i] = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
ByteBuffer buffer = reservoir.getBuffer();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
break;
}
bufferQueue.offer(buffer);
if (Thread.currentThread().isInterrupted())
break;
}
}
});
}
Thread[] putBufferThread = new Thread[putBufferThreadsCount];
for (int i = 0; i < putBufferThreadsCount; i++) {
putBufferThread[i] = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
ByteBuffer buffer = bufferQueue.poll();
if (buffer != null) {
reservoir.putBuffer(buffer);
}
if (Thread.currentThread().isInterrupted())
break;
}
}
});
}
for (int i = 0; i < takeBufferThreadsCount; i++) {
takeBufferThreads[i].start();
}
for (int i = 0; i < putBufferThreadsCount; i++) {
putBufferThread[i].start();
}
// Let the threads run for 2 secs
Thread.sleep(2 * 1000);
for (int i = 0; i < takeBufferThreadsCount; i++) {
takeBufferThreads[i].interrupt();
takeBufferThreads[i].join();
}
for (int i = 0; i < putBufferThreadsCount; i++) {
putBufferThread[i].interrupt();
putBufferThread[i].join();
}
// None of the BBs we got from pool is growing while in use. So we should not change the
// runningAverage in pool
assertEquals(initialByteBufferSize, this.reservoir.getRunningAverage());
}
use of java.util.concurrent.ConcurrentLinkedDeque in project zipkin by openzipkin.
the class DeduplicatingExecutorTest method multithreaded.
/**
* This shows that any number of threads perform a computation only once.
*/
@Test
public void multithreaded() throws Exception {
Session session = mock(Session.class);
DeduplicatingExecutor executor = new DeduplicatingExecutor(session, TimeUnit.SECONDS.toMillis(1L));
BoundStatement statement = mock(BoundStatement.class);
when(session.executeAsync(statement)).thenAnswer(invocationOnMock -> mock(ResultSetFuture.class));
int loopCount = 1000;
CountDownLatch latch = new CountDownLatch(loopCount);
ExecutorService exec = Executors.newFixedThreadPool(10);
Collection<ListenableFuture<?>> futures = new ConcurrentLinkedDeque<>();
for (int i = 0; i < loopCount; i++) {
exec.execute(() -> {
futures.add(executor.maybeExecuteAsync(statement, "foo"));
futures.add(executor.maybeExecuteAsync(statement, "bar"));
latch.countDown();
});
}
latch.await();
ImmutableSet<ListenableFuture<?>> distinctFutures = ImmutableSet.copyOf(futures);
assertThat(distinctFutures).hasSize(2);
// expire the result
Thread.sleep(1000L);
// Sanity check: we don't memoize after we should have expired.
assertThat(executor.maybeExecuteAsync(statement, "foo")).isNotIn(distinctFutures);
assertThat(executor.maybeExecuteAsync(statement, "bar")).isNotIn(distinctFutures);
}
use of java.util.concurrent.ConcurrentLinkedDeque in project camel by apache.
the class DelayedMonoPublisherTest method testMultipleSubscribersMixedArrivalException.
@Test
public void testMultipleSubscribersMixedArrivalException() throws Exception {
DelayedMonoPublisher<Integer> pub = new DelayedMonoPublisher<>(service);
Exception ex = new RuntimeException("An exception");
ConcurrentLinkedDeque<Throwable> exceptions = new ConcurrentLinkedDeque<>();
CountDownLatch latch = new CountDownLatch(2);
Flowable.fromPublisher(pub).doOnError(exceptions::add).doOnError(e -> latch.countDown()).subscribe();
Thread.sleep(200);
pub.setException(ex);
Flowable.fromPublisher(pub).doOnError(exceptions::add).doOnError(e -> latch.countDown()).subscribe();
assertTrue(latch.await(1, TimeUnit.SECONDS));
assertEquals(2, exceptions.size());
for (Throwable t : exceptions) {
assertEquals(ex, t);
}
}
Aggregations