Concurrent Accumulators

One of the most common scenarios in concurrent programming is updating of numeric counters accessed by multiple threads. There have been many idioms to do this over the years, starting from synchronized blocks (which introduce a high level of contention), to read/write locks to AtomicInteger(s). While the last ones are more efficient, as they rely directly on processor CAS instructions, they require a higher degree of familiarity to implement the required semantics correctly.


With Java 8 this problem is solved at the framework level with new concurrent accumulator classes that enable you to very efficiently increase / decrease the value of a counter in a thread safe manner. This is really a case where it’s not a question of taste, or preference – using these new classes in your code is really a no-brainer.


Are there any other language features you think every developers should know about? Add them in the comments section.

要查看或添加评论,请登录

Amit Rai的更多文章

  • Difference between array and arraylist #java

    Difference between array and arraylist #java

    I. Size: Array in Java is fixed in size.

  • Java Stream API for Bulk Data Operations on Collections

    Java Stream API for Bulk Data Operations on Collections

    A new java.util.

  • Nashorn

    Nashorn

    Nashorn: A Next-Generation JavaScript Engine for the JVM by Julien Ponge Scenarios for using Oracle Nashorn as a…

  • CLUSTER

    CLUSTER

    A single instance of Node.js runs in a single thread.

  • Parallel Operations

    Parallel Operations

    With the addition of Lambda expressions to arrays operations, Java introduced a key concept into the language of…

  • RxJS

    RxJS

    What is RxJS? RxJS or Reactive Extensions for JavaScript is a library for transforming, composing, and querying streams…

  • HTTP/2 vs HTTP 1.x

    HTTP/2 vs HTTP 1.x

    Let’s take a look at the key differences compared to HTTP 1.x and what issue each improvement is addressing: HTTP/2 is…

  • Garbage-First Garbage Collector

    Garbage-First Garbage Collector

    The Garbage-First (G1) garbage collector is a server-style garbage collector, targeted for multiprocessor machines with…

  • Exception Propagation

    Exception Propagation

    After a method throws an exception, the runtime system searches the call stack for a method that contains a block of…

  • Microbenchmarks

    Microbenchmarks

    Microbenchmarks are coming The Java Microbenchmarking Harness (JMH) by Alexey Shipilev is taking the next step in its…

社区洞察

其他会员也浏览了