-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.pivovarit</groupId> | ||
<version>1.0</version> | ||
<artifactId>java-concurrent-hm-compute-if-absent</artifactId> | ||
|
||
<name>java-concurrent-hm-compute-if-absent</name> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>21</source> | ||
<target>21</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
35 changes: 35 additions & 0 deletions
35
...ncurrent-hm-compute-if-absent/src/main/java/com/pivovarit/chm/ComputeIfAbsentExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.pivovarit.chm; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Stream; | ||
|
||
class ComputeIfAbsentExample { | ||
|
||
public static void main(String[] args) { | ||
var map = new ConcurrentHashMap<>(1024); | ||
var counter = new AtomicInteger(); | ||
Stream.of(1, 2) | ||
.map(tid -> Thread.ofPlatform().start(() -> { | ||
for (int i = 0; i < 10; i++) { | ||
int key = ThreadLocalRandom.current().nextInt(10); | ||
map.computeIfAbsent(key, s -> { | ||
if (counter.incrementAndGet() > 1) { | ||
System.out.println("I told you so!"); | ||
} | ||
doWork(); | ||
counter.decrementAndGet(); | ||
return key; | ||
}); | ||
} | ||
})).forEach(t -> {}); | ||
} | ||
|
||
private static void doWork() { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException ignored) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters