forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentHashMap1.java
More file actions
33 lines (23 loc) · 1.06 KB
/
ConcurrentHashMap1.java
File metadata and controls
33 lines (23 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.winterbe.java8.samples.concurrent;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool;
/**
* @author Benjamin Winterberg
*/
public class ConcurrentHashMap1 {
public static void main(String[] args) {
testForEach();
}
private static void testForEach() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0");
// map.forEach((key, value) -> System.out.printf("key: %s; value: %s\n", key, value));
System.out.println("Parallelism: " + ForkJoinPool.getCommonPoolParallelism());
map.forEach(1, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
// map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
System.out.println(map.mappingCount());
}
}