jdk5.0之后提供了很多种并发类容器来代替同步类容器从而改善性能。同步类容器的状态都是串行化的,他们虽然实现了线程安全,但是严重降低了并发性,在多线程环境时,严重降低了应用程序的吞度量。
并发类容器是专门针对并发设计的,使用CuncurrentHashMap来代替基于三列的传统的HashTable,而且在CuncurrentHashMap中,添加了一些常见符合操作的支持,以及使用了CopyOnWriteArrayList 代替Voctor,并发的CopyonWriteArraySet,以及并发的Queue , ConcurrentLinkedQueue 和LinkedBlockingQueue,前者是高性能的队列,后者是以阻塞式的队列,具体实现Queue还有很多,例如ArrayBlockingQueue , PriorityBlockingQueue , SynchronousQueue等。
同步类容器不能同时进行复合性 操作如:
public static void main(String[] args) {
//:避免线程同步可采用Vector替代ArrayList HashTable替代HashMap
final Vector<String> tickets = new Vector<String>();
//Map<String, String> map = Collections.synchronizedMap(new HashMap<String, String>());
for(int i = 1; i<= 1000; i++){
tickets.add("火车"+i);
}
for (Iterator iterator = tickets.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
tickets.remove(20);
}
}
如上,会出现 CuncurrentModificationException异常
Exception in thread "main" java.util.ConcurrentModificationException