Example usage for java.util.concurrent LinkedBlockingQueue addAll

List of usage examples for java.util.concurrent LinkedBlockingQueue addAll

Introduction

In this page you can find the example usage for java.util.concurrent LinkedBlockingQueue addAll.

Prototype

boolean addAll(Collection<? extends E> c);

Source Link

Document

Adds all of the elements in the specified collection to this collection (optional operation).

Usage

From source file:org.apache.distributedlog.admin.DistributedLogAdmin.java

private static Map<String, StreamCandidate> checkStreams(final Namespace namespace,
        final Collection<String> streams, final OrderedScheduler scheduler, final int concurrency)
        throws IOException {
    final LinkedBlockingQueue<String> streamQueue = new LinkedBlockingQueue<String>();
    streamQueue.addAll(streams);
    final Map<String, StreamCandidate> candidateMap = new ConcurrentSkipListMap<String, StreamCandidate>();
    final AtomicInteger numPendingStreams = new AtomicInteger(streams.size());
    final CountDownLatch doneLatch = new CountDownLatch(1);
    Runnable checkRunnable = new Runnable() {
        @Override/*from ww  w .j  a  v a 2  s  .co  m*/
        public void run() {
            while (!streamQueue.isEmpty()) {
                String stream;
                try {
                    stream = streamQueue.take();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
                StreamCandidate candidate;
                try {
                    LOG.info("Checking stream {}.", stream);
                    candidate = checkStream(namespace, stream, scheduler);
                    LOG.info("Checked stream {} - {}.", stream, candidate);
                } catch (Throwable e) {
                    LOG.error("Error on checking stream {} : ", stream, e);
                    doneLatch.countDown();
                    break;
                }
                if (null != candidate) {
                    candidateMap.put(stream, candidate);
                }
                if (numPendingStreams.decrementAndGet() == 0) {
                    doneLatch.countDown();
                }
            }
        }
    };
    Thread[] threads = new Thread[concurrency];
    for (int i = 0; i < concurrency; i++) {
        threads[i] = new Thread(checkRunnable, "check-thread-" + i);
        threads[i].start();
    }
    try {
        doneLatch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    if (numPendingStreams.get() != 0) {
        throw new IOException(numPendingStreams.get() + " streams left w/o checked");
    }
    for (int i = 0; i < concurrency; i++) {
        threads[i].interrupt();
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    return candidateMap;
}

From source file:com.twitter.distributedlog.admin.DistributedLogAdmin.java

private static Map<String, StreamCandidate> checkStreams(
        final com.twitter.distributedlog.DistributedLogManagerFactory factory, final Collection<String> streams,
        final ExecutorService executorService, final BookKeeperClient bkc, final String digestpw,
        final int concurrency) throws IOException {
    final LinkedBlockingQueue<String> streamQueue = new LinkedBlockingQueue<String>();
    streamQueue.addAll(streams);
    final Map<String, StreamCandidate> candidateMap = new ConcurrentSkipListMap<String, StreamCandidate>();
    final AtomicInteger numPendingStreams = new AtomicInteger(streams.size());
    final CountDownLatch doneLatch = new CountDownLatch(1);
    Runnable checkRunnable = new Runnable() {
        @Override/*from   w  w w  .  j av  a 2s. co m*/
        public void run() {
            while (!streamQueue.isEmpty()) {
                String stream;
                try {
                    stream = streamQueue.take();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
                StreamCandidate candidate;
                try {
                    LOG.info("Checking stream {}.", stream);
                    candidate = checkStream(factory, stream, executorService, bkc, digestpw);
                    LOG.info("Checked stream {} - {}.", stream, candidate);
                } catch (IOException e) {
                    LOG.error("Error on checking stream {} : ", stream, e);
                    doneLatch.countDown();
                    break;
                }
                if (null != candidate) {
                    candidateMap.put(stream, candidate);
                }
                if (numPendingStreams.decrementAndGet() == 0) {
                    doneLatch.countDown();
                }
            }
        }
    };
    Thread[] threads = new Thread[concurrency];
    for (int i = 0; i < concurrency; i++) {
        threads[i] = new Thread(checkRunnable, "check-thread-" + i);
        threads[i].start();
    }
    try {
        doneLatch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    if (numPendingStreams.get() != 0) {
        throw new IOException(numPendingStreams.get() + " streams left w/o checked");
    }
    for (int i = 0; i < concurrency; i++) {
        threads[i].interrupt();
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    return candidateMap;
}

From source file:com.twitter.distributedlog.auditor.DLAuditor.java

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/* w w  w .j av  a2  s . c  om*/
        public void execute(String stream) throws IOException {
            collectLedgersFromStream(factory, stream, ledgers);
        }
    });
}

From source file:com.twitter.distributedlog.auditor.DLAuditor.java

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// w  w  w.  ja  v a2  s .  co  m
        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;
}

From source file:com.SecUpwN.AIMSICD.service.AimsicdService.java

/**
 * Updates Neighbouring Cell details/*from w ww .  j a  v a2 s  .  c  o  m*/
 */
public List<Cell> updateNeighbouringCells() {
    List<Cell> neighboringCells = new ArrayList<>();

    List<NeighboringCellInfo> neighboringCellInfo;
    neighboringCellInfo = tm.getNeighboringCellInfo();
    if (neighboringCellInfo.size() == 0) {
        // try to poll the neighboring cells for a few seconds
        final LinkedBlockingQueue<NeighboringCellInfo> neighboringCellBlockingQueue = new LinkedBlockingQueue<>(
                100);
        final PhoneStateListener listener = new PhoneStateListener() {
            private void handle() {
                List<NeighboringCellInfo> neighboringCellInfo;
                neighboringCellInfo = tm.getNeighboringCellInfo();
                if (neighboringCellInfo.size() == 0) {
                    return;
                }
                Log.i(TAG, "neighbouringCellInfo empty - event based polling succeeded!");
                tm.listen(this, PhoneStateListener.LISTEN_NONE);
                neighboringCellBlockingQueue.addAll(neighboringCellInfo);
            }

            @Override
            public void onServiceStateChanged(ServiceState serviceState) {
                handle();
            }

            @Override
            public void onDataConnectionStateChanged(int state) {
                handle();
            }

            @Override
            public void onDataConnectionStateChanged(int state, int networkType) {
                handle();
            }

            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                handle();
            }

            @Override
            public void onCellInfoChanged(List<CellInfo> cellInfo) {
                handle();
            }
        };
        Log.i(TAG, "neighbouringCellInfo empty - start polling");

        //LISTEN_CELL_INFO added in API 17
        if (Build.VERSION.SDK_INT > 16) {
            tm.listen(listener, PhoneStateListener.LISTEN_CELL_INFO | PhoneStateListener.LISTEN_CELL_LOCATION
                    | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_SERVICE_STATE
                    | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        } else {
            tm.listen(listener,
                    PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
                            | PhoneStateListener.LISTEN_SERVICE_STATE
                            | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }

        for (int i = 0; i < 10 && neighboringCellInfo.size() == 0; i++) {
            try {
                Log.i(TAG, "neighbouringCellInfo empty - try " + i);
                NeighboringCellInfo info = neighboringCellBlockingQueue.poll(1, TimeUnit.SECONDS);
                if (info == null) {
                    neighboringCellInfo = tm.getNeighboringCellInfo();
                    if (neighboringCellInfo.size() > 0) {
                        Log.i(TAG, "neighbouringCellInfo empty - try " + i + " succeeded time based");
                        break;
                    } else {
                        continue;
                    }
                }
                ArrayList<NeighboringCellInfo> cellInfoList = new ArrayList<NeighboringCellInfo>(
                        neighboringCellBlockingQueue.size() + 1);
                while (info != null) {
                    cellInfoList.add(info);
                    info = neighboringCellBlockingQueue.poll(1, TimeUnit.SECONDS);
                }
                neighboringCellInfo = cellInfoList;
            } catch (InterruptedException e) {
                // normal
            }
        }
    }

    Log.i(TAG, "neighbouringCellInfo Size - " + neighboringCellInfo.size());
    for (NeighboringCellInfo neighbourCell : neighboringCellInfo) {
        Log.i(TAG, "neighbouringCellInfo - CID:" + neighbourCell.getCid() + " LAC:" + neighbourCell.getLac()
                + " RSSI:" + neighbourCell.getRssi() + " PSC:" + neighbourCell.getPsc());

        final Cell cell = new Cell(neighbourCell.getCid(), neighbourCell.getLac(), neighbourCell.getRssi(),
                neighbourCell.getPsc(), neighbourCell.getNetworkType(), false);
        neighboringCells.add(cell);
    }

    return neighboringCells;
}