背景:
在网络程序中经常遇到多个线程同时去操作一个集合Set,List.Map,Queue。Hashtable,Vector 在迭代集合的时候如何避免java.util.ConcurrentModificationException的发生。
以Hashtable为例,api中不是说Hashtable是同步的吗?
答:通过源码查看Hashtable中有一个keySet内部类,keySet对象又是通过Collections.synchronizedSet生成的,而Collections.synchronizedSet中并没有对iterator添加同步关键字。
SynchronizedCollection<E>类的iterator源码
public Iterator<E> iterator() {
return c.iterator(); // Must be manually synched by user!
}
Hashtable类同样也不会对iterator进行同步处理,只有数据修改才加同步关联字。
替代过程中删除元素:
方法:迭代过程中删除元素使用迭代器的remove方法,而不是foreach
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) throws InterruptedException {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
// error
try {
for (String str : list) {
list.remove(str);
System.out.println(str);
}
} catch (Exception e) {
System.err.println(e);
Thread.sleep(100);
}
System.err.println("------------------------------");
// success
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
String str = iterator.next();
System.out.println(str);
iterator.remove();
}
System.out.println("size = " + list.size());
}
}
多线程并发一个线程添加元素,另一个线程迭代元素:
方法1:多线程并发操作在迭代的时候获取集合对象的锁,使其它线程访问带synchronized的方法时堵塞。
package test;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
private static Map<String, String> map = new Hashtable<String, String>();
public static void main(String[] args) {
// Collections.syn(list)
map.put("a", "a");
map.put("b", "b");
map.put("c", "c");
Thread th1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(300);
map.put(String.valueOf(i), String.valueOf(i));
System.out.println("map put " + i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized (map) {
for (Entry<String, String> tmp : map.entrySet()) {
try {
Thread.sleep(1000);
System.out.println("iterator key " + tmp.getKey());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
附:HashMap 可通过Collections.synchronizedMap实现同步
方法3:使用java.util.concurrent.ConcurrentHashMap
package test;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap;
public class Test {
private static Map<String, String> map = new ConcurrentSkipListMap <String, String>();
public static void main(String[] args) {
// Collections.syn(list)
map.put("a", "a");
map.put("b", "b");
map.put("c", "c");
Thread th1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(400);
map.put(String.valueOf(i), String.valueOf(i));
System.out.println("map put " + i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
th1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (Entry<String, String> tmp : map.entrySet()) {
try {
System.out.println("iterator key " + tmp.getKey());
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
总结:对Hashtable 或HashMap在迭代的时候添加加 synchronized会造成其它线程的堵塞,ConcurrentHashMap时并不会迭代时不会堵塞其它线程。效率更高。
ConcurrentHashMap专门用于高并发大数据线程安全的类,高并发、大数据写入时性能优说Hashtable与Collections.synchronized(),而且他们的锁方式也不一样(synchronized,lock)。