Java Code Examples for java.util.concurrent.ConcurrentLinkedDeque#addFirst()

The following examples show how to use java.util.concurrent.ConcurrentLinkedDeque#addFirst() . 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: ConcurrentLinkedDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addFirst(null) throws NPE
 */
public void testAddFirstNull() {
    ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
    try {
        q.addFirst(null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 2
Source File: ConcurrentLinkedDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addFirst(x) succeeds
 */
public void testAddFirst() {
    ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
    q.addFirst(zero);
    q.addFirst(one);
    assertSame(one, q.peekFirst());
    assertSame(zero, q.peekLast());
}
 
Example 3
Source File: ConcurrentLinkedDequeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * addFirst(null) throws NPE
 */
public void testAddFirstNull() {
    ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
    try {
        q.addFirst(null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 4
Source File: ConcurrentLinkedDequeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * addFirst(x) succeeds
 */
public void testAddFirst() {
    ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
    q.addFirst(zero);
    q.addFirst(one);
    assertSame(one, q.peekFirst());
    assertSame(zero, q.peekLast());
}
 
Example 5
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void maybeOffload(CompletableFuture<PositionImpl> finalPromise) {
    if (!offloadMutex.tryLock()) {
        scheduledExecutor.schedule(safeRun(() -> maybeOffloadInBackground(finalPromise)),
                                   100, TimeUnit.MILLISECONDS);
    } else {
        CompletableFuture<PositionImpl> unlockingPromise = new CompletableFuture<>();
        unlockingPromise.whenComplete((res, ex) -> {
                offloadMutex.unlock();
                if (ex != null) {
                    finalPromise.completeExceptionally(ex);
                } else {
                    finalPromise.complete(res);
                }
            });

        if (config.getLedgerOffloader() != null && config.getLedgerOffloader() != NullLedgerOffloader.INSTANCE
                && config.getLedgerOffloader().getOffloadPolicies() != null) {
            long threshold = config.getLedgerOffloader().getOffloadPolicies().getManagedLedgerOffloadThresholdInBytes();

            long sizeSummed = 0;
            long alreadyOffloadedSize = 0;
            long toOffloadSize = 0;

            ConcurrentLinkedDeque<LedgerInfo> toOffload = new ConcurrentLinkedDeque<>();

            // go through ledger list from newest to oldest and build a list to offload in oldest to newest order
            for (Map.Entry<Long, LedgerInfo> e : ledgers.descendingMap().entrySet()) {
                long size = e.getValue().getSize();
                sizeSummed += size;
                boolean alreadyOffloaded = e.getValue().hasOffloadContext()
                        && e.getValue().getOffloadContext().getComplete();
                if (alreadyOffloaded) {
                    alreadyOffloadedSize += size;
                } else if (sizeSummed > threshold) {
                    toOffloadSize += size;
                    toOffload.addFirst(e.getValue());
                }
            }

            if (toOffload.size() > 0) {
                log.info("[{}] Going to automatically offload ledgers {}"
                                + ", total size = {}, already offloaded = {}, to offload = {}",
                        name, toOffload.stream().map(LedgerInfo::getLedgerId).collect(Collectors.toList()),
                        sizeSummed, alreadyOffloadedSize, toOffloadSize);
            } else {
                // offloadLoop will complete immediately with an empty list to offload
                log.debug("[{}] Nothing to offload, total size = {}, already offloaded = {}, threshold = {}",
                        name, sizeSummed, alreadyOffloadedSize, threshold);
            }

            offloadLoop(unlockingPromise, toOffload, PositionImpl.latest, Optional.empty());
        }
    }
}