An interesting question came up on the jsr166 concurrency interest mailing list recently which I felt was worthy of mention. Why is there no ConcurrentHashSet equivalent of the ConcurrentHashMap data structure and how does one achieve the same concurrency and performance characteristics of the latter while maintaining the uniqueness semantics of the former? Currently there exist a few different ways of making a Set concurrent.
- CopyOnWriteArraySet
- ConcurrentSkipListSet
- Collections.synchronizedSet(Set<T> s)
However none of these exhibit the lock striped minimal blocking high concurrency characteristics of a ConcurrentHashMap. In actual fact all Set implementations with the exception of EnumSet are little more than wrappers around a second kind of backing implementation. Here are all Set implementations and their corresponding backing implementations in brackets.
- HashSet (HashMap)
- TreeSet (TreeMap)
- LinkedHashSet (LinkedHashMap)
- CopyOnWriteArraySet (CopyOnWriteArrayList)
- ConcurrentSkipListSet (ConcurrentSkipListMap)
- JobStateReasons (HashMap)
Following on from that, for those map implementations for which there aren’t already Set equivalents, the JDK from version 1.6 onwards provides a wayfor you to create a set with your own choice of backing map implementation. For example one can create a ConcurrentHashSet or a WeakHashSet simply by doing the following.
Collections.newSetFromMap(new ConcurrentHashMap<Object,Boolean>()) Collections.newSetFromMap(new WeakHashMap<Object, Boolean>())
With this knowledge some may opt to use the underlying map implementations directly by themselves and this is a trade off between a solely theoretical performance optimisation and making your choice of collection a semantically correct one.
相关推荐
- **标准库**:Java并发工具包(java.util.concurrent)提供了丰富的并发工具类。 - **第三方库**:例如Apache Commons Concurrency等。 ##### 2. 构建库 - **自定义并发组件**:根据项目需求开发特定的并发工具。 ...
Java的并发库提供了一系列工具和API,如`java.util.concurrent`包,帮助开发者有效地管理并发任务。本书主要涵盖以下几个方面: 1. **线程基础**:书中首先介绍了线程的基本概念,包括如何创建和管理线程,以及线程...
concurrent programming in java design principles and patterns .chm
《Doug Lea, Concurrent Programming in Java Design Principles and Patterns》是一本深入探讨Java并发编程的经典著作,由Doug Lea撰写。这本书对于理解Java平台上的多线程编程和并发设计模式至关重要,是许多Java...
《Java并发编程》一书是由著名并发编程专家Doug Lea所著,他同时也是Java并发包(JUC)的作者,这本书详细介绍了Java多线程编程的基础概念和高级技术。 首先,书中提到了并发编程的基本概念,包括并发模型、设计力...
本书《Concurrent Programming in Java™: Design Principles and Patterns 2nd》由Doug Lea编写,出版于1999年,是关于Java并发编程的一本权威指南。Java平台因其强大的线程支持能力而备受青睐,这使得Java程序员...
例如,使用无锁数据结构或原子操作(`java.util.concurrent.atomic`包)。 3. **避免死锁、活锁和饥饿**:理解并预防这些并发问题至关重要。死锁发生在两个或多个线程相互等待对方释放资源导致僵局;活锁是线程不断...
Concurrent Programming in Java™: Design Principles and Patterns, Second Edition. 介绍并发编程的好的著作,著名的并发大师 Doug Lea的杰作。
《Java并发编程实践》第二版是一本专注于Java编程语言中并发程序设计的著作,由Doug Lea撰写,Addison Wesley出版社于1999年10月出版。这本书旨在为那些熟悉面向对象编程但对并发编程了解不多的开发者提供指导,同时...
Java Concurrent in practice (animated)
最后,"Addison_Wesley_-_Concurrent_Programming_in_Java_2nd_Ed_(1999).pdf"是《并发编程在Java》的第二版,由Doug Lea撰写。这本书是Java并发编程领域的另一部里程碑作品,它在Java 1.4时代就深入介绍了线程、...
Java提供了多种机制来保证线程安全,如`synchronized`关键字、`volatile`变量以及`java.util.concurrent`包中的高级并发工具类。 2. **死锁**:当两个或更多的线程在执行过程中,每个线程都在等待另一个线程释放...
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
Concurrent Programming in Java Design Principles and Pattern英文版 2.48M Java并发编程设计原则与模式_第二版(原书中文版) 19.4M Concurrent_Programming_in_Java_Design_Principles_Lecture DougLea
"java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError" 是一个典型的错误提示,它表明在并发执行过程中遇到了内存不足的问题。下面我们将深入探讨这个问题的原因、影响以及如何解决。 内存溢出...
本书《Concurrent Programming in Java - Design Principles and Patterns (Second Edition)》深入探讨了在Java编程语言中进行并发编程的方法、设计理念及其实现技巧。它假设读者已经具备面向对象编程的经验,但对于...