当前位置:网站首页>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
边栏推荐
- Sha6 of D to large integer
- Project training of Shandong University rendering engine system (V)
- pbootcms的if判断失效直接显示标签怎么回事?
- Interview: difference between '= =' and equals()
- Statistical machine learning code set
- Glove word embedding (IMDb film review emotion prediction project practice)
- calibration of sth
- Overview of webrtc's audio network Countermeasures
- Interview: do you understand the packing and unpacking operations?
- MySQL面试整理
猜你喜欢
Super detailed dry goods! Docker+pxc+haproxy build a MySQL Cluster with high availability and strong consistency
Collect | 22 short videos to learn Adobe Illustrator paper graphic editing and typesetting
MySQL面试整理
【研究】英文论文阅读——英语poor的研究人员的福利
有哪些特容易考上的院校?
Anyone who watches "Meng Hua Lu" should try this Tiktok effect
acwing 800. Target and of array elements
canvas 高级功能(下)
Batch --04--- moving components
双写一致性问题
随机推荐
ISCC-2022 部分wp
The process of "unmanned aquaculture"
Interview: what are shallow copy and deep copy?
The C Programming Language(第 2 版) 笔记 / 7 输入与输出 / 7.8 其它函数
Project training of Shandong University rendering engine system (V)
[DSP video tutorial] DSP video tutorial Issue 8: performance comparison of DSP library trigonometric function, C library trigonometric function and hardware trigonometric function, and accuracy compar
Acwing 1927 automatic completion (knowledge points: hash, bisection, sorting)
acwing795 前缀和(一维)
Differences between SQL and NoSQL of mongodb series
Batch --04--- moving components
Postgresql源码(53)plpgsql语法解析关键流程、函数分析
Nacos Config 动态刷新源码剖析
《安富莱嵌入式周报》第268期:2022.05.30--2022.06.05
【摸鱼神器】UI库秒变LowCode工具——列表篇(一)设计与实现
Leetcode 2194. Cells within a range in Excel table (yes, solved)
【DSP视频教程】DSP视频教程第8期:DSP库三角函数,C库三角函数和硬件三角函数的性能比较,以及与Matlab的精度比较(2022-06-04)
Project training of Shandong University rendering engine system (II)
使用 .NET 升级助手将NET Core 3.1项目升级为.NET 6
acwing 2816. Judgement subsequence
js监听用户是否打开屏幕焦点