Skip to content

Commit 780bfa6

Browse files
committed
ConcurrentHashMap examples
1 parent 01dfc04 commit 780bfa6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.winterbe.java8.samples.concurrent;
2+
3+
import java.util.concurrent.ConcurrentHashMap;
4+
import java.util.concurrent.ForkJoinPool;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
public class ConcurrentHashMap1 {
10+
11+
public static void main(String[] args) {
12+
testForEach();
13+
}
14+
15+
private static void testForEach() {
16+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
17+
map.putIfAbsent("foo", "bar");
18+
map.putIfAbsent("han", "solo");
19+
map.putIfAbsent("r2", "d2");
20+
map.putIfAbsent("c3", "p0");
21+
22+
23+
// map.forEach((key, value) -> System.out.printf("key: %s; value: %s\n", key, value));
24+
25+
System.out.println("Parallelism: " + ForkJoinPool.getCommonPoolParallelism());
26+
27+
map.forEach(1, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
28+
// map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
29+
30+
System.out.println(map.mappingCount());
31+
}
32+
33+
}

0 commit comments

Comments
 (0)