java.util.concurrent.atomic.AtomicInteger.updateAndGet()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(123)

本文整理了Java中java.util.concurrent.atomic.AtomicInteger.updateAndGet()方法的一些代码示例,展示了AtomicInteger.updateAndGet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AtomicInteger.updateAndGet()方法的具体详情如下:
包路径:java.util.concurrent.atomic.AtomicInteger
类名称:AtomicInteger
方法名:updateAndGet

AtomicInteger.updateAndGet介绍

[英]Atomically updates the current value with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
[中]原子地使用应用给定函数的结果更新当前值,并返回更新后的值。该函数应该没有副作用,因为当尝试的更新由于线程之间的争用而失败时,可以重新应用该函数。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

/** Sets the stopped. */
public void setStopped( boolean stopped ) {
 status.updateAndGet( v -> stopped ? v | BitMaskStatus.STOPPED.mask : ( BitMaskStatus.BIT_STATUS_SUM
   ^ BitMaskStatus.STOPPED.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * Sets whether the transformation is running.
 *
 * @param running true if the transformation is running, false otherwise
 */
public void setRunning( boolean running ) {
 status.updateAndGet( v -> running ? v | RUNNING.mask : ( BIT_STATUS_SUM ^ RUNNING.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * Sets whether the transformation is preparing for execution.
 *
 * @param preparing true if the transformation is preparing for execution, false otherwise
 */
public void setPreparing( boolean preparing ) {
 status.updateAndGet( v -> preparing ? v | PREPARING.mask : ( BIT_STATUS_SUM ^ PREPARING.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

public void setFinished( boolean finished ) {
 status.updateAndGet( v -> finished ? v | BitMaskStatus.FINISHED.mask : ( BitMaskStatus.BIT_STATUS_SUM
   ^ BitMaskStatus.FINISHED.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * Sets whether the transformation is initializing.
 *
 * @param initializing true if the transformation is initializing, false otherwise
 */
public void setInitializing( boolean initializing ) {
 status.updateAndGet( v -> initializing ? v | INITIALIZING.mask : ( BIT_STATUS_SUM ^ INITIALIZING.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

protected void setInitialized( boolean initialized ) {
 status.updateAndGet( v -> initialized ? v | BitMaskStatus.INITIALIZED.mask : ( BitMaskStatus.BIT_STATUS_SUM
   ^ BitMaskStatus.INITIALIZED.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

public void setPaused( boolean paused ) {
 status.updateAndGet( v -> paused ? v | PAUSED.mask : ( BIT_STATUS_SUM ^ PAUSED.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

public void setStopped( boolean stopped ) {
 status.updateAndGet( v -> stopped ? v | STOPPED.mask : ( BIT_STATUS_SUM ^ STOPPED.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

protected void setActive( boolean active ) {
 status.updateAndGet( v -> active ? v | BitMaskStatus.ACTIVE.mask : ( BitMaskStatus.BIT_STATUS_SUM
   ^ BitMaskStatus.ACTIVE.mask ) & v );
}

代码示例来源:origin: pentaho/pentaho-kettle

protected void setFinished( boolean finished ) {
 status.updateAndGet( v -> finished ? v | FINISHED.mask : ( BIT_STATUS_SUM ^ FINISHED.mask ) & v );
}

代码示例来源:origin: datumbox/datumbox-framework

/**
 * Sets the record in a particular position in the dataset, WITHOUT updating
 * the internal meta-info and returns the previous value (null if not existed).
 * This method is similar to set() and it allows quick updates
 * on the dataset. Nevertheless it is not advised to use this method because
 * unless you explicitly call the recalculateMeta() method, the meta data
 * will be corrupted. If you do use this method, MAKE sure you perform the
 * recalculation after you are done with the updates.
 *
 * @param rId
 * @param r
 * @return
 */
public Record _unsafe_set(Integer rId, Record r) {
  //move ahead the next id
  data.atomicNextAvailableRecordId.updateAndGet(x -> (x<rId)?Math.max(x+1,rId+1):x);
  return data.records.put(rId, r);
}

代码示例来源:origin: oracle/helidon

@Override
public void lengthen() {
  int factor = prolongationFactor.updateAndGet((i) -> {
    if (i > 0) {
      return ++i;
    } else {
      return 1;
    }
  });
  Duration candidate = lengthenFunction.apply(delay, factor);
  delay = max.compareTo(candidate) > 0 ? candidate : max;
}

代码示例来源:origin: oracle/helidon

@Override
public void shorten() {
  int factor = prolongationFactor.updateAndGet((i) -> {
    if (i < 0) {
      return --i;
    } else {
      return -1;
    }
  });
  Duration candidate = shortenFunction.apply(delay, -factor);
  delay = min.compareTo(candidate) > 0 ? min : candidate;
}

代码示例来源:origin: biezhi/learn-java8

private static void testUpdate() {
  atomicInt.set(0);
  ExecutorService executor = Executors.newFixedThreadPool(2);
  IntStream.range(0, NUM_INCREMENTS)
      .forEach(i -> {
        Runnable task = () ->
            atomicInt.updateAndGet(n -> n + 2);
        executor.submit(task);
      });
  ConcurrentUtils.stop(executor);
  System.out.format("Update: %d\n", atomicInt.get());
}

代码示例来源:origin: org.eclipse.jetty/jetty-io

private ManagedSelector chooseSelector()
{
  return _selectors[_selectorIndex.updateAndGet(_selectorIndexUpdate)];
}

代码示例来源:origin: lucko/helper

@Nonnull
@Override
public E previous() {
  return this.objects.get(this.cursor.updateAndGet(i -> {
    if (i == 0) {
      return this.size - 1;
    }
    return i - 1;
  }));
}

代码示例来源:origin: lucko/helper

@Nonnull
@Override
public E next() {
  return this.objects.get(this.cursor.updateAndGet(i -> {
    int n = i + 1;
    if (n >= this.size) {
      return 0;
    }
    return n;
  }));
}

代码示例来源:origin: io.helidon.config/helidon-config

@Override
public void shorten() {
  int factor = prolongationFactor.updateAndGet((i) -> {
    if (i < 0) {
      return --i;
    } else {
      return -1;
    }
  });
  Duration candidate = shortenFunction.apply(delay, -factor);
  delay = min.compareTo(candidate) > 0 ? min : candidate;
}

代码示例来源:origin: stackoverflow.com

void increment(int incValue, AtomicInteger i) {
  // The lambda is closed over incValue. Because of this the created IntUnaryOperator
  // will have a field which contains incValue. Because of this a new instance must
  // be allocated on every call to the increment method.
  i.updateAndGet(value -> incValue + value);

  // The lambda is not closed over anything. The same IntBinaryOperator instance
  // can be used on every call to the increment method, nothing need to be allocated.
  i.accumulateAndGet(incValue, (incValueParam, value) -> incValueParam + value);
}

代码示例来源:origin: nurkiewicz/rxjava-workshop

public static void main6(String[] args) {
  AtomicInteger atomic = new AtomicInteger();
  
  int cur;
  do {
    cur = atomic.get();
  } while (!atomic.compareAndSet(cur, cur * 2));
  
  atomic.updateAndGet(x -> x * 2);
}

相关文章