Java Absolute Dry--list, Set, map concurrency data structure comparison implementation

Source: Internet
Author: User

JAVA concurrency Data structure comparison implementation

For List,Set, andMap Concurrent applications, we can use the collections The following method converts a non-thread-safe List,set, andMap to Thread-safe. But efficiency is not the best,the JDK provides a dedicated thread-safe List and Set implementation class, We'll discuss their implementation later.



First, copyonwritearraylist and vector realization discussion

Copyonwritearraylist and vectors are two thread-safe lists, andArrayList are not thread-safe. Below we compare the implementation of these two thread-safe List How different, each has what advantages and disadvantages.

1. The copyonwritearraylist Get () method implements the

The Copyonwritearraylist get method does not have any locks. So the efficiency is high.

2. The Get method of vector is implemented as follows:

The get method of the Vector increases the lock, so the comparison of the two get methods can illustrate the copeonwritearraylist read more efficiently.

3. Below we compare the implementation of vector and copyonwritearraylist in adding data.

The Add () method of the Vector is implemented as:

4. The Add () method of Copyonwritearraylist is implemented as follows:

The implementation of the Copyonwritearraylist add method is to lock, then copy the array, add new elements, and then write, compared to the simple Vector lock ratio, Copyonwritearraylist Adding methods should be less efficient. So

Concurrent write multi-case is recommended with Vector, concurrent read a lot of cases suggested with copyonwritearraylist.

Second, concurrent set

Copyonwritearrayset is a thread-safe implementation of the set interface. It is internally dependent on copyonwritearraylist, so it is suitable for read and write-less concurrency.

Third, concurrent map

The JDK provides a high concurrency map implementation Concurrenthashmap. Its get is unlocked, but the put is locked.

Iv. Concurrent Queue

Concurrentlinkedqueue is a queue that is suitable for high concurrency scenarios. It achieves high performance in a highly concurrent state by means of a lock-free way, and usually its performance is better than the typical implementation of Blockingqueue Arrayblockingqueue, Linkedblockingqueue.

V. Concurrent Deque

Deque is a two-terminal queue that can be queued at the head or tail.

The implementation class has Arraydeque, Linkedblockingdeque.

Where Linkedblockingdeque is thread-safe, but both read and write are locked, so efficiency is not particularly high.

Java Absolute Dry--list, Set, map concurrency data structure comparison implementation

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.