当前位置:网站首页>Contract awarding and AQS

Contract awarding and AQS

2022-06-12 16:34:00 -Implicit function-

Five common types of contract awarding

ConcurrentHashMap

CopyOnWriteArrayList

CopyOnWriteArraySet

ArrayBlockingQueue

LinkedBlockingQueue

Concurrent contracting is a thread - safe collection of concurrent threads

1、ConcurrentHashMap

(1) Thread safe HashMap The implementation of the

(2) data structure : A specified number of Segment Array , Every element in the array Segment Equivalent to one HashTable( One HashEntry[])

(3) For expansion , Just expand your own Segment Not the whole table Capacity expansion

(4)key And value Can't be null, and hashMap Sure

(5) towards map Additive elements

5.1) according to key obtain key.hashCode Of hash value

5.2) according to hash Value to calculate the... To be inserted Segment

5.3) according to hash Value and Segment Medium HashEntry The capacity of -1 Bitwise and get the... To be inserted HashEntry Of index

5.4) if HashEntry[index] Medium HashEntry The linked list has the same as the inserted element key and hash value , according to onlyIfAbsent Decide whether to replace the old value

5.5) If there is no same key and hash, Directly return to insert the new node into the chain head , The original head node is set as the new node next( In the same way as HashMap Agreement , All are HashEntry The method of replacement )

(6)ConcurrentHashMap be based on concurrencyLevel Divide into multiple Segment To store key-value, In this case put Only the current is locked Segment, You can avoid put Lock up the whole map, Thus, the blocking phenomenon during concurrency is reduced

from map Get the element

(7) according to key obtain key.hashCode Of hash value

7.1) according to hash Value and find the corresponding Segment

7.2) according to hash Value and Segment Medium HashEntry The capacity of -1 Bitwise and fetch HashEntry Of index

7.3) Traverse the whole HashEntry[index] Linked list , find hash and key Equal to a given parameter HashEntry, for example e

If not found e, return null

If you find e, obtain e.value

If e.value!=null, Go straight back to

If e.value==null, Then lock it first , Etc put Operation will value After setting successfully , Back again value value

(8) about get In terms of operation , Basically no lock , Only when you find e And e.value be equal to null, It may be the present one HashEntry Just created ,value Property has not been set successfully , At this time we read that it is time HashEntry Of value The default value of null, So lock here , wait for put After the end , return value value

(9) Lock condition ( Section lock ):

---put

---get Found in the hash And key Are the same as the specified parameters HashEntry, however value==null The situation of

---remove

---size(): After three attempts , Not yet , Traverse all of Segment, Lock them separately ( Namely establishment Global lock )

2、CopyOnWriteArrayList

Thread safe and unlocked during read operations ArrayList

The model adopted is "CopyOnWrite"( Write operation --> Include increase 、 Delete , Use copy to complete )

The underlying data structure is a Object[], The initial capacity is 0, After that, every additional element , Capacity +1, Copy the array

Traversal is only a copy of the global array , Even if the global array has been added, deleted or modified , The copy will not change , So there will be no concurrent exceptions . however , You may read some newly deleted objects during the traversal

Add, delete and change the lock 、 I can't read it

Read more and write less, and dirty data has little impact on concurrency , choice CopyOnWriteArrayList

3、CopyOnWriteArraySet

be based on CopyOnWriteArrayList, Don't add repeating elements

4、ArrayBlockingQueue

Based on the array 、 fifo 、 Thread safety , It can realize blocking read and write for a specified time , And the capacity can be limited

form : An array of objects +1 The lock ReentrantLock+2 Conditions Condition

Comparison of three types of team entry

offer(E e): If the queue is not full , Return immediately true; If the queue is full , Return immediately false--> Don't block

put(E e): If the queue is full , Keep blocking , Until the array is full or the thread is interrupted --> Blocking

offer(E e, long timeout, TimeUnit unit): Insert an element at the end of the team ,, If the array is full , Then enter wait , Until the following three situations occur :--> Blocking

# Awakened

# Waiting time out

# The current thread is interrupted

Three kinds of pairing comparison

poll(): If there is no element , Go straight back to null; If there are elements , Out of the team

take(): If the queue is empty , Keep blocking , Until the array is not empty or the thread is interrupted --> Blocking

poll(long timeout, TimeUnit unit): If the array is not empty , Out of the team ; If the array is empty and has timed out , return null; If the array is empty and the time does not time out , Then enter wait , Until the following three situations occur :

# Awakened

# Waiting time out

# The current thread is interrupted

It should be noted that , An array is an array that must have a specified length , In the whole process , The length of the array does not change , The head of the team has been moving backward with the operation of entering and leaving the team

There are two forms of locks: fair and unfair

In the case of only high concurrency in the team or high concurrency out of the team , Because of manipulating arrays , And no expansion is required , A high performance

5、LinkedBlockingQueue

Based on linked list , Use a lock for reading and writing , In high concurrency There are many read and write operations Under the circumstances , Better performance than the ArrayBlockingQueue

Make a list + Two locks + Two conditions

The default capacity is an integer maximum , It can be seen that there is no capacity limit

The three types of team entry and three types of team exit are exactly the same as those above , Just because LinkedBlockingQueue The capacity of is infinite , In the process of joining the team , No blocking waiting


2、AQS

 

AQS Is a two-way linked list queue , Memory space is not continuous ,

for instance :

        Now? ConcurrentHashMap The collection has two threads , There is no problem when two threads operate on different array indexes , When two threads operate on the same index , There will be synchronization problems , At this time, we need to use AQS To solve the

        The solution is to add the optimistic lock and the fair lock , Add the heavyweight lock

In the first place first head This thread , hold state Change to 1, And then execute , After that , The second thread is coming , When a lock is found, it is blocked to enter the queue for spin , After the third one comes back, he will stop spinning and wait , This is becoming a heavyweight lock

原网站

版权声明
本文为[-Implicit function-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121626337514.html