concurrenthashmap

java并发:同步容器&并发容器

独自空忆成欢 提交于 2019-11-27 12:39:13
第一节 同步容器、并发容器 1.简述同步容器与并发容器   在Java并发编程中,经常听到同步容器、并发容器之说,那什么是同步容器与并发容器呢?同步容器可以简单地理解为通过synchronized来实现同步的容器,比如Vector、Hashtable以及SynchronizedList等容器,如果有多个线程调用同步容器的方法,它们将会串行执行。   可以通过查看Vector、Hashtable等同步容器的实现代码,可以看到这些容器实现线程安全的方式就是将它们的状态封装起来,并在需要同步的方法上加上关键字synchronized,但在某些情况下,同步容器不一定就是线程安全的,比如获取最后一个元素或者删除最后一个元素,我们需要实现额外的同步操作: public static Object getLast(Vector list) { int lastIndex = list.size() - 1; return list.get(lastIndex); } public static void deleteLast(Vector list) { int lastIndex = list.size() - 1; list.remove(lastIndex); }   虽然上面的方法看起来没有问题,Vector自身的方法也是同步的,但是在多线程环境中还是隐藏着问题。如果有两个线程A

ConcurrentHashMap: avoid extra object creation with “putIfAbsent”?

让人想犯罪 __ 提交于 2019-11-27 09:45:33
问题 I am aggregating multiple values for keys in a multi-threaded environment. The keys are not known in advance. I thought I would do something like this: class Aggregator { protected ConcurrentHashMap<String, List<String>> entries = new ConcurrentHashMap<String, List<String>>(); public Aggregator() {} public void record(String key, String value) { List<String> newList = Collections.synchronizedList(new ArrayList<String>()); List<String> existingList = entries.putIfAbsent(key, newList); List

golang struct concurrent read and write without Lock is also running ok?

孤者浪人 提交于 2019-11-27 07:11:32
问题 concurrentMap() function Have WARNING: DATA RACE , And fatal error : concurrent map read and map write concurrentStruct() have WARNING: DATA RACE, But running ok why the struct can DATA RACE? package main import ( "sync" ) func main() { // concurrentMap() concurrentStruct() // concurrentStructWithMuLock() } type Metadata struct { mu sync.RWMutex // 🔐 key bool } // concurrentStruct 并发操作结构体 // concurrent read and write the struct // go run -race main.go 有 WARNING: DATA RACE,但是可以运行 // go run

ConcurrentHashMap in Java?

六月ゝ 毕业季﹏ 提交于 2019-11-27 06:06:45
What is the use of ConcurrentHashMap in Java? What are its benefits? How does it work? Sample code would be useful too. The point is to provide an implementation of HashMap that is threadsafe. Multiple threads can read from and write to it without the chance of receiving out-of-date or corrupted data. ConcurrentHashMap provides its own synchronization, so you do not have to synchronize accesses to it explicitly. Another feature of ConcurrentHashMap is that it provides the putIfAbsent method, which will atomically add a mapping if the specified key does not exist. Consider the following code:

集合

谁说我不能喝 提交于 2019-11-27 04:56:05
List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口 Set下有HashSet,LinkedHashSet,TreeSet List下有ArrayList,Vector,LinkedList Map下有Hashtable,LinkedHashMap,HashMap,TreeMap Connection 接口 List — List 有序,可重复 ArrayList 优点: 底层数据结构是 数组 , 查询快 ,增删慢。 缺点: 线程不安全 ,效率高 Vector 优点: 底层数据结构是 数组 , 查询快 ,增删慢。 缺点: 线程安全 ,效率低 LinkedList 优点: 底层数据结构是 链表 ,查询慢, 增删快 。 缺点: 线程不安全 ,效率高 Set —Set 无序,唯一 HashSet 底层数据结构是 哈希表 。(无序,唯一) 如何来保证元素唯一性? 1.依赖两个方法:hashCode()和equals() LinkedHashSet 底层数据结构是 链表和哈希表 。(FIFO插入有序,唯一) 1.由链表保证元素有序 2.由哈希表保证元素唯一 TreeSet 底层数据结构是 红黑树 。( 唯一,有序 ) 1. 如何保证元素排序的呢? 自然排序 比较器排序 2.如何保证元素唯一性的呢? 根据比较的返回值是否是0来决定

Java集合类/容器类(下)

陌路散爱 提交于 2019-11-27 04:42:54
接着上篇文章给出的Java在JDK1.2之后在java.util包中给出的集合类/容器类以及数组的应用整理。也考虑和讨论了并发一致性的问题。 其实在高并发的情况下,仅仅靠java.util包中提供的最基本的容器往往不能满足具体应用场景的需求。 在JavaSE5之后,也就是JDK1.5出来之后,在java.util.concurrent包中又提供了很多并发场景下方便使用的容器/集合工具类。这其中包括 CopyOnWriteArrayList 、 CopyOnWriteArraySet 、 ConcurrentHashMap 、 ConcurrentLinkedQueue 、 BlockingQueue 等。 在CopyOnWriteArrayList中为了保证数据一致性,每当有新的改变的时候,会利用写时拷贝(Copy On Write),复制出一份新的List出来。而已经使用旧的拷贝的引用的,还会继续使用旧的。 同时为了保证并发情况下的执行效率,像ConcurrentHashMap中还用到了Segment进行分段,减小了锁粒度,提高了并发性。 BlockingQueue及其具体的各种实现类,则保证了队列为空时消费者的阻塞和队列为满时生产者的阻塞,在“生产者-消费者”模式下得到了广泛的应用。 ConcurrentLinkedQueue是旨在大并发量情况下,维持队列的基本功能

“Undefined reference: .. ConcurrentHashMap.keySet()” when building in Java 8

☆樱花仙子☆ 提交于 2019-11-27 01:53:15
问题 i have a project, and i am build this project with jdk 6,7,8 and my target is 1.6 when i build jdk 8 i get this error: Undefined reference: java.util.concurrent.ConcurrentHashMap.KeySetView java.util.concurrent.ConcurrentHashMap.keySet() since i have this code in that line: final Iterator<CLASS_NAME> itr = hashMap.keySet().iterator(); how can avoid error, i made some search in internet, and since java 8 changed its return type keyset, i got error. is this any solution. i am using maven, and

ConcurrentHashMap, which concurrent features improved in JDK8

大憨熊 提交于 2019-11-27 01:41:50
问题 Can any concurrent expert explain in ConcurrentHashMap, which concurrent features improved comparing with which in previous JDKs 回答1: Well, the ConcurrentHashMap has been entirely rewritten. Before Java 8, each ConcurrentHashMap had a “concurrency level” which was fixed at construction time. For compatibility reasons, there is still a constructor accepting such a level though not using it in the original way. The map was split into as many segments, as its concurrency level, each of them

Is ConcurrentHashMap.get() guaranteed to see a previous ConcurrentHashMap.put() by different thread?

帅比萌擦擦* 提交于 2019-11-27 01:33:13
问题 Is ConcurrentHashMap.get() guaranteed to see a previous ConcurrentHashMap.put() by different thread? My expectation is that is is, and reading the JavaDocs seems to indicate so, but I am 99% convinced that reality is different. On my production server the below seems to be happening. (I've caught it with logging.) Pseudo code example: static final ConcurrentHashMap map = new ConcurrentHashMap(); //sharedLock is key specific. One map, many keys. There is a 1:1 // relationship between key and