Java Multithreading Series--"Juc Collection" 06 of the Concurrentskiplistset

Source: Internet
Author: User

Overview

This chapter provides a detailed introduction to the Concurrentskiplistset class in the Java.util.concurrent package. The content includes:
Concurrentskiplistset Introduction
Concurrentskiplistset principles and data structures
List of Concurrentskiplistset functions
Concurrentskiplistset Source code (JDK1.7.0_40 version)
Concurrentskiplistset Example

Reprint Please specify source: http://www.cnblogs.com/skywang12345/p/3498634.html

Concurrentskiplistset Introduction

Concurrentskiplistset is a thread-safe, ordered collection that is suitable for high concurrency scenarios.
Concurrentskiplistset and TreeSet, though they are all orderly collections. However, first, they have different thread-safety mechanisms, TreeSet is non-thread-safe, and Concurrentskiplistset is thread-safe. Second, Concurrentskiplistset is achieved through CONCURRENTSKIPLISTMAP, and TreeSet is achieved through TREEMAP.

concurrentskiplistset principles and data Structures

Concurrentskiplistset the data structure, as shown in:

Description :
(Concurrentskiplistset) inherited from Abstractset. Therefore, it is essentially a collection.
(Concurrentskiplistset) implements the Navigableset interface. Therefore, Concurrentskiplistset is an ordered collection.
Concurrentskiplistset is implemented through Concurrentskiplistmap. It contains an Concurrentnavigablemap object m, and the M object is actually an instance of the Concurrentnavigablemap implementation class Concurrentskiplistmap. The elements in Concurrentskiplistmap are key-value key-value pairs, and Concurrentskiplistset is a collection that uses only the key! in Concurrentskiplistmap

List of Concurrentskiplistset functions
//Constructs a new empty set that sorts the elements in their natural order.Concurrentskiplistset ()//Constructs a new set that contains the elements in the specified collection, and the new set sorts them according to the natural order of the elements. Concurrentskiplistset (collection<?Extends e>C//Constructs a new empty set that sorts its elements according to the specified comparer. Concurrentskiplistset (comparator<?Super e>Comparator//Constructs a new set that contains the same elements as the specified ordered set and uses the same order. Concurrentskiplistset (sortedset<e>S//If this set does not contain the specified element, the specified element is added.BooleanAdd (e E)//Returns the smallest element in this set that is greater than or equal to the given element, or null if no such element exists.E ceiling (e e)//Removes all elements from this set.voidClear ()//Returns a shallow copy of this Concurrentskiplistset instance. Concurrentskiplistset<e>Clone ()//Returns a comparer that sorts the elements in this set, or null if the set uses the natural order of its elements. comparator<?Super e>Comparator ()//Returns True if this set contains the specified element.BooleanContains (Object o)//Returns an iterator that iterates in descending order on the elements of this set. Iterator<e>Descendingiterator ()//Returns an inverted view of the elements contained in this set. Navigableset<e>Descendingset ()//Compares the equality of the specified object to this set.BooleanEquals (Object o)//Returns the current first (lowest) element in this set.E First ()//Returns the largest element in this set that is less than or equal to the given element, or null if no such element exists.E floor (e e)//Returns a partial view of this set whose elements are strictly less than toelement. Navigableset<e>HeadSet (E toelement)//Returns a partial view of this set whose elements are less than (or equal to, if inclusive is true) toelement. Navigableset<e> HeadSet (E toelement,BooleanInclusive//Returns the smallest element in this set that is strictly greater than the given element, or null if no such element exists.e higher (e e)//Returns True if this set contains no elements.BooleanIsEmpty ()//Returns an iterator that iterates in ascending order on the elements of this set. Iterator<e>Iterator ()//Returns the current last (highest) element in this set.E last ()//Returns the largest element in this set that is strictly less than the given element, or null if no such element exists.e Lower (e e)//Gets and removes the first (lowest) element, or null if this set is null.E Pollfirst ()//Gets and removes the last (highest) element, or null if this set is null.E Polllast ()//If the specified element is present in this set, it is removed.BooleanRemove (Object o)//Removes all elements contained in the specified collection from this set.Boolean RemoveAll (collection<?>C//Returns the number of elements in this set.IntSize ()// Returns a partial view of this set whose elements range from fromelement to toelement. Navigableset<e> subset (e fromelement, boolean frominclusive, e toelement, boolean toinclusive) //  Returns a partial view of this set, whose elements are from fromelement (including) to Toelement (not included). Navigableset<e> subset (e fromelement, E toelement) // Returns a partial view of this set whose elements are greater than or equal to fromelement. Navigableset<e> Tailset (E fromelement) // Returns a partial view of this set whose elements are greater than (or equal to, if inclusive is true) fromelement. Navigableset<e> Tailset (E fromelement, boolean inclusive)    

Concurrentskiplistset Source code (jdk1.7.0_40 version)

Concurrentskiplistset.java's complete source code is as follows:

View Code

Concurrentskiplistset is implemented through CONCURRENTSKIPLISTMAP, and its interfaces are basically implemented by invoking the Concurrentskiplistmap interface. There is no longer an analysis of its source code.

Concurrentskiplistset Example
1Import java.util.*;2Import java.util.concurrent.*;34/*5* Concurrentskiplistset is a "thread safe" collection, and TreeSet is non-thread safe.6*7* The following is an example of "multiple threads working concurrently and traversing set set"8* (01) When set is a Concurrentskiplistset object, the program will run normally.9* (02) When set is a TreeSet object, the program generates an Concurrentmodificationexception exception.10*11* @author Skywang12*/13PublicClassConcurrentSkipListSetDemo1 {1415//When the Todo:set is a TreeSet object, the program makes an error.16//private static set<string> Set = new treeset<string> ();17Privatestatic Set<string> Set =New concurrentskiplistset<string>();18PublicStaticvoidMain (string[] args) {1920//Start two threads at the same time to operate the set!21stNew MyThread ("a"). Start ();22New MyThread ("B"). Start ();23}2425PrivateStaticvoidPrintall () {String value =Null;Iterator ITERSet.iterator ();28While(Iter.hasnext ()) {Value =(String) Iter.next ();System.out.print (value+ ",");31}32System.out.println ();33}3435PrivateStaticClass MyThreadExtendsThread {36MyThread (String name) {37Super(name);38}39@Override40PublicvoidRun () {41int i = 0;42 43 // "thread name" + "ordinal" 44 String val = Thread.CurrentThread (). GetName () + (I%6 Set.add (val); // traverse set through "Iterator". 47  printall ();  }49 }50 }51}       

Run results (one time) :

Result Description :
In the sample program, two threads (thread A and thread B) are started to operate on Concurrentskiplistset respectively. In the case of thread A, it first gets the thread name + ordinal and then adds the string to the Concurrentskiplistset collection, and then iterates over and outputs all the elements in the collection. Thread B operates like thread A, except that the name of thread B differs from the name of thread A.
When set is a Concurrentskiplistset object, the program runs correctly. If you change set to TreeSet, the program produces an concurrentmodificationexception exception.

Java Multithreading Series--"Juc Collection" 06 of the Concurrentskiplistset

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.