Example usage for java.util.concurrent.atomic AtomicInteger decrementAndGet

List of usage examples for java.util.concurrent.atomic AtomicInteger decrementAndGet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger decrementAndGet.

Prototype

public final int decrementAndGet() 

Source Link

Document

Atomically decrements the current value, with memory effects as specified by VarHandle#getAndAdd .

Usage

From source file:org.codice.ddf.admin.application.service.migratable.ProfileMigratableTest.java

private static Answer succeedsImportOnLastAttempt(AtomicInteger attempts) {
    return AdditionalAnswers.<Boolean, ProfileMigrationReport, JsonProfile>answer((r, p) -> {
        final int attempt = attempts.decrementAndGet();

        if (attempt <= 0) { // succeeds on last attempt only
            if (attempt == 0) {
                r.recordTask();/* w  w w.  j ava2  s .c o m*/
            } // else - don't record tasks on the verification attempt that will follow the last
              //          attempt
            return true;
        }
        r.recordTask();
        r.recordOnFinalAttempt(new MigrationException("testing import #" + (attempt + 1)));
        return false;
    });
}

From source file:org.springframework.yarn.am.allocate.DefaultAllocateCountTracker.java

/**
 * Modify {@link AtomicInteger} either by incrementing
 * or decrementing value. Value is always kept as
 * non-negative./*from w w  w .j  a  v a2  s  . co  m*/
 *
 * @param value the value to modify
 * @param increment if true increment, if false decrement
 * @return true, if value is modified, false otherwise
 */
private static boolean modify(AtomicInteger value, boolean increment) {
    if (increment) {
        value.incrementAndGet();
        return true;
    } else {
        if (value.get() > 0) {
            value.decrementAndGet();
            return true;
        } else {
            return false;
        }
    }
}

From source file:org.springframework.yarn.am.allocate.DefaultAllocateCountTracker.java

/**
 * Modify {@link AtomicInteger} matched by key from a map
 * either by incrementing or decrementing value. Value is
 * always kept as non-negative.//from  w  w  w.j ava2 s  .  c  o  m
 *
 * @param map the map to search for value
 * @param key the key to find the value
 * @param increment if true increment, if false decrement
 * @return true, if value is modified, false otherwise
 */
