Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java concurrent Container

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand Java concurrent container". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand Java concurrent container".

Catalogue

1. Atomic class

2. Lock

3. Concurrent container

4. Under the List interface

5. Under the Map interface

6. Under the Set interface

7. Under the Queue interface

Java concurrent package (concurrent) is a powerful tool used by Java to deal with concurrency problems. There are mainly atomic classes, lock, concurrent container classes and so on. The main purpose of this series of blogs is to introduce some commonly used concurrency containers and classes in parallel packages. Then let's unveil the package together.

Environment:

Based on JDK1.8

1. Atomic class

The first thing to show up is our atomic class. What is an atomic class? What is the use of atoms?

The first question is what is an atomic class: to manipulate atomic classes, we call them atomic classes. Why should there be atomic classes?

The atomic class is to ensure the atomicity of the operation. For example: long item0; i=i+1 (I is a global variable), in a multithreaded environment, there is a thread safety problem, because the operation i=i+1 is divided into three CPU instructions. Instruction switching occurs after the instruction is executed, resulting in visibility problems. However, if we use the AtomicLong class to wrap I and then call the getAndIncrement () method (which is atomic), we can keep it safe.

AtomicLong atest = new AtomicLong (0); atest.getAndIncrement ()

The main atomic classes are as follows:

2. Lock

When the atomic class is over, let's move on to locks. We all know that the synchronized keyword in Java is used as synchronous locks, and Lock locks are provided in concurrent packages. I'll talk about the difference between Lock and synchronized later.

The class diagram of the lock is as follows:

3. Concurrent container

After briefly talking about locks, let's move on to the highlight concurrency container. Although, Java provides containers for synchronizing containers Vector and Collections wrappers. But the biggest problem with the synchronization container is its poor performance. Because it directly locks all methods of adding elements, deleting elements, and reading elements.

All concurrent containers are provided in concurrent packages, and the implementation of concurrent containers will be described later.

Let's first look at which concurrency containers are available.

Classified according to the type of data structure.

4. Under the List interface

There is a CopyOnWriteArrayList implementation class under the List interface. Its implementation is to maintain an array internally, and the member variable array points to the internal array. Read operations are all based on array, and write operations are performed.

CopyOnWriteArrayList makes a copy of the array and then performs the operation of adding elements on the newly copied array. Point the array to the new array after execution. It is only suitable for scenarios where there are very few writes, and can tolerate temporary inconsistencies between reading and writing.

5. Under the Map interface

There are ConcurrentHashMap and ConcurrentSkipListMap under the Map interface. The internal data structure of ConcurrentHashMap is the same as that of HashMap, which is composed of array, linked list and red-black tree. The data structure inside ConcurrentSkipListMap is the array structure of array + hopping table.

6. Under the Set interface

There are two concurrent classes, ConcurrentSkipListSet and CopyOnWriteArraySet, under the Set interface.

7. Under the Queue interface

There are many concurrent container classes under the Queue interface, and there are under the blocking queue BlockingQueue interface

Single-ended blocking queue:

ArrayBlockingQueue

LinkedBlockingQueue

SynchronousQueue

LinkedTransferQueue

PriorityBlockingQueue

DelayQueue

Double-ended blocking queue: LinkedBlockingDeque

Non-blocking queues are: single-ended queue ConcurrentLinkedQueue, double-ended queue: ConcurrentLinkedDeque.

Thank you for your reading, the above is the content of "how to understand Java concurrent container". After the study of this article, I believe you have a deeper understanding of how to understand Java concurrent container, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report