1.ArrayList并发更新导致的异常
单线程情况下示例代码:
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(UUID.randomUUID().toString().substring(0, 8));
}
list.forEach(str -> System.out.println(str));
}
}
运行结果:
426cd13c
43650a69
a6edcf46
2dcaa91f
66ef746f
04f486d2
b2e8e66b
316c4d55
b3369413
203f354c
多线程情况下示例代码:
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
new Thread(() -> {
list.add(UUID.randomUUID().toString().substring(0, 8));
System.out.println(list);
}).start();
}
}
}
运行结果:出现java.util.ConcurrentModificationException异常
Exception in thread "Thread-7" java.util.ConcurrentModificationException
[null, dd8ca085, 41235775, 7822dae2]
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
[null, dd8ca085, 41235775, 7822dae2, 7059a1e2, 1f5be300, 112110fd]
at java.util.ArrayList$Itr.next(ArrayList.java:851)
[null, dd8ca085, 41235775, 7822dae2, 7059a1e2, 1f5be300, 112110fd, e5333519, 2063136b]
[null, dd8ca085, 41235775, 7822dae2, 7059a1e2]
[null, dd8ca085, 41235775, 7822dae2, 7059a1e2]
at java.util.AbstractCollection.toString(AbstractCollection.java:461)
[null, dd8ca085, 41235775, 7822dae2, 7059a1e2, 1f5be300]
at java.lang.String.valueOf(String.java:2994)
[null, dd8ca085, 41235775, 7822dae2]
[null, dd8ca085, 41235775, 7822dae2]
at java.io.PrintStream.println(PrintStream.java:821)
[null, dd8ca085, 41235775, 7822dae2, 7059a1e2, 1f5be300, 112110fd, e5333519, 2063136b, 3dcca9e6]
at com.mine.core.list.ListDemo.lambda$main$0(ListDemo.java:14)
at java.lang.Thread.run(Thread.java:745)
- 1.出现java.util.ConcurrentModificationException异常的原因:
并发争抢修改导致(参考花名册前面情况:一同学正在写,另外一个同学过来抢夺,导致数据不一致异常。并发修改异常) - 2.解决方法:
- 2.1 new Vector<>();//同步方法,缺点降低并发性
- 2.2 Collections.synchronizedList(new ArrayList<>());//同步方法包装,缺点降低并发性
- 2.3 new CopyOnWriteArrayList();//读写分离的思想,保证线程安全的前提下,并发性更高
- 3.CopyOnWriteArrayList原理分析:
CopyOnWrite容器即写时复制的容器。往一个容器添加元素的时候,不直接往容器object[]添加,而是先将当前容器object[]进行copy,复制出一个新的容器object[] newElements,然后新的容器object[] newElements里添加元素,添加完元素之后,再将原容器的引用指向新的容器setArray(newElements);这样做的好处是可以对CopyOnWrite容器进行并发的读,而不需要加锁,因为当前容器不会添加任何元素。所以CopyOnWrite容器也是一种读写分离的思想,读和写不同的容器。
源代码:
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len + 1);
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
2.HashSet并发更新导致的异常
多线程情况下示例代码:
public class SetDemo {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
for (int i = 0; i < 30; i++) {
new Thread(() -> {
set.add(UUID.randomUUID().toString().substring(0, 8));
System.out.println(set);
}).start();
}
}
}
运行结果:
Exception in thread "Thread-23" Exception in thread "Thread-24" Exception in thread "Thread-0" java.util.ConcurrentModificationException
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.util.AbstractCollection.toString(AbstractCollection.java:461)
at java.lang.String.valueOf(String.java:2994)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.io.PrintStream.println(PrintStream.java:821)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at com.mine.core.list.SetDemo.lambda$main$0(SetDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
java.util.ConcurrentModificationException
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.util.AbstractCollection.toString(AbstractCollection.java:461)
at java.lang.String.valueOf(String.java:2994)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.io.PrintStream.println(PrintStream.java:821)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
at com.mine.core.list.SetDemo.lambda$main$0(SetDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, a22c181f, 00d9d442, a9586192, 33f0950c]
Exception in thread "Thread-1" java.util.ConcurrentModificationException
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, 3e824226, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, 97689236, 09d79233, 3e824226, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.util.AbstractCollection.toString(AbstractCollection.java:461)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, a1cc66ec, 97689236, 09d79233, 3e824226, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, a1cc66ec, 97689236, 09d79233, 3e824226, a22c181f, 8ee13937, 00d9d442, a9586192, 33f0950c]
at com.mine.core.list.SetDemo.lambda$main$0(SetDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-4" java.util.ConcurrentModificationException
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, a1cc66ec, 97689236, 09d79233, 3e824226, a22c181f, 00d9d442, a9586192, 33f0950c]
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, a1cc66ec, 97689236, 09d79233, 3e824226, a22c181f, 8ee13937, 00d9d442, a9586192, 07c8d0a8, 33f0950c]
at java.util.AbstractCollection.toString(AbstractCollection.java:461)
at java.lang.String.valueOf(String.java:2994)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, a1cc66ec, 97689236, 09d79233, 3e824226, a22c181f, 8ee13937, 00d9d442, a9586192, fe686032, 07c8d0a8, 33f0950c]
at java.io.PrintStream.println(PrintStream.java:821)
at com.mine.core.list.SetDemo.lambda$main$0(SetDemo.java:15)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 53cf4009, 898f7565, c28fea5a, 64d384d7, 06194f4c, f58c090a, 8b98f7be, a1cc66ec, 97689236, 09d79233, 3e824226, a22c181f, 8ee13937, 00d9d442, a9586192, fe686032, 07c8d0a8, 33f0950c, 69816fb6]
at java.lang.Thread.run(Thread.java:745)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 898f7565, 64d384d7, f58c090a, a4655692, 09d79233, 3e824226, 8ee13937, a9586192, 07c8d0a8, 69816fb6, 53cf4009, c28fea5a, 06194f4c, 8b98f7be, a1cc66ec, 97689236, a22c181f, 00d9d442, fe686032, 33f0950c]
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 898f7565, 64d384d7, f58c090a, a4655692, 09d79233, 26161710, 3e824226, 8ee13937, a9586192, 07c8d0a8, 69816fb6, 53cf4009, c28fea5a, 06194f4c, 8b98f7be, a1cc66ec, 97689236, a22c181f, 00d9d442, fe686032, 33f0950c]
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at java.util.AbstractCollection.toString(AbstractCollection.java:461)
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 898f7565, 64d384d7, f58c090a, a4655692, 09d79233, 26161710, 3e824226, 8ee13937, a9586192, 07c8d0a8, 69816fb6, 3b9cae66, 53cf4009, c28fea5a, 06194f4c, 8b98f7be, a1cc66ec, 97689236, a22c181f, 00d9d442, fe686032, 33f0950c] at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at com.mine.core.list.SetDemo.lambda$main$0(SetDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
[b60dc930, 03d75955, 52dd48e9, 8c8339a7, b254d9ed, 4c8d3bfe, 898f7565, 64d384d7, f58c090a, a4655692, 09d79233, a698eb9f, 26161710, 3e824226, 8ee13937, a9586192, 07c8d0a8, 69816fb6, 3b9cae66, 53cf4009, c28fea5a, 06194f4c, 8b98f7be, a1cc66ec, 97689236, a22c181f, 00d9d442, fe686032, 33f0950c]
[b60dc930, 03d75955, 52dd48e9, 8c8339a7, b254d9ed, 4c8d3bfe, 898f7565, 64d384d7, f58c090a, a4655692, 09d79233, 26161710, 3e824226, 8ee13937, a9586192, 07c8d0a8, 69816fb6, 3b9cae66, 53cf4009, c28fea5a, 06194f4c, 8b98f7be, a1cc66ec, 97689236, a22c181f, 00d9d442, fe686032, 33f0950c]
[b60dc930, 03d75955, 52dd48e9, b254d9ed, 4c8d3bfe, 898f7565, 64d384d7, f58c090a, a4655692, 09d79233, 26161710, 3e824226, 8ee13937, a9586192, 07c8d0a8, 69816fb6, 3b9cae66, 53cf4009, c28fea5a, 06194f4c, 8b98f7be, a1cc66ec, 97689236, a22c181f, 00d9d442, fe686032, 33f0950c]
同样会出现java.util.ConcurrentModificationException异常
解决方法:
- 1.Collections.synchronizedSet(new HashSet<>());
- 2.new CopyOnWriteArraySet<>();
CopyOnWriteArraySet源码:同CopyOnWriteArrayList
/**
* Creates an empty set.
*/
public CopyOnWriteArraySet() {
al = new CopyOnWriteArrayList<E>();
}
3.HashMap并发更新导致的异常
多线程情况下示例代码:
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
for (int i = 0; i < 30; i++) {
new Thread(() -> {
map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0, 8));
System.out.println(map);
}).start();
}
}
}
运行结果:
{Thread-0=b52efed1}
Exception in thread "Thread-10" java.util.ConcurrentModificationException
{Thread-3=bfbfd0db, Thread-11=5a9874fa, Thread-4=1231e961, Thread-10=b610b506, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-12=58404554, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-13=29e29920, Thread-12=58404554, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.util.AbstractMap.toString(AbstractMap.java:531)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at com.mine.core.list.MapDemo.lambda$main$0(MapDemo.java:15)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.lang.Thread.run(Thread.java:745)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
Exception in thread "Thread-26" java.util.ConcurrentModificationException
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
{Thread-22=66695fe6, Thread-21=3cc88068, Thread-20=ab65f799, Thread-25=f9370615, Thread-24=fa215a7f, Thread-23=9700b11d, Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-19=ae3d97e7, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
{Thread-22=66695fe6, Thread-21=3cc88068, Thread-20=ab65f799, Thread-26=15017c7e, Thread-25=f9370615, Thread-24=fa215a7f, Thread-23=9700b11d, Thread-28=8f5a2e9d, Thread-27=440dc328, Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-19=ae3d97e7, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
{Thread-22=66695fe6, Thread-21=3cc88068, Thread-20=ab65f799, Thread-26=15017c7e, Thread-25=f9370615, Thread-24=fa215a7f, Thread-23=9700b11d, Thread-29=91809c84, Thread-28=8f5a2e9d, Thread-27=440dc328, Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-19=ae3d97e7, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029} at java.util.AbstractMap.toString(AbstractMap.java:531)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at com.mine.core.list.MapDemo.lambda$main$0(MapDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-22" Exception in thread "Thread-23" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-0=b52efed1, Thread-2=c121f029} at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
at java.util.AbstractMap.toString(AbstractMap.java:531)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at com.mine.core.list.MapDemo.lambda$main$0(MapDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-19" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
at java.util.AbstractMap.toString(AbstractMap.java:531)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at com.mine.core.list.MapDemo.lambda$main$0(MapDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
at java.util.AbstractMap.toString(AbstractMap.java:531)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at com.mine.core.list.MapDemo.lambda$main$0(MapDemo.java:15)
at java.lang.Thread.run(Thread.java:745)
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-0=b52efed1, Thread-2=c121f029}
{Thread-3=bfbfd0db, Thread-0=b52efed1, Thread-2=c121f029}
{Thread-3=bfbfd0db, Thread-0=b52efed1}
{Thread-22=66695fe6, Thread-21=3cc88068, Thread-20=ab65f799, Thread-26=15017c7e, Thread-25=f9370615, Thread-24=fa215a7f, Thread-23=9700b11d, Thread-27=440dc328, Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-19=ae3d97e7, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
{Thread-22=66695fe6, Thread-21=3cc88068, Thread-20=ab65f799, Thread-24=fa215a7f, Thread-23=9700b11d, Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-19=ae3d97e7, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-21=3cc88068, Thread-20=ab65f799, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-23=9700b11d, Thread-19=ae3d97e7, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-20=ab65f799, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-19=ae3d97e7, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-15=d7418c65, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-18=97614664, Thread-17=ce43f899, Thread-16=cd7d07ee, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
{Thread-3=bfbfd0db, Thread-4=1231e961, Thread-5=88b1e48f, Thread-6=ceb7d541, Thread-7=a054a6df, Thread-8=da9551f4, Thread-9=b991cd45, Thread-11=5a9874fa, Thread-10=b610b506, Thread-14=9c025150, Thread-13=29e29920, Thread-12=58404554, Thread-0=b52efed1, Thread-1=cb6ddbd6, Thread-2=c121f029}
同样会出现java.util.ConcurrentModificationException异常
解决方法:
- 1.Collections.synchronizedMap(new HashMap<>());//同步方法,并发性不高
- 2.HashTable;//同步方法,并发性不高
- 3.new ConcurrentHashMap<>();//分段锁,并发性高