Chapter 10 Summary of five types of concurrent packages and chapter 10 five types of packages
1. Five common concurrent packages
- ConcurrentHashMap
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- ArrayBlockingQueue
- LinkedBlockingQueue
2. ConcurrentHashMap
- Implementation of thread-safe HashMap
- Data structure: a specified number of Segment arrays. The Segment of each element in the array is equivalent to a HashTable (A HashEntry []).
- To resize a table, you only need to expand your own Segment instead of the whole table.
- Neither key nor value can be null, while hashMap can
- Add element to map
- Obtain the hash value of key. hashCode based on the key.
- Calculate the Segment to be inserted based on the hash value
- Based on the hash value and the capacity of the HashEntry in the Segment-1 bitwise and obtain the index of the HashEntry TO BE INSERTED
- If the HashEntry linked list in HashEntry [index] has the same key and hash value as the inserted element, determine whether to replace the old value based on onlyIfAbsent.
- If there is no same key and hash, the new node is directly inserted into the chain header, and the original header node is set as the next node (the HashMap method is the same as that of the HashEntry method)
- ConcurrentHashMap divides multiple segments based on concurrencyLevel to store the key-value. In this way, only the current Segment is locked during put, so that the entire map is not locked during put, this reduces the blocking of concurrency.
- Retrieve element from map
- Obtain the hash value of key. hashCode based on the key.
- Find the corresponding Segment based on the hash value
- Based on the hash value and the capacity of the HashEntry in the Segment-1 bitwise and obtain the index of the HashEntry
- Traverse the entire HashEntry [index] linked list to find the HashEntry with the same hash and key as the given parameter, such as e
- If no e is found, null is returned.
- For example, find e and obtain e. value.
- If e. value! = Null, return directly
- If e. value = null, the lock is applied first, and the value is returned after the concurrent put operation sets the value successfully.
- For get operations, there is basically no lock, only when e is found and e. value is equal to null. It may be that the current HashEntry has just been created and the value attribute has not been set successfully. At this time, we read that the default value of the HashEntry is null, so the lock is applied here, returns the value after the put operation is complete.
- Locking status(Segment lock ):
- Put
- Get finds the HashEntry with the same hash and key as the specified parameter, but the value = null
- Remove
- Size (): After three attempts, the system has not succeeded yet. traverse all segments and lock them separately (that is, createGlobal lock)
3. CopyOnWriteArrayList
- ArrayList with thread security and no lock during read Operations
- The adopted mode is "CopyOnWrite" (that is, the write operation --> includes adding, deleting, and copying)
- The underlying data structure is an Object [] with an initial capacity of 0. Each time an element is added, the size is + 1, and the array is copied once.
- Only a copy of the global array is traversed. Even if the global array is added, deleted, and changed, the copy will not change, so no concurrency exception will occur. However, some objects that have just been deleted may be read during traversal.
- Add, delete, modify, and lock; read locks
- Select CopyOnWriteArrayList
4. CopyOnWriteArraySet
- Based on CopyOnWriteArrayList, duplicate elements are not added
5. ArrayBlockingQueue
- Based on arrays, first-in-first-out, and thread security, block read/write operations can be implemented at a specified time, and the capacity can be limited.
- Composition: an object array + 1 lock ReentrantLock + 2 Condition Conditions
- Comparison of three teams
- Offer (E): If the queue is not full, true is returned immediately; if the queue is full, false is returned immediately --> not blocked
- Put (E): If the queue is full, it is blocked until the array is full or the thread is interrupted. --> Blocking
- Offer (E e, long timeout, TimeUnit): insert an element at the end of the team. If the array is full, wait until the following three conditions occur: --> Blocking
- Awakened
- Wait time timeout
- The current thread is interrupted.
- Comparison between three types
- Poll (): If no element exists, null is directly returned. If an element exists
- Take (): If the queue is empty, it will be blocked until the array is not empty or the thread is interrupted --> Blocking
- Poll (long timeout, TimeUnit unit, until the following three situations occur:
- Awakened
- Wait time timeout
- The current thread is interrupted.
- It should be noted that the array is an array with a length that must be specified. during the whole process, the length of the array remains unchanged, and the head of the team keeps moving after the inbound and outbound operations.
- There are two lock forms: Fairness and non-fairness.
- In the case of high concurrency or high concurrency, the performance is very high because the operation array does not need to be resized.
6. Define blockingqueue
- Based on the linked list implementation, read and write each use a lock, high concurrencyMultiple read/write operationsThe performance is better than that of ArrayBlockingQueue.
- Form a linked list + two locks + two conditions
- The default capacity is the maximum integer value. It can be seen that there is no capacity limit.
- The three teams and the three teams are exactly the same as those above, but because the capacity of the LinkedBlockingQueue is unlimited, the waiting may not be blocked during the process.