Concurrentskiplistset of Java concurrency set_ Power node Java college sorting

Introduction to concurrentskiplistset

Concurrentskiplistset is a thread safe and orderly collection, which is suitable for high concurrency scenarios. Concurrentskiplistset and TreeSet, although they are ordered sets. However, first, their thread safety mechanisms are different. TreeSet is non thread safe, while concurrentskiplistset is thread safe. Second, the concurrentskiplistset is implemented through the concurrentskiplistmap, while the TreeSet is implemented through the treemap.

Principle and data structure of concurrentskiplistset

The data structure of concurrentskiplistset is shown in the following figure:

explain:

(01) concurrentskiplistset inherits from abstractset. Therefore, it is essentially a set. (02) the concurrentskiplistset implements the navigableset interface. Therefore, the concurrentskiplistset is an ordered set. (03) the concurrentskiplistset is implemented through the concurrentskiplistmap. It contains a concurrentnavigablemap object m, which is actually an instance of the implementation class concurrentskiplistmap of concurrentnavigablemap. The elements in the concurrentskiplistmap are key value pairs; The concurrentskiplistset is a collection, which only uses the key in the concurrentskiplistmap!

List of concurrentskiplistset functions

Concurrentskiplistset is implemented through concurrentskiplistmap, and its interfaces are basically implemented by calling the concurrentskiplistmap interface. Here we will not analyze its source code.

Concurrentskiplistset example

(a) operation result:

Result description:

In the sample program, start two threads (thread a and thread b) to operate the concurrentskiplistset respectively. For thread a, it will first obtain "thread name" + "sequence number", and then add the string to the concurrentskiplistset set; Next, all the elements in the collection are traversed and output. The operation of thread B is the same as that of thread a, except that the name of thread B is different from that of thread a.

When set is a concurrentskiplistset object, the program can run normally. If set is changed to TreeSet, the program will generate a concurrentmodificationexception.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>