java并发编程之:并发类容器

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

 

转载于:https://my.oschina.net/LucasZhu/blog/995837

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值