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