Search in sources :

Example 26 with ConcurrentLinkedDeque

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

Example 27 with ConcurrentLinkedDeque

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

Example 28 with ConcurrentLinkedDeque

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());
}
Also used : ByteBuffer(java.nio.ByteBuffer) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Test(org.junit.Test)

Example 29 with ConcurrentLinkedDeque

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);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ExecutorService(java.util.concurrent.ExecutorService) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CountDownLatch(java.util.concurrent.CountDownLatch) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Test(org.junit.Test)

Example 30 with ConcurrentLinkedDeque

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);
    }
}
Also used : DelayedMonoPublisher(org.apache.camel.component.reactive.streams.engine.DelayedMonoPublisher) Assert.assertTrue(org.junit.Assert.assertTrue) BlockingQueue(java.util.concurrent.BlockingQueue) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Test(org.junit.Test) DelayedMonoPublisher(org.apache.camel.component.reactive.streams.engine.DelayedMonoPublisher) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) TimeUnit(java.util.concurrent.TimeUnit) TestSubscriber(org.apache.camel.component.reactive.streams.support.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Flowable(io.reactivex.Flowable) After(org.junit.After) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) Test(org.junit.Test)

Aggregations

ConcurrentLinkedDeque (java.util.concurrent.ConcurrentLinkedDeque)209 Test (org.junit.Test)21 NoSuchElementException (java.util.NoSuchElementException)16 Iterator (java.util.Iterator)14 Random (java.util.Random)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 Deque (java.util.Deque)7 ExecutorService (java.util.concurrent.ExecutorService)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 IOException (java.io.IOException)4 List (java.util.List)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 BlockIdList (alluxio.grpc.BlockIdList)3 BlockStoreLocationProto (alluxio.grpc.BlockStoreLocationProto)3 LocationBlockIdListEntry (alluxio.grpc.LocationBlockIdListEntry)3