Copyonwritearrayset of Java concurrency set_ Power node Java college sorting

Copyonwritearrayset introduction

It is an unordered set of thread safe, which can be understood as a thread safe HashSet. Interestingly, copyonwritearrayset and HashSet inherit from the common parent abstractset; However, the HashSet is implemented through the "HashMap", while the copyonwritearrayset is implemented through the "copyonwritearraylist", not a hash table.

Similar to copyonwritearraylist, copyonwritearrayset has the following features:

1. It is most suitable for applications with the following characteristics: set size is usually kept very small, read-only operations are far more than variable operations, and conflicts between threads need to be prevented during traversal.

2. It is thread safe.

3. Because the entire base array usually needs to be copied, variable operations (add (), set (), remove (), etc.) are very expensive.

4. The iterator supports immutable operations such as hasnext(), next(), but does not support immutable operations such as remove().

5. Traversal using iterators is fast and does not conflict with other threads. When constructing iterators, iterators rely on invariant array snapshots.

Copyonwritearrayset principle and data structure

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

explain:

1. Copyonwritearrayset inherits from abstractset, which means it is a collection.

2. Copyonwritearrayset contains the copyonwritearraylist object, which is implemented through copyonwritearraylist. Copyonwritearraylist is essentially a dynamic array queue,

Therefore, copyonwritearrayset is equivalent to a "set" implemented through a dynamic array! Duplicate elements are allowed in copyonwritearraylist; However, copyonwritearrayset is a collection, so it cannot have duplicate collections. Therefore, copyonwritearraylist additionally provides two APIs for adding elements, addifabsent() and addallabsent(). When adding elements through these APIs, the addition operation is performed only when the elements do not exist!

As for the "thread safety" mechanism of copyonwritearrayset, like copyonwritearraylist, it is implemented through volatile and mutex. The data structure of copyonwritearraylist has been described in the previous chapter. It will not be repeated here.

Copyonwritearrayset function list

Copyonwritearrayset is implemented through copyonwritearraylist, and its API is basically implemented by calling the API of copyonwritearraylist. I believe that understanding copyonwritearraylist and copyonwritearrayset is natural; Therefore, the code of copyonwritearrayset will not be parsed in detail here.

Copyonwritearrayset example

Next, we use an example to compare HashSet and copyonwritearrayset.

(a) operation result:

Result description:

Since set is a collection object, it does not contain duplicate elements.

If the set in the source code is changed to a HashSet object, the program will generate a concurrentmodificationexception.

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
分享
二维码
< <上一篇
下一篇>>