Concurrency data structure under multithreading

Source: Internet
Author: User

1. Concurrent List
There are three kinds of data structures in ArrayList, linkedlist and vectors under list, in which vector is thread-safe.
There is also a list interface implemented by the Copyonwritearraylist class under List, which is also thread-safe.

Copyonwritearraylist vs. Vectors:
(1) Position of the lock
The implementation of Copyonwritearraylist is to remove the lock in the read operation, while the write has a lock and more copy operation.
The vector adds a lock in both read and write operations.
(2) Speed comparison
Copyonwritearraylist is faster than vector because it removes the lock in the read operation. However, the copyonwritearraylist is more than the copy operation, so its write speed is slower than Vect.

Personal recommendation: In reading and writing small with copyonwritearraylist, if read less write long, with vector

2. Concurrent Set
Similar to list, set also has a copyonwritearrayset, which implements the set interface, thread safety.

Copyonwritearrayset, its internal implementation is completely dependent on the copyonwritearraylist, so copyonwritearraylist has the characteristics, copyonwritearrayset all have.

Personal recommendation: When reading more than writing, use Copyonwritearrayset.
3. Concurrent Map
The map has HashMap, HashTable (subclass Linkedhashmap), TreeMap, Concurrenthashmap. The thread-safe is hashtable and Concurrenthashmap.
Concurrenthashmap compared to Hashtable:
Concurrenthashmap get () is not locked, put () and use to reduce the lock granularity of the way to synchronize, rather than want to hashtable use synchronized simple synchronization, so its efficiency than Hashtable high.
   size of Lock:
For Concurrenthashmap, instead of locking the entire hashmap, he divides the HashMap into segments that require a put () operation, which is obtained according to its hashcode
This paragraph, the paragraph is given a specific lock, the other segments can be used by other threads to lock.

Personal recommendation: If security is not considered, use HashMap. Consider security using Concurrenthashmap.
4. Concurrent Queue
There are two types of concurrent queues, one for concurrentlinkedqueue with high concurrency, and the other for blocking queue blockingqueue.

Detailed
Concurrentlinkedqueue uses a lock-free approach, so its performance is good in high concurrency.
Blockingqueue uses the mode of producer consumer to lock. The concrete implementation of blocking has Arrayblockingqueue and linkedblockingqueue

Personal recommendation: If high concurrency is required, use Concurrentlinkedqueue. If you want to implement data sharing in multiple threads, use Blockingqueue.

5, concurrent Dueue (double-ended queue)
The concrete realization of dueue has linkedblockingdueue. Dueue Dueue inherits the queue, so it has more features than queue. But the performance of Linkedblockingdueue is much lower than that of linkedblockingqueue, less than Concurrenlinkedqueue.

Concurrency data structure under multithreading

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.