现象
在我们使用java8中的parallel流时可能会遇到线程安全问题,如下面的例子:
@Test
public void test1() {
System.out.println("==== test ArrayList ====");
for (int i = 0; i < 10; i++) {
// the initial capacity is must
List<Integer> list = new ArrayList<>(10000);
IntStream.range(0, 10000)
.boxed()
.parallel()
.forEach(data -> list.add(data));
System.out.println("NO." + i + ": " + list.size());
}
// test HashMap
System.out.println("==== test HashMap ====");
for (int i = 0; i < 10; i++) {
Map<Integer, Integer> map = new HashMap<>();
IntStream.range(0, 10000)
.boxed()
.parallel()
.forEach(data -> map.put(data, data));
System.out.println("NO." + i + ": " + map.size());
}
}
运行结果:
==== test ArrayList ====
NO.0: 8634
NO.1: 6443
NO.2: 6229
NO.3: 3624
NO.4: 2894
NO.5: 3888
NO.6: 3405
NO.7: 3722
NO.8: 3532
NO.9: 3850
==== test HashMap ====
NO.0: 8355
NO.1: 7732
NO.2: 8986
NO.3: 8796
NO.4: 8620
NO.5: 8873
NO.6: 7203
NO.7: 7882
NO.8: 8382
NO.9: 7346
解决方案
1. 使用java8提供的收集器
@Test
public void test2() {
System.out.println("==== test List ====");
for (int i = 0; i < 10; i++) {
List<Integer> list = IntStream.range(0, 10000)
.boxed()
.parallel()
.collect(Collectors.toList());
System.out.println("NO." + i + ": " + list.size());
}
// test HashMap
System.out.println("==== test Map ====");
for (int i = 0; i < 10; i++) {
Map<Integer, Integer> map = IntStream.range(0, 10000)
.boxed()
.parallel()
.collect(Collectors.toMap(
data -> data,
data -> data
)
);
System.out.println("NO." + i + ": " + map.size());
}
}
结果
==== test List ====
NO.0: 10000
NO.1: 10000
NO.2: 10000
NO.3: 10000
NO.4: 10000
NO.5: 10000
NO.6: 10000
NO.7: 10000
NO.8: 10000
NO.9: 10000
==== test Map ====
NO.0: 100000
NO.1: 100000
NO.2: 100000
NO.3: 100000
NO.4: 100000
NO.5: 100000
NO.6: 100000
NO.7: 100000
NO.8: 100000
NO.9: 100000
2. 使用Collections.synchronizedList()
和Collections.synchronizedMap()
分别包装我们的ArrayList和HashMap
@Test
public void test3() {
System.out.println("==== test ArrayList ====");
for (int i = 0; i < 10; i++) {
List<Integer> list = Collections
.synchronizedList(new ArrayList<>(10000));
IntStream.range(0, 10000)
.boxed()
.parallel()
.forEach(data -> list.add(data));
System.out.println("NO." + i + ": " + list.size());
}
// test HashMap
System.out.println("==== test HashMap ====");
for (int i = 0; i < 10; i++) {
Map<Integer, Integer> map = Collections.synchronizedMap(new HashMap<>());
IntStream.range(0, 10000)
.boxed()
.parallel()
.forEach(data -> map.put(data, data));
System.out.println("NO." + i + ": " + map.size());
}
}
结果
==== test ArrayList ====
NO.0: 10000
NO.1: 10000
NO.2: 10000
NO.3: 10000
NO.4: 10000
NO.5: 10000
NO.6: 10000
NO.7: 10000
NO.8: 10000
NO.9: 10000
==== test HashMap ====
NO.0: 10000
NO.1: 10000
NO.2: 10000
NO.3: 10000
NO.4: 10000
NO.5: 10000
NO.6: 10000
NO.7: 10000
NO.8: 10000
NO.9: 10000