java.util.concurrent.atomic.LongAccumulator Java Examples

The following examples show how to use java.util.concurrent.atomic.LongAccumulator. 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: AccumulatorTest.java    From LearningOfThinkInJava with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception{
    LongAccumulator accumulator=new LongAccumulator(Long::max,Long.MIN_VALUE);
    Thread[] ts=new Thread[100];

    for(int i=0;i<100;i++){
        ts[i]=new Thread(()->{
            Random random=new Random();
            long value=random.nextLong();
            accumulator.accumulate(value);
        });
        ts[i].start();
    }

    for(int i=0;i<100;i++){
        ts[i].join();
    }
    System.out.println(accumulator.longValue());
}
 
Example #2
Source File: LongAccumulatorUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException {
    // given
    ExecutorService executorService = Executors.newFixedThreadPool(8);
    LongBinaryOperator sum = Long::sum;
    LongAccumulator accumulator = new LongAccumulator(sum, 0L);
    int numberOfThreads = 4;
    int numberOfIncrements = 100;

    // when
    Runnable accumulateAction = () -> IntStream.rangeClosed(0, numberOfIncrements).forEach(accumulator::accumulate);

    for (int i = 0; i < numberOfThreads; i++) {
        executorService.execute(accumulateAction);
    }

    // then
    executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
    executorService.shutdown();
    assertEquals(accumulator.get(), 20200);

}
 
Example #3
Source File: QueryScheduler.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor to initialize QueryScheduler
 * @param queryExecutor QueryExecutor engine to use
 * @param resourceManager for managing server thread resources
 * @param serverMetrics server metrics collector
 */
public QueryScheduler(@Nonnull Configuration config, @Nonnull QueryExecutor queryExecutor,
    @Nonnull ResourceManager resourceManager, @Nonnull ServerMetrics serverMetrics,
    @Nonnull LongAccumulator latestQueryTime) {
  Preconditions.checkNotNull(config);
  Preconditions.checkNotNull(queryExecutor);
  Preconditions.checkNotNull(resourceManager);
  Preconditions.checkNotNull(serverMetrics);

  this.serverMetrics = serverMetrics;
  this.resourceManager = resourceManager;
  this.queryExecutor = queryExecutor;
  this.latestQueryTime = latestQueryTime;
  this.queryLogRateLimiter = RateLimiter.create(config.getDouble(QUERY_LOG_MAX_RATE_KEY, DEFAULT_QUERY_LOG_MAX_RATE));
  this.numDroppedLogRateLimiter = RateLimiter.create(1.0d);
  this.numDroppedLogCounter = new AtomicInteger(0);

  LOGGER.info("Query log max rate: {}", queryLogRateLimiter.getRate());
}
 
Example #4
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * accumulates by multiple threads produce correct result
 */
public void testAccumulateAndGetMT() {
    final int incs = 1000000;
    final int nthreads = 4;
    final ExecutorService pool = Executors.newCachedThreadPool();
    LongAccumulator a = new LongAccumulator(Long::max, 0L);
    Phaser phaser = new Phaser(nthreads + 1);
    for (int i = 0; i < nthreads; ++i)
        pool.execute(new AccTask(a, phaser, incs));
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    long expected = incs - 1;
    long result = a.get();
    assertEquals(expected, result);
    pool.shutdown();
}
 
Example #5
Source File: LongAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * accumulates by multiple threads produce correct result
 */
public void testAccumulateAndGetMT() {
    final int incs = 1000000;
    final int nthreads = 4;
    final ExecutorService pool = Executors.newCachedThreadPool();
    LongAccumulator a = new LongAccumulator(Long::max, 0L);
    Phaser phaser = new Phaser(nthreads + 1);
    for (int i = 0; i < nthreads; ++i)
        pool.execute(new AccTask(a, phaser, incs));
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    long expected = incs - 1;
    long result = a.get();
    assertEquals(expected, result);
    pool.shutdown();
}
 
Example #6
Source File: AccumulatorTest.java    From LearningOfThinkInJava with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception{
    LongAccumulator accumulator=new LongAccumulator(Long::max,Long.MIN_VALUE);
    Thread[] ts=new Thread[100];

    for(int i=0;i<100;i++){
        ts[i]=new Thread(()->{
            Random random=new Random();
            long value=random.nextLong();
            accumulator.accumulate(value);
        });
        ts[i].start();
    }

    for(int i=0;i<100;i++){
        ts[i].join();
    }
    System.out.println(accumulator.longValue());
}
 
Example #7
Source File: LongAccumulatorDemo.java    From java-concurrent-programming with MIT License 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    LongAccumulator accumulator = new LongAccumulator(Long::max, Long.MIN_VALUE);
    Thread[] ts = new Thread[1000];

    for(int i=0;i<1000;i++){
        ts[i] = new Thread(()->{
            Random random = new Random();
            long value = random.nextLong();
            accumulator.accumulate(value);
        });
        ts[i].start();
    }
    for(int i=0;i<1000;i++){
        ts[i].join();
    }
    System.out.println(accumulator.longValue());
}
 
Example #8
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * floatValue returns current value.
 */
public void testFloatValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0.0f, ai.floatValue());
    ai.accumulate(1);
    assertEquals(1.0f, ai.floatValue());
}
 
Example #9
Source File: Serial.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #10
Source File: Serial.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #11
Source File: Serial.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #12
Source File: Serial.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #13
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * accumulate accumulates given value to current, and get returns current value
 */
public void testAccumulateAndGet() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.accumulate(-4);
    assertEquals(2, ai.get());
    ai.accumulate(4);
    assertEquals(4, ai.get());
}
 
Example #14
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * reset() causes subsequent get() to return zero
 */
public void testReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.reset();
    assertEquals(0, ai.get());
}
 
Example #15
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getThenReset() returns current value; subsequent get() returns zero
 */
public void testGetThenReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    assertEquals(2, ai.getThenReset());
    assertEquals(0, ai.get());
}
 
Example #16
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toString returns current value.
 */
public void testToString() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals("0", ai.toString());
    ai.accumulate(1);
    assertEquals(Long.toString(1), ai.toString());
}
 
Example #17
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * intValue returns current value.
 */
public void testIntValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0, ai.intValue());
    ai.accumulate(1);
    assertEquals(1, ai.intValue());
}
 
Example #18
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * longValue returns current value.
 */
public void testLongValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0, ai.longValue());
    ai.accumulate(1);
    assertEquals(1, ai.longValue());
}
 
Example #19
Source File: Serial.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #20
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * doubleValue returns current value.
 */
public void testDoubleValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0.0, ai.doubleValue());
    ai.accumulate(1);
    assertEquals(1.0, ai.doubleValue());
}
 
Example #21
Source File: Serial.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #22
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    LongAccumulator a = acc;
    for (int i = 0; i < incs; ++i)
        a.accumulate(i);
    result = a.get();
    phaser.arrive();
}
 
Example #23
Source File: PriorityScheduler.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
public PriorityScheduler(@Nonnull Configuration config, @Nonnull ResourceManager resourceManager,
    @Nonnull QueryExecutor queryExecutor, @Nonnull SchedulerPriorityQueue queue, @Nonnull ServerMetrics metrics,
    @Nonnull LongAccumulator latestQueryTime) {
  super(config, queryExecutor, resourceManager, metrics, latestQueryTime);
  Preconditions.checkNotNull(queue);
  this.queryQueue = queue;
  this.numRunners = resourceManager.getNumQueryRunnerThreads();
  runningQueriesSemaphore = new Semaphore(numRunners);
}
 
Example #24
Source File: Serial.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #25
Source File: QuerySchedulerFactory.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/**
 * Static factory to instantiate query scheduler based on scheduler configuration.
 * 'name' configuration in the scheduler will decide which scheduler instance to create
 * Besides known instances, 'name' can be a classname
 * @param schedulerConfig scheduler specific configuration
 * @param queryExecutor QueryExecutor to use
 * @return returns an instance of query scheduler
 */
public static @Nonnull
QueryScheduler create(@Nonnull Configuration schedulerConfig, @Nonnull QueryExecutor queryExecutor,
    ServerMetrics serverMetrics, @Nonnull LongAccumulator latestQueryTime) {
  Preconditions.checkNotNull(schedulerConfig);
  Preconditions.checkNotNull(queryExecutor);

  String schedulerName =
      schedulerConfig.getString(ALGORITHM_NAME_CONFIG_KEY, DEFAULT_QUERY_SCHEDULER_ALGORITHM).toLowerCase();
  if (schedulerName.equals(FCFS_ALGORITHM)) {
    LOGGER.info("Using FCFS query scheduler");
    return new FCFSQueryScheduler(schedulerConfig, queryExecutor, serverMetrics, latestQueryTime);
  } else if (schedulerName.equals(TOKEN_BUCKET_ALGORITHM)) {
    LOGGER.info("Using Priority Token Bucket scheduler");
    return TokenPriorityScheduler.create(schedulerConfig, queryExecutor, serverMetrics, latestQueryTime);
  } else if (schedulerName.equals(BOUNDED_FCFS_ALGORITHM)) {
    return BoundedFCFSScheduler.create(schedulerConfig, queryExecutor, serverMetrics, latestQueryTime);
  }

  // didn't find by name so try by classname
  QueryScheduler scheduler = getQuerySchedulerByClassName(schedulerName, schedulerConfig, queryExecutor);
  if (scheduler != null) {
    return scheduler;
  }

  // if we don't find the configured algorithm we warn and use the default one
  // because it's better to execute with poor algorithm than completely fail.
  // Failure on bad configuration will cause outage vs an inferior algorithm that
  // will provide degraded service

  LOGGER.warn("Scheduler {} not found. Using default FCFS query scheduler", schedulerName);
  return new FCFSQueryScheduler(schedulerConfig, queryExecutor, serverMetrics, latestQueryTime);
}
 
Example #26
Source File: PrioritySchedulerTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
public static TestPriorityScheduler create(Configuration config) {
  ResourceManager rm = new PolicyBasedResourceManager(config);
  QueryExecutor qe = new TestQueryExecutor();
  groupFactory = new TestSchedulerGroupFactory();
  MultiLevelPriorityQueue queue =
      new MultiLevelPriorityQueue(config, rm, groupFactory, new TableBasedGroupMapper());
  latestQueryTime = new LongAccumulator(Long::max, 0);
  return new TestPriorityScheduler(config, rm, qe, queue, metrics, latestQueryTime);
}
 
Example #27
Source File: PublishKafkaRecord_1_0.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Integer evaluateRecordPath(final RecordPath recordPath, final Record record) {
    final RecordPathResult result = recordPath.evaluate(record);
    final LongAccumulator accumulator = new LongAccumulator(Long::sum, 0);

    result.getSelectedFields().forEach(fieldValue -> {
        final Object value = fieldValue.getValue();
        final long hash = Objects.hashCode(value);
        accumulator.accumulate(hash);
    });

    return accumulator.intValue();
}
 
Example #28
Source File: PublishKafkaRecord_2_0.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Integer evaluateRecordPath(final RecordPath recordPath, final Record record) {
    final RecordPathResult result = recordPath.evaluate(record);
    final LongAccumulator accumulator = new LongAccumulator(Long::sum, 0);

    result.getSelectedFields().forEach(fieldValue -> {
        final Object value = fieldValue.getValue();
        final long hash = Objects.hashCode(value);
        accumulator.accumulate(hash);
    });

    return accumulator.intValue();
}
 
Example #29
Source File: Serial.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");

    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
 
Example #30
Source File: LongAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * reset() causes subsequent get() to return zero
 */
public void testReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.reset();
    assertEquals(0, ai.get());
}