Example usage for java.util.concurrent ArrayBlockingQueue drainTo

List of usage examples for java.util.concurrent ArrayBlockingQueue drainTo

Introduction

In this page you can find the example usage for java.util.concurrent ArrayBlockingQueue drainTo.

Prototype

public int drainTo(Collection<? super E> c) 

Source Link

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List<Integer> list = new ArrayList<Integer>();

    int capacity = 100;
    ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
    queue.put(0);//from  w  w  w. ja v a  2 s  .c o  m
    queue.put(1);
    queue.put(2);
    queue.drainTo(list);
    System.out.println(queue);
    System.out.println(list);
}

From source file:org.datacleaner.api.AnalyzerResultFutureTest.java

public void testMultiThreadedListenerScenario() throws Exception {
    final int threadCount = 10;

    final Thread[] threads = new Thread[threadCount];
    @SuppressWarnings({ "unchecked" })
    final Listener<NumberResult>[] listeners = new Listener[threadCount];
    final ArrayBlockingQueue<Object> resultQueue = new ArrayBlockingQueue<>(threadCount);

    for (int i = 0; i < listeners.length; i++) {
        listeners[i] = new Listener<NumberResult>() {
            @Override//from  w ww .  j  a v  a2  s . c  o m
            public void onSuccess(NumberResult result) {
                resultQueue.add(result);
            }

            @Override
            public void onError(RuntimeException error) {
                resultQueue.add(error);
            }
        };
    }

    final Ref<NumberResult> resultRef = new LazyRef<NumberResult>() {
        @Override
        protected NumberResult fetch() throws Throwable {
            final long randomSleepTime = (long) (1000 * Math.random());
            Thread.sleep(randomSleepTime);
            return new NumberResult(43);
        }
    };

    final AnalyzerResultFuture<NumberResult> future = new AnalyzerResultFutureImpl<>("foo", resultRef);

    for (int i = 0; i < threads.length; i++) {
        final Listener<NumberResult> listener = listeners[i];
        threads[i] = new Thread() {
            @Override
            public void run() {
                future.addListener(listener);
            }
        };
    }

    final int halfOfTheThreads = threads.length / 2;
    for (int i = 0; i < halfOfTheThreads; i++) {
        threads[i].start();
    }
    for (int i = 0; i < halfOfTheThreads; i++) {
        threads[i].join();
    }

    future.get();

    // to avoid any race conditions we use the drainTo method before calling
    // toString().
    final List<Object> result = new ArrayList<>();
    resultQueue.drainTo(result);

    assertEquals("[43, 43, 43, 43, 43]", result.toString());
    assertEquals(halfOfTheThreads, result.size());

    for (int i = halfOfTheThreads; i < threads.length; i++) {
        threads[i].start();
    }
    for (int i = halfOfTheThreads; i < threads.length; i++) {
        threads[i].join();
    }

    resultQueue.drainTo(result);

    assertEquals("[43, 43, 43, 43, 43, 43, 43, 43, 43, 43]", result.toString());
    assertEquals(threads.length, result.size());
}