Java Code Examples for java.util.concurrent.LinkedBlockingQueue#addAll()

The following examples show how to use java.util.concurrent.LinkedBlockingQueue#addAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DLAuditor.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void collectLedgersFromDL(final URI uri,
                                  final com.twitter.distributedlog.DistributedLogManagerFactory factory,
                                  final Set<Long> ledgers) throws IOException {
    logger.info("Enumerating {} to collect streams.", uri);
    Collection<String> streams = factory.enumerateAllLogsInNamespace();
    final LinkedBlockingQueue<String> streamQueue = new LinkedBlockingQueue<String>();
    streamQueue.addAll(streams);

    logger.info("Collected {} streams from uri {} : {}",
                new Object[] { streams.size(), uri, streams });

    executeAction(streamQueue, 10, new Action<String>() {
        @Override
        public void execute(String stream) throws IOException {
            collectLedgersFromStream(factory, stream, ledgers);
        }
    });
}
 
Example 2
Source File: DLAuditor.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private Map<String, Long> calculateStreamSpaceUsage(
        final URI uri, final com.twitter.distributedlog.DistributedLogManagerFactory factory)
    throws IOException {
    Collection<String> streams = factory.enumerateAllLogsInNamespace();
    final LinkedBlockingQueue<String> streamQueue = new LinkedBlockingQueue<String>();
    streamQueue.addAll(streams);

    final Map<String, Long> streamSpaceUsageMap =
            new ConcurrentSkipListMap<String, Long>();
    final AtomicInteger numStreamsCollected = new AtomicInteger(0);

    executeAction(streamQueue, 10, new Action<String>() {
        @Override
        public void execute(String stream) throws IOException {
            streamSpaceUsageMap.put(stream,
                    calculateStreamSpaceUsage(factory, stream));
            if (numStreamsCollected.incrementAndGet() % 1000 == 0) {
                logger.info("Calculated {} streams from uri {}.", numStreamsCollected.get(), uri);
            }
        }
    });

    return streamSpaceUsageMap;
}
 
Example 3
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addAll(this) throws IllegalArgumentException
 */
public void testAddAllSelf() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    try {
        q.addAll(q);
        shouldThrow();
    } catch (IllegalArgumentException success) {}
}
 
Example 4
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addAll of a collection with any null elements throws NPE after
 * possibly adding some elements
 */
public void testAddAll3() {
    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE - 1; ++i)
        ints[i] = new Integer(i);
    Collection<Integer> elements = Arrays.asList(ints);
    try {
        q.addAll(elements);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 5
Source File: LinkedBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addAll throws IllegalStateException if not enough room
 */
public void testAddAll4() {
    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE; ++i)
        ints[i] = new Integer(i);
    Collection<Integer> elements = Arrays.asList(ints);
    try {
        q.addAll(elements);
        shouldThrow();
    } catch (IllegalStateException success) {}
}
 
Example 6
Source File: MCSS.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
private synchronized LinkedBlockingQueue<IAtomContainer> submitSingleThreadedJob(List<IAtomContainer> mcssList, JobType jobType, int nThreads) {
    LinkedBlockingQueue<IAtomContainer> solutions = new LinkedBlockingQueue<>();
    MCSSThread task = new MCSSThread(mcssList, jobType, 1);
    LinkedBlockingQueue<IAtomContainer> results = task.call();
    if (results != null) {
        solutions.addAll(results);
    }
    return solutions;
}
 
Example 7
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * addAll(this) throws IllegalArgumentException
 */
public void testAddAllSelf() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    try {
        q.addAll(q);
        shouldThrow();
    } catch (IllegalArgumentException success) {}
}
 
Example 8
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * addAll of a collection with any null elements throws NPE after
 * possibly adding some elements
 */
public void testAddAll3() {
    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE - 1; ++i)
        ints[i] = new Integer(i);
    Collection<Integer> elements = Arrays.asList(ints);
    try {
        q.addAll(elements);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 9
Source File: LinkedBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * addAll throws IllegalStateException if not enough room
 */
public void testAddAll4() {
    LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE - 1);
    Integer[] ints = new Integer[SIZE];
    for (int i = 0; i < SIZE; ++i)
        ints[i] = new Integer(i);
    Collection<Integer> elements = Arrays.asList(ints);
    try {
        q.addAll(elements);
        shouldThrow();
    } catch (IllegalStateException success) {}
}
 
Example 10
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private int bkFence(final BookKeeperClient bkc, List<Long> ledgers, int fenceRate) throws Exception {
    if (ledgers.isEmpty()) {
        System.out.println("Nothing to fence. Done.");
        return 0;
    }
    ExecutorService executorService = Executors.newCachedThreadPool();
    final RateLimiter rateLimiter = RateLimiter.create(fenceRate);
    final byte[] passwd = getConf().getBKDigestPW().getBytes(UTF_8);
    final CountDownLatch latch = new CountDownLatch(ledgers.size());
    final AtomicInteger numPendings = new AtomicInteger(ledgers.size());
    final LinkedBlockingQueue<Long> ledgersQueue = new LinkedBlockingQueue<Long>();
    ledgersQueue.addAll(ledgers);

    for (int i = 0; i < concurrency; i++) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (!ledgersQueue.isEmpty()) {
                    rateLimiter.acquire();
                    Long lid = ledgersQueue.poll();
                    if (null == lid) {
                        break;
                    }
                    System.out.println("Fencing ledger " + lid);
                    int numRetries = 3;
                    while (numRetries > 0) {
                        try {
                            LedgerHandle lh = bkc.get().openLedger(lid, BookKeeper.DigestType.CRC32, passwd);
                            lh.close();
                            System.out.println("Fenced ledger " + lid + ", " + numPendings.decrementAndGet() + " left.");
                            latch.countDown();
                        } catch (BKException.BKNoSuchLedgerExistsException bke) {
                            System.out.println("Skipped fence non-exist ledger " + lid + ", " + numPendings.decrementAndGet() + " left.");
                            latch.countDown();
                        } catch (BKException.BKLedgerRecoveryException lre) {
                            --numRetries;
                            continue;
                        } catch (Exception e) {
                            e.printStackTrace();
                            break;
                        }
                        numRetries = 0;
                    }
                }
                System.out.println("Thread exits");
            }
        });
    }
    latch.await();
    SchedulerUtils.shutdownScheduler(executorService, 2, TimeUnit.MINUTES);
    return 0;
}
 
Example 11
Source File: MCSS.java    From ReactionDecoder with GNU Lesser General Public License v3.0 4 votes vote down vote up
private synchronized LinkedBlockingQueue<IAtomContainer> submitMultiThreadedJob(List<IAtomContainer> mcssList, JobType jobType, int nThreads) {
    int taskNumber = 1;
    LinkedBlockingQueue<IAtomContainer> solutions = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Callable<LinkedBlockingQueue<IAtomContainer>>> callablesQueue = new LinkedBlockingQueue<>();
    ExecutorService threadPool = newFixedThreadPool(nThreads);
    int step = (int) ceil(mcssList.size() / nThreads);
    if (step < 2) {
        step = 2; // Can't have a step size of less than 2
    }
    for (int i = 0; i < mcssList.size(); i += step) {
        int endPoint = i + step;
        if (endPoint > mcssList.size()) {
            endPoint = mcssList.size();
        }
        List<IAtomContainer> subList = new ArrayList<>(mcssList.subList(i, endPoint));
        if (subList.size() > 1) {
            MCSSThread mcssJobThread = new MCSSThread(subList, jobType, taskNumber, am ,bm);
            callablesQueue.add(mcssJobThread);
            taskNumber++;
        } else {
            solutions.add(subList.get(0));
        }
    }
    try {
        /*
         * Wait all the threads to finish
         */
        List<Future<LinkedBlockingQueue<IAtomContainer>>> futureList = threadPool.invokeAll(callablesQueue);
        /*
         * Collect the results
         */
        for (Future<LinkedBlockingQueue<IAtomContainer>> callable : futureList) {
            LinkedBlockingQueue<IAtomContainer> mapping = callable.get();
            if (callable.isDone() && mapping != null) {
                solutions.addAll(mapping);
            } else {
                LOGGER.warn("WARNING: InComplete job in AtomMappingTool: ");
            }
        }
        threadPool.shutdown();
        // Wait until all threads are finish
        while (!threadPool.isTerminated()) {
        }
        gc();
    } catch (InterruptedException | ExecutionException e) {
        LOGGER.debug("ERROR: in AtomMappingTool: " + e.getMessage());
        LOGGER.error(e);
    } finally {
        threadPool.shutdown();
    }

    return solutions;
}