private static boolean modifyWithKey(Map<String, AtomicInteger> map, String key, boolean increment) {
    AtomicInteger value = map.get(key);
    if (value != null) {
        if (increment) {
            value.incrementAndGet();
        } else {
            if (value.get() > 0) {
                value.decrementAndGet();
                return true;
            } else {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}

From source file:io.apiman.gateway.engine.vertx.polling.URILoadingRegistry.java

public static void reloadData(IAsyncHandler<Void> doneHandler) {
    synchronized (URILoadingRegistry.class) {
        if (instance == null) {
            doneHandler.handle((Void) null);
            return;
        }/*from ww  w  .ja  v a  2s .c o m*/
        Map<URILoadingRegistry, IAsyncResultHandler<Void>> regs = instance.handlers;
        Vertx vertx = instance.vertx;
        URI uri = instance.uri;
        Map<String, String> config = instance.config;
        AtomicInteger ctr = new AtomicInteger(regs.size());
        OneShotURILoader newLoader = new OneShotURILoader(vertx, uri, config);

        regs.entrySet().stream().forEach(pair -> {
            // Clear the registrys' internal maps to prepare for reload.
            // NB: If we add production hot reloading, we'll need to work around this (e.g. clone?).
            pair.getKey().getMap().clear();
            // Re-subscribe the registry.
            newLoader.subscribe(pair.getKey(), result -> {
                checkAndFlip(ctr.decrementAndGet(), newLoader, doneHandler);
            });
        });
        checkAndFlip(ctr.get(), newLoader, doneHandler);
    }
}

From source file:org.jasig.cas.web.support.AbstractInMemoryThrottledSubmissionHandlerInterceptorAdapter.java

/**
 * This class relies on an external configuration to clean it up. It ignores the threshold data in the parent class.
 *//*from  ww  w. j ava2  s. co m*/
public final void decrementCounts() {
    final Set<String> keys = this.ipMap.keySet();

    for (final Iterator<String> iter = keys.iterator(); iter.hasNext();) {
        final String key = iter.next();
        final AtomicInteger integer = this.ipMap.get(key);
        final int newValue = integer.decrementAndGet();

        if (newValue == 0) {
            iter.remove();
        }
    }
}

From source file:org.limewire.mojito.routing.ClassfulNetworkCounter.java

/**
 * Decrements and returns the current number of Contacts 
 * that are from the same Class C Network as the given
 * Contact.// www .  ja v  a2  s . c o m
 */
public synchronized int decrementAndGet(Contact node) {
    if (bucket.isLocalNode(node)) {
        return 0;
    }

    if (!ContactUtils.isIPv4Address(node)) {
        if (LOG.isInfoEnabled()) {
            LOG.info(node + " has not an IPv4 Address");
        }
        return 0;
    }

    int masked = ContactUtils.getClassC(node);
    AtomicInteger counter = nodesPerNetwork.get(masked);
    if (counter != null) {
        int count = counter.decrementAndGet();
        if (count <= 0) {
            nodesPerNetwork.remove(masked);
            assert (!nodesPerNetwork.containsKey(masked));
        }
        return count;
    }
    return 0;
}

From source file:com.taobao.pushit.server.listener.ConnectionNumberListener.java

public void onConnectionClosed(Connection conn) {
    String remoteIp = null;/* w  ww  . j  ava2s  .c  om*/
    try {
        // IP
        remoteIp = this.getRemoteIp(conn);
        // 
        lock.lock();
        AtomicInteger connNum = this.connectionIpNumMap.get(remoteIp);
        if (connNum == null) {
            return;
        }
        if (connNum.decrementAndGet() <= 0) {
            this.connectionIpNumMap.remove(remoteIp);
        }
    } finally {
        lock.unlock();
    }
}

From source file:com.opengamma.financial.currency.AbstractCurrencyMatrix.java

protected CurrencyMatrixValue removeConversion(final Currency source, final Currency target) {
    ArgumentChecker.notNull(source, "source");
    ArgumentChecker.notNull(target, "target");
    ConcurrentHashMap<Currency, CurrencyMatrixValue> conversions = _values.get(source);
    if (conversions == null) {
        // Nothing from that source
        return null;
    }/* w ww  .  j  a v  a  2 s . c  om*/
    final CurrencyMatrixValue value = conversions.remove(target);
    if (value == null) {
        // No conversion from source to target
        return null;
    }
    // Removed a value, so need to decrease the target's reference count
    AtomicInteger targetCount = _targets.get(target);
    if (targetCount != null) {
        // Target count should never be null at this point
        if (targetCount.decrementAndGet() == 0) {
            // This was the last reference to the target, confirm and remove atomically against the "add" operation
            synchronized (targetCount) {
                if (targetCount.get() == 0) {
                    _targets.remove(target);
                }
            }
        }
    }
    return value;
}

From source file:com.nearinfinity.blur.thrift.AsyncClientPool.java

private void returnClient(Connection connection, TAsyncClient client) throws InterruptedException {
    if (!client.hasError()) {
        getQueue(connection).put(client);
    } else {//from   w  w  w  .  j av a 2  s .  com
        AtomicInteger counter = _numberOfConnections.get(connection.getHost());
        if (counter != null) {
            counter.decrementAndGet();
        }
    }
}

From source file:org.corfudb.runtime.protocols.AsyncPooledThriftClient.java

private void returnClient(TAsyncClient client) throws InterruptedException {
    if (!client.hasError()) {
        getQueue(_connection).put(client);
    } else {//from w  w w .ja v a  2s.  c om
        AtomicInteger counter = _numberOfConnections.get(_connection.getHost());
        if (counter != null) {
            counter.decrementAndGet();
        }
    }
}