java.util.concurrent.atomic.AtomicInteger#updateAndGet()源码实例Demo

下面列出了java.util.concurrent.atomic.AtomicInteger#updateAndGet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Java-Coding-Problems   文件: Main.java
public static void main(String[] args) throws InterruptedException {

        System.out.println("updateAndGet(), accumulateAndGet():");
        AtomicIntegerArray atomicArray = new AtomicIntegerArray(new int[]{3, 4, 2, 5});
        for (int i = 0; i < atomicArray.length(); i++) {
            atomicArray.updateAndGet(i, elem -> elem * elem);
        }
        System.out.println("Result: " + atomicArray);

        AtomicInteger nr1 = new AtomicInteger(3);
        int result1 = nr1.accumulateAndGet(5, (x, y) -> x * y); // x = 3, y = 5

        AtomicInteger nr2 = new AtomicInteger(3);
        int result2 = nr2.updateAndGet(x -> 5 * x);

        System.out.println("Result (nr1): " + result1);
        System.out.println("Result (nr2): " + result2);

        System.out.println("\naddAndGet():");
        AtomicInteger nr3 = new AtomicInteger(3);
        int result3 = nr3.addAndGet(4);
        System.out.println("Result (nr3): " + result3);

        System.out.println("\ncompareAndSet():");
        AtomicInteger nr4 = new AtomicInteger(3);
        boolean wasSet = nr4.compareAndSet(3, 5);
        System.out.println("Result (nr4): " + nr4.get() + " Was set: " + wasSet);
    }
 
源代码2 项目: prong-uid   文件: DefaultUidGeneratorTest.java
/**
 * Worker run
 */
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
    for (;;) {
        int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
        if (myPosition == SIZE) {
            return;
        }

        doGenerate(uidSet, myPosition);
    }
}
 
源代码3 项目: prong-uid   文件: CachedUidGeneratorTest.java
/**
 * Woker run
 */
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
    for (;;) {
        int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
        if (myPosition == SIZE) {
            return;
        }

        doGenerate(uidSet, myPosition);
    }
}
 
/**
 * Worker run
 */
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
    for (;;) {
        int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
        if (myPosition == SIZE) {
            return;
        }

        doGenerate(uidSet, myPosition);
    }
}
 
源代码5 项目: uid-generator   文件: DefaultUidGeneratorTest.java
/**
 * Worker run
 */
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
    for (;;) {
        int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
        if (myPosition == SIZE) {
            return;
        }

        doGenerate(uidSet, myPosition);
    }
}
 
源代码6 项目: uid-generator   文件: CachedUidGeneratorTest.java
/**
 * Woker run
 */
private void workerRun(Set<Long> uidSet, AtomicInteger control) {
    for (;;) {
        int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1));
        if (myPosition == SIZE) {
            return;
        }

        doGenerate(uidSet, myPosition);
    }
}
 
源代码7 项目: OSPREY3   文件: Kernel.java
public int getBestBlockThreads(AtomicInteger blockThreads) {
	return blockThreads.updateAndGet((int val) -> {
		if (val == -1) {
			val = calcMaxBlockThreads();
		}
		return val;
	});
}
 
源代码8 项目: presto   文件: ThriftHiveMetastore.java
@SafeVarargs
private final <T> T alternativeCall(
        ClientSupplier clientSupplier,
        Predicate<Exception> isValidExceptionalResponse,
        AtomicInteger chosenAlternative,
        Call<T>... alternatives)
        throws TException
{
    checkArgument(alternatives.length > 0, "No alternatives");
    int chosen = chosenAlternative.get();
    checkArgument(chosen == Integer.MAX_VALUE || (0 <= chosen && chosen < alternatives.length), "Bad chosen alternative value: %s", chosen);

    if (chosen != Integer.MAX_VALUE) {
        try (ThriftMetastoreClient client = clientSupplier.createMetastoreClient()) {
            return alternatives[chosen].callOn(client);
        }
    }

    Exception firstException = null;
    for (int i = 0; i < alternatives.length; i++) {
        int position = i;
        try (ThriftMetastoreClient client = clientSupplier.createMetastoreClient()) {
            T result = alternatives[i].callOn(client);
            chosenAlternative.updateAndGet(currentChosen -> Math.min(currentChosen, position));
            return result;
        }
        catch (TException | RuntimeException exception) {
            if (isValidExceptionalResponse.test(exception)) {
                // This is likely a valid response. We are not settling on an alternative yet.
                // We will do it later when we get a more obviously valid response.
                throw exception;
            }
            if (firstException == null) {
                firstException = exception;
            }
            else if (firstException != exception) {
                firstException.addSuppressed(exception);
            }
        }
    }

    verifyNotNull(firstException);
    propagateIfPossible(firstException, TException.class);
    throw propagate(firstException);
}
 
源代码9 项目: xtext-core   文件: LookAheadInfo.java
protected void checkConsistency(ILeafNode node, AtomicInteger currentLookAhead) {
	if (!node.isHidden()) {
		currentLookAhead.updateAndGet(old->old > 0 ? old - 1 : old);
	}
}
 
源代码10 项目: dnsjava   文件: ExtendedResolver.java
private Void handle(Message result, Throwable ex, CompletableFuture<Message> f) {
  AtomicInteger failureCounter = resolvers.get(currentResolver).failures;
  if (ex != null) {
    log.debug(
        "Failed to resolve {}/{}, id={} with resolver {} ({}) on attempt {} of {}, reason={}",
        query.getQuestion().getName(),
        Type.string(query.getQuestion().getType()),
        query.getHeader().getID(),
        currentResolver,
        resolvers.get(currentResolver).resolver,
        attempts[currentResolver],
        retriesPerResolver,
        ex.getMessage());

    failureCounter.incrementAndGet();

    if (endTime - System.nanoTime() < 0) {
      f.completeExceptionally(
          new IOException(
              "Timed out while trying to resolve "
                  + query.getQuestion().getName()
                  + "/"
                  + Type.string(query.getQuestion().type)
                  + ", id="
                  + query.getHeader().getID()));
    } else {
      // go to next resolver, until retries on all resolvers are exhausted
      currentResolver = (currentResolver + 1) % resolvers.size();
      if (attempts[currentResolver] < retriesPerResolver) {
        send().handleAsync((r, t) -> handle(r, t, f));
        return null;
      }

      f.completeExceptionally(ex);
    }
  } else {
    failureCounter.updateAndGet(i -> i > 0 ? (int) Math.log(i) : 0);
    f.complete(result);
  }

  return null;
}