Synchronization container classes & Concurrent container classes for the [Java Concurrency programming] collection framework

Source: Internet
Author: User
Tags wrapper

Obscurity also have a career, and know is also boundless. ——— "Chuang Tzu"

In the previous article, we already know the principles and procedures for designing a thread-safe class, as well as the details that we should be aware of during the design process. In fact, the Java collection library contains a thread-safe collection and a non-thread-safe collection, and it can be said that the Java thread-Safe collection library is implemented according to the instance enclosing, security delegate and combination in the previous article.

    • non-synchronous collection

    • Synchronize collection

    • Concurrent collection

So, what are the specific sets of classes they contain, what are the main differences, and what are their frameworks?

This is what we are going to talk about today, after reading this article you should know what specific container classes are used in a common collection library, which collections should be used in the case of multithreading, and the main differences between them.

Java Collection Framework

The Java Collection Toolkit, under the Java.util package, contains commonly used data structures such as arrays, lists, stacks, queues, collections, hash tables, and so on.

Here is a skeleton diagram of the Java collection class first:

Java Collection Framework

The Collection interface is the root interface of the collection class, and there is no direct implementation class in Java that provides this interface. But let it be inherited produces two interfaces, namely Set and List. The set cannot contain duplicate elements. A list is an ordered collection that can contain duplicate elements that provide a way to access by index.

Map is another interface in the Java.util package that is not related to the Collection interface and is independent of each other, but is part of the collection class. Map contains the key-value pair. A Map cannot contain duplicate keys, but it can contain the same value.

    1. hasnext () whether there is a next element.

    2. next () returns the next element.

    3. remove () deletes the current element.

For the Java collection framework, there are no more instructions here, and if you want to do a full anatomy, it is estimated to open another column. Here's a breakdown of the specific container classes, and we'll look directly at what they belong to.

Non-synchronous collection

Unsynchronized collections, which are non-thread-safe when concurrent access, but are more efficient because they do not have a synchronization policy (locking mechanism). Common non-synchronous collections include the following:

  • ArrayList

  • HashSet

  • HashMap

  • LinkedList

  • TreeSet

  • TreeMap

  • Priorityqueue

Synchronizing Collections

Each method is synchronized and locked to ensure thread safety.

  • HashTable

  • Vector

  • Stack

  • Sync wrapper: [Collections.synchronizedmap (), Collections.synchronizedlist ()]

Java Collection class non-thread-safe collections can be used in the synchronization wrapper to make the collection thread-safe, in fact, the implementation principle is equivalent to each method plus a layer of synchronous lock, such as:

    1. HashMap-Collections.synchronizedmap (New HashMap ())

    2. ArrayList-Collections.synchronizedlist (New arraylist<> ())

Concurrent Collections

Under the Java.util.concurrent package, the concurrent container classes are as follows:

  • Concurrenthashmap

  • Concurrentskiplistmap

  • Concurrentskiplistset

  • Copyonwritearraylist

  • Copyonwritearrayset

  • Arrayblockingqueue

  • Linkedblockingqueue

  • Priorityblockingqueue

  • Linkedblockingdeque

  • Concurrentlinkedqueue

differences between a synchronous collection class and a concurrent collection class

whether synchronizing collections or concurrent collections they all support thread safety, the main difference between them is performance and scalability, and how they implement thread safety .

Synchronous collection classes, Hashtable and vectors are also synchronized set wrapper classes, Collections.synchronizedmap () and Collections.synchronizedlist (), It is much slower than concurrent implementations (such as Concurrenthashmap, Copyonwritearraylist, Copyonwritehashset).

The main cause of this slowness is the lock, which locks the entire map or list, each operation is a serial operation, and only one thread can operate at a time. While concurrent collections do not, concurrent collections implement thread safety by using advanced and sophisticated techniques to strip locks.

For example, Concurrenthashmap will divide the entire map into fragments, lock only the relevant fragments, and allow multiple threads to access other unlocked fragments.

Copyonwritearraylist allows multiple threads to read in an unsynchronized way, and it copies the entire list to it when the thread is written. This can be more scalable than using synchronous collections if you are using concurrent collections under conditions that are beneficial to concurrent collections, such as read more write less.

At the end of this article, if the article is wrong, please criticize, if it helps you, you can pay attention to me, thank you!


Synchronization container classes & Concurrent container classes for the [Java Concurrency programming] collection framework

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.