Java Concurrent Programming: synchronization container

Java Concurrent Programming: synchronization container

In order to write thread safe programs, Java provides some thread safe classes and concurrency tools, such as synchronization container, concurrency container, blocking queue and synchronizer (such as countdownlatch). Today we will discuss synchronization container.

The following is an outline of the contents of this article:

I Why do synchronization containers appear?

II Synchronization container classes in Java

III Defects of synchronization container

If there is anything wrong, please forgive me and welcome criticism and correction.

Please respect the author's labor achievements. Please indicate the original link for Reprint:

   http://www.cnblogs.com/dolphin0520/p/3933404.html

I Why do synchronization containers appear?

In the collection container framework of Java, there are four main categories: list, set, queue and map.

List, set and queue interfaces inherit the collection interface respectively, and map itself is an interface.

Note that collection and map are a top-level interface, while list, set and queue inherit the collection interface, representing array, set and queue containers respectively.

For example, ArrayList and LinkedList implement the list interface, HashSet implements the set interface, deque (two-way queue, allowing queue in and out operations at the head and tail of the queue) inherits the queue interface, and PriorityQueue implements the queue interface. In addition, LinkedList (actually a two-way linked list) implements the deque interface.

Containers like ArrayList, LinkedList, and HashMap are non thread safe.

Problems arise when multiple threads access these containers concurrently.

Therefore, when writing programs, programmers must be required to manually synchronize anywhere they access these containers, which makes it very inconvenient to use these containers.

Therefore, Java provides a synchronization container for users to use.

II Synchronization container classes in Java

In Java, synchronization containers mainly include two types:

  1)Vector、Stack、HashTable

2) the class created by the static factory method provided in the collections class

Vector implements the list interface. Vector is actually an array, similar to ArrayList, but the methods in vector are synchronized, that is, synchronization measures are taken.

Stack is also a synchronization container. Its methods are synchronized with synchronized. In fact, it inherits from the vector class.

Hashtable implements the map interface, which is very similar to HashMap, but hashtable performs synchronization, while HashMap does not.

Collections class is a tool providing class. Note that it is different from collection, which is a top-level interface. A large number of methods are provided in the collections class, such as sorting and finding collections or containers. Most importantly, it provides several static factory methods to create synchronization container classes, as shown in the following figure:

III Defects of synchronization container

It can be seen from the specific implementation source code of the synchronization container that the methods in the synchronization container are synchronized. Obviously, this will inevitably affect the execution performance. In addition, must the synchronization container be truly thread safe? Not necessarily. This will be discussed below.

Let's first look at the performance differences between traditional asynchronous containers and synchronous containers. Let's take ArrayList and vector as examples:

1. Performance issues

Let's take a look at the performance difference between vector and ArrayList when inserting data through an example:

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