Java (JUC) synchronous container concurrent container Daquan

What is a synchronization container?

同步容器synchronizedModifying the container with keywords ensures that only one thread is using the container at a time, thereby making the container thread safe. Synchronized means synchronized, it is reflected in the 多线程变为串行waiting to be executed. (But note that compound operations do not guarantee thread safety. For example: A thread acquires the tail node in the first step, and the value of the tail node is increased by 1 in the second step, but after A thread executes the first step, B thread delete If the tail node is reached, the A thread will report a null pointer when performing the second step)

What is a concurrent container?

并发容器Refers to permission 多线程同时使用容器, and to ensure thread safety. In order to achieve maximize concurrency, Java Concurrency Kit uses a variety of ways to improve the efficiency optimization concurrent containers, the core is: , CAS(无锁), COW(读写分离), 分段锁.

Synchronize container sorting

1. Vector

Vector implements the List interface just like ArrayList, and its various operations on arrays are the same as ArrayList. The difference is that all methods of Vertor that may be thread-unsafe are modified with synchronized.

2. Stack

Stack is a subclass of Vertor, and Stack implements the advanced stack. Synchronized modifications have been performed on operations such as stack push and pop.

3. HashTable

HashTable implements the Map interface, and the functions it implements are basically the same (HashTable cannot store null, and both HashMap keys and values ​​can store null). The difference is that HashTable uses synchronized modified methods.

4. Synchronized collection classes provided by Collections

List list = Collections.synchronizedList (new ArrayList ());
Set set = Collections.synchronizedSet (new HashSet ());
Map map = Collections.synchronizedMap (new HashMap ());
In fact, the above three containers are Collections through the proxy mode Synchronized synchronization is added to the original operation. The synchronized granularity is too large, resulting in low efficiency in multi-threaded processing. Therefore, in JDK1.5, the concurrent container under the concurrent package was introduced to deal with the problem of low processing efficiency of the container under multithreading.

Concurrent container sorting

1. CopyOnWriteArrayList

CopyOnWriteArrayList is equivalent to an ArrayList that implements thread safety. Its mechanism is to copy out a copy array when there is a write operation to the container, and assign the copy array reference to the container after the operation is completed. The bottom layer uses ReentrantLock to ensure synchronization. But it sacrifices the consistency of the container in exchange for the high concurrency efficiency of the container (the old data read during the copy). Therefore, it cannot be used in scenarios that require strong consistency.

2. CopyOnWriteArraySet

CopyOnWriteArraySet has the same principle as CopyOnWriteArrayList, it is a Set collection that implements the CopyOnWrite mechanism.

3. ConcurrentHashMap

ConcurrentHashMap is equivalent to HashMap that implements thread safety. The keys are unordered, and neither key nor value can be null. Prior to JDK8, ConcurrentHashMap used a segmented lock mechanism to improve concurrency efficiency, and only needed to lock when operating on key-value pairs of the same segment. After reaching JDK8, the lock segmentation mechanism was abandoned and the CAS algorithm was used instead.

4. ConcurrentSkipListMap

ConcurrentSkipListMap is equivalent to a TreeMap that implements thread safety. The keys are ordered, and neither key nor value can be null. It uses a skip table mechanism to replace red and black trees. Why not continue to use red and black trees? Because the red-black tree needs to be rotated and adjusted when inserting or deleting nodes, the granularity that needs to be controlled is larger. The jump table uses a linked list, using a lock-free CAS mechanism to achieve high concurrent thread safety.

5. ConcurrentSkipListSet

ConcurrentSkipListSet has the same principle as ConcurrentSkipListMap, it is a TreeSet that achieves high concurrent thread safety.

Queue type

Blocking type

1. ArrayBlockingQueue

ArrayBlockingQueue is a bounded blocking thread-safe queue implemented with an array. If you continue to stuff elements into the full queue, it will cause the current thread to block. If you get an element from an empty queue, it will cause the current thread to block. Use ReentrantLock to ensure thread safety in concurrent situations.

2. LinkedBlockingQueue

LinkedBlockingQueue is a one-way linked list based on arbitrary (in fact bounded) FIFO blocking queue. Access and removal operations are performed at the head of the team, addition operations are performed at the end of the team, and different locks are used for protection. Only two operations can be locked at the same time for operations that may involve multiple nodes.

3. PriorityBlockingQueue

PriorityBlockingQueue is an unbounded blocking queue that supports priority. By default, the elements are arranged in ascending order in natural order. You can also implement the compareTo () method of the custom class to specify the element sorting rules,

4. DelayQueue

DelayQueue is an unbounded blocking queue implemented internally using priority queues. At the same time, how long the element node data needs to wait before it can be accessed. When the data fetch queue is empty, wait, there is data, but the wait time is overtime when the delay time is not reached.

5. SynchronousQueue

SynchronousQueue has no capacity. It is a blocking queue that does not store elements. It will directly deliver the elements to consumers. It must wait for the added elements in the queue to be consumed before it can continue to add new elements. Equivalent to a conveyor belt with a capacity of 1.

6. LinkedTransferQueue

LinkedTransferQueue is an unbounded transmission blocking queue composed of linked lists. It combines the advantages of ConcurrentLinkedQueue, SynchronousQueue, LinkedBlockingQueue and so on. The specific mechanism is more complicated.

7. LinkedBlockingDeque

LinkedBlockingDeque is a bidirectional blocking queue composed of a linked list structure. The so-called two-way queue refers to that elements can be inserted and removed from both ends of the queue.

Non-blocking

1. ConcurrentLinkedQueue

ConcurrentLinkedQueue is a thread-safe unbounded non-blocking queue. Its underlying data structure is implemented using a singly linked list. The enqueue and dequeue operations use the CAS we often mentioned to ensure thread safety.

Published 248 original articles · praised 416 · 90,000 views +

Guess you like

Origin blog.csdn.net/qq_33709508/article/details/105447391