Multithreaded series five: Concurrent tool classes and concurrent containers

Source: Internet
Author: User
Tags volatile

One, concurrent containers 1. Concurrenthashmap Why to use Concurrenthashmap

In a multithreaded environment, a put operation using hashmap causes a dead loop, resulting in a CPU utilization close to 100%,hashmap that causes a dead loop when a put operation is performed concurrently, because multithreading causes HashMap entry linked list

The formation of a circular data structure, once the formation of a circular data structure, entry next node is never empty, will produce a dead loop to get entry.

Hashtable containers use synchronized to ensure thread safety, but Hashtable is inefficient in the event of intense online competition. Because when a thread accesses the Hashtable synchronization method, other threads also access the Hashtable synchronization method, which goes into blocking or polling state. If thread 1 uses put for element addition, thread 2 cannot use the Put method to add elements or use the Get method to get elements, so the more intense the competition the less efficient.

Some useful methods of Concurrenthashmap

Many times we want to insert an element when the element does not exist, and we generally write the code as follows

Synchronized (map) {

if (map.get (key) = = null) {

Return Map.put (key, value);

} else{

return Map.get (key);

}

}

The Putifabsent (key,value) method atomically implements the same function

V putifabsent(K key, V value)

if Key corresponding to the value does not exist, then put go in, go back. NULL . Otherwise, no put, returns the already existing value.   

Boolean remove (object key, Object value)

if Key the corresponding value is value , the removal k-v , return true . Otherwise, it does not remove and returns false.

boolean replace (K key, v OldValue, v newvalue)

if Key the corresponding current value is OldValue , it is replaced by NewValue , return true . Otherwise, do not replace, return false.

The explanation of the hash

hashes , arbitrary-length inputs , are transformed into fixed-length outputs through an algorithm . A mapping that belongs to compression.

The hash Algorithm sample diagram demonstrates:

Similar to the implementation of HASPMAP is to use the hash, such as the 1000 elements into the length of 10 hashmap inside, before putting the 1000 numbers through the hash algorithm map into 10 arrays, this time there will be the same mapping values in an array of the same position, Will produce a hash collision, at this time HashMap will use the entry linked list after the collision array to store the same mapping value, and then use the Equals method to determine whether the same linked list stores the same value to get the value, the list is hashmap to solve the collision method, So we generally write a class to write their own hashcode method and the Equals method, if the key hashcode the same, and then use the Equals method of the key to determine the key content is not the same, the same gets the value

Md5,Sha, remainder are hash algorithms,concurrenthashmap is wang/jenkins algorithm

Implementation of Concurrenthashmap under 1.7

The design idea of segmented lock .

Example diagram of the idea of segmented locks:

Description

A) The traditional hashtable is a small space for the whole segment of the array locked, so that performance is relatively low

b) Concurrenthashmap is preceded by an array in a very small space array, mapped to the previous array, and then mapped to a small space in the back of the array; When you read it, just lock the previous array. This is the idea of a segmented lock.

Concurrenthashmap is composed of the segment array structure and the HASHENTRY array structure. Segment is actually a reentrant lock (Reentrantlock), which is a lock for segmentation. The hashentry is used to store key-value pairs of data. A concurrenthashmap contains an array of segment. The structure of segment is similar to HashMap, and it is an array and a linked list structure. A segment contains an array of hashentry, each of which is an element of a list structure, each segment guarding an element in a hashentry array, and when the data of the Hashentry array is modified, The segment lock that corresponds to it must be obtained first.

Description: There are two hashing processes: for example, inserting a 1000 number, first of all 1000 of the number of bits (up to 16 bits) to do a hash to find the position in the segments array, and then the 1000 itself to do a hash to find the position in the table

Same as when getting values

The

Concurrenthashmap initialization method is through Initialcapacity, Loadfactor, and Concurrencylevel ( parameters concurrencylevel map According to this to determine segment size of the array concurrencylevel default is default_concurrency_level = +;)

Concurrenthashmap allows multiple read operations to be performed concurrently, and read operations do not require locking. Concurrenthashmap Implementation technology is to ensure that hashentry is almost immutable. hashentry represents a node in each hash chain, and you can see that the object properties are either final , It's either volatile .

Summary:Concurrenthashmap in 1.7 and below the implementation of the use of array + linked list, using the idea of segmented lock

Implementation of CONCURRENTHASHMAP under 1.8

Improved One: Cancel segments field direct use save data with table array element as a lock, which enables the locking of each row of data, further reducing the probability of concurrency conflicts.

improved two: The original table array + unidirectional linked list data structure, changed to table array + one-way list + red black tree structure. For a list of more than 8 ( the default ) ,the structure of the red-black tree is used in jdk1.8, so the time complexity of the query can be reduced to O (LOGN) , you can improve performance.

Summary: The implementation of CONCURRENTHASHMAP in 1.8 uses the array + linked list + red-black tree, when the list number more than 8 when the original linked list into a red-black tree, using red and black trees to access , using the idea of element lock

2. Concurrentskiplistmap and concurrentskiplistset

concurrency implementation of Concurrentskiplistmap TreeMap

concurrency implementation of Concurrentskiplistset TreeSet

know what a skiplist is ?

Two-point lookup and AVL Tree Lookup

A binary lookup requires that the element be randomly accessed, so it determines the need to store the element in contiguous memory. This looks really fast, but when inserting and deleting elements, it takes a lot of moving elements to ensure the order of the elements.

If you need a data structure that can do binary lookups and quickly add and remove elements, the binary lookup tree is the first, and the binary lookup tree can become a linked list in the worst case.

So, there is a balanced binary tree, according to the difference of the balance algorithm has AVL trees ,b-tree,b+tree, red-black trees, etc., but the AVL tree is more complex to achieve, the balance operation is difficult to understand, At this time, you can use the skiplist jumping table structure.

the traditional single-linked list is a linear structure, and inserting a node into an ordered list requires O (n) time, the lookup operation requires O (n) time.

If we use the Jump table shown, we can reduce the time it takes to find O (N/2), because we can first search through the topmost pointers of each node, so we can skip half the nodes.

For example, we want to find19, First and6comparison, greater than6after that, in and9for comparison, and then in and Ato compare......The last comparison is +, I found +Greater than +that indicates the point found in -and the +between, from this process, we can see that the time of the search skipped over the3,7, Aand so the complexity of the lookup isO (N/2).

jumping tables are also an algorithm that uses "space for time" to improve the efficiency of lookups by adding forward pointers to each node.

jumping tables are also known as probabilities, or stochastic data structures, and the current open source software Both Redis and lucence are useful to it.

3. Concurrentlinkedqueue unbounded non-blocking queue

Concurrentlinkedqueue linkedlist Concurrent version

Add,offer: Adding elements

Peek ():get header element does not take the element away

Poll ():get head element take the element away

4. Copyonwritearraylist and copyonwritearrayset

when writing , it can be read concurrently.

for scenarios where read-write is less: such as white list, blacklist, product category to visit and update the scene, if we have a search site, users in this Site search box, enter keywords search content, but some keywords are not allowed to be searched. These keywords that cannot be searched are placed in a blacklist, and the Blacklist is updated every night. When the user searches, it checks that the current keyword is not in the blacklist, and if so, the prompt cannot be searched.

Weaknesses: High memory footprint, weak data consistency

Summary: Write the time to re-copy the data, and then write data in the copied data, after writing the original data reference to perform the copied data, so there is weak consistency of data, suitable for reading more than write less scenes

Multithreaded series five: Concurrent tool classes and concurrent containers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.