Synchronous Class Containers and Concurrent Class Containers

Synchronized class container:

Synchronized class containers are thread-safe, but in some scenarios, locks are required to protect conforming operations, such as: iteration (repeatedly access elements, traverse all elements in the container), jump (find according to the specified order) The next element of the current element), and conditional operations. These conforming operations may exhibit unexpected behaviors when multi-threaded concurrently modifying the container. The most classic is ConcurrentModificationException. The reason is that when the container is iterated, the concurrent Modified the content, because the early iterators were not designed with concurrent modification in mind.

同步类容器:如古老的Vector,HashTable,这些容器的同步功能其实都是jdkCollections.synchronized***等方法去实现的.其底层无非就是用
synchronized关键字对每一个共用的方法进行同步,使得每次只能有一个线程访问容器的状态.这很明显不满足我们今天互联网高并发的需求,在保证线程安全的同时,
也必须要有足够好的性能

Concurrent class container:

After jdk5.0, a variety of concurrent containers are provided to replace synchronization containers to improve performance. The state of synchronization containers is serialized. Although they achieve thread safety, they seriously reduce concurrent containers. In a multi-threaded environment , severely reduces the throughput of the application

Concurrent containers are specially designed for concurrency. ConcurrentHashMap is used to replace the traditional HashTable for hashing, and in ConcurrentHashMap, support for some common composite operations is added, and CopyOnWriteArrayList is used instead of Vector, concurrent CopyonWriteArraySet, and concurrent Queue , ConcurrentLinkedQueue and LinkedQueue. The former is a high-performance queue, and the latter is a blocking queue. There are many specific implementations of Queue, such as ArrayBlockingQueue.PriorityBlockingQueue, SynchronousQueue, etc.

ConcurrentMap:

The ConcurrentHashMap interface has two important implementations: ConcurrentHashMap, ConcurrentSkipListMap (support concurrent sorting function, make up for ConcurrentHashMap)

ConcurrentHashMap内部使用段(Segment)来表示这些不同的部分,每个段其实就是一个小的HashTable,他们有自己的锁.只要多个修改操作发生在不同的段上,
他们就可以并发进行,把每一个整体分成16段(Segment),也就是最高支持16个线程的并发修改操作,这也是在多线程场景时,减小锁的粒度从而降低锁竞争的一种方案,
并且代码中大多共享变量使用volatile关键字声明,目的是第一时间获取修改的内容,性能非常好.

The design idea is to reduce the granularity of locks to reduce the problem of multi-threaded lock competition and improve concurrent performance.

You can understand the underlying implementation principle of ConcurrentHashMap

Conpy-On-Write container:

The Copy-On-Write container, referred to as COW, is an optimization strategy used in programming. There are two types of COW containers in JDK: CopyOnWriteArrayList and CopyOnWriteArraySet. COW containers are very useful and can be used in many concurrent scenarios.

CopyOnWrite容器即写时复制容器,通俗的理解是当我们往一个容器添加元素的的时候,不直接往当前容器添加,而是现将容器进行Copy,复制出一个新容器,
然后往新的容器里添加元素,添加完元素之后,在将原容器的引用指向新的容器,这样做的好处是我们可以对CopyOnWrite容器进行并发的读,而不需要加锁,因为
当前容器不会添加新的内容,所以CopyOnWrite容器也是一种读写分离的思想读和写不同的容器.

The Copy-On-Write container is best used in an environment with more reads and fewer writes. Every time you write, you need to copy the source element (this will consume resources)

Read-write lock: read-read sharing, write-write mutual exclusion

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325762530&siteId=291194637