Java 8: ConcurrentHashMap Atomic Updates

Java 8: ConcurrentHashMap Atomic Updates

Here's a great look at what Java 8 brought to ConcurrentHashMaps, including close looks at the methods at your disposal and their performance impacts. Let's explore it with us now.

Here's a great look at what Java 8 brought to ConcurrentHashMaps, including close looks at the methods at your disposal and their performance impacts.

Whilst doing some refactoring on updates to ConcurrentHashMap values, I came across these great articles ...

... and was inspired to try to develop the theme a bit further.

Pre-Java 8, we had various ways to try to perform atomic operations on the values of Concurrent collections as described by Dima.

For example, a simple counter:

Java

1

// Incrementing a count of the occurrences of a currency symbol

2

// (In reality we would have used an atomic variable even pre Java 8)

3

ConcurrentHashMap <String Integer> map = new ConcurrentHashMap <>();

4

String key = "USD/JPY";

5

Double oldValue; Double newValue; double increment = 1.0;

6

do {

7

    oldValue = results.get(key);

8

    newValue = oldValue == null? increment: oldValue + increment;

9

} while (!results.replace(key, oldValue, newValue));

java tutorial java 8 java performance concurrenthashmap atomic updates

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Laravel 8 Tutorial for Beginners

Hello everyone! I just updated this tutorial for Laravel 8. In this tutorial, we'll go through the basics of the Laravel framework by building a simple blogging system. Note that this tutorial is only for beginners who are interested in web...

Going Beyond Java 8: Local Variable Type Inference (var) - DZone Java

In this article, we will talk about the most important new feature introduced with Java 10, officially called local variable type inference. An extremely important function in java. You will regret skipping this article.

Laravel 8 Tutorial For Beginners

In this tutorial, i will provide you some useful tutorial of laravel 8 version. So, you can learn laravel 8 an easy way. <div style = "background-color:#cce5f6;border-left:solid 4px...

Java Tutorial for Beginners | Java Tutorial | Java Programming | Core Java Tutorial | Learn Java

This "Java Tutorial for Beginners" Full course will help you master all the major concepts in java such as Arrays, Polymorphism, Abstraction, Inheritance, and Collections.