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

What is the function of Java synchronization container and concurrency container

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what is the role of Java synchronization container and concurrent container". In daily operation, I believe that many people have doubts about the role of Java synchronization container and concurrent container. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the role of Java synchronization container and concurrent container"! Next, please follow the editor to study!

Synchronization container

The synchronous container modifies the container by the synchronized keyword to ensure that only one thread uses the container at a time, thus making the container thread safe. Synchronized means synchronization.

Both 1.Vector and ArrayList implement the List interface. Vector operates on arrays the same as ArrayList. The difference is that Vector adds the synchronized keyword to the possible thread-safe methods.

2.Stack is a subclass of Vector. Stack implements first-in, last-out, synchronized modification in the out-and-out stack.

3.HashTable: it implements the Map interface, the operation is the same as HashMap (difference: HashTable cannot store null,HashMap key value can be null), all operations of HashTable are modified with synchronized.

4.Collections provides thread synchronization collection classes

List list=Collections.synchronizedList (new ArrayList ()); Set set=Collections.synchronizedSet (new HashSet ()); Map map=Collections.synchronizedMap (new HashMap ()); concurrent container

A concurrent container is a container that allows multi-thread access and ensures thread safety. In order to improve the concurrency as much as possible, many optimization methods are adopted in the Java concurrency toolkit to improve the execution efficiency of the concurrent container. The core is lock, CAS (no lock), COW (read-write separation), segmented lock.

1.CopyOnWriteArrayList

CopyOnWriteArrayList is equivalent to thread-safe ArrayList. When writing to the container, Copy gives a copy array, assigns the reference of the copy array to the container after the operation is completed, and the bottom layer is to ensure synchronization through ReentrantLock. However, it sacrifices container consistency for container concurrency (old data is read during Copy), so it can not be used in strongly consistent scenarios.

2.CopyOnWriteArraySet

The principle of CopyOnWriteArraySet is the same as that of CopyOnWriteArrayList, it is a Set collection that implements the CopyOnWrite mechanism.

3.ConcurrentHashMap

ConcurrentHashMap is equivalent to thread-safe HashMap,Key is unordered, and neither key nor value can be null. Before JDK8, the mechanism of segmented locking is used to improve concurrency, and locking is needed only when operating the same key-value pair. After JDK8, CAS algorithm is used to improve container concurrency.

4.ConcurrentSkipListMap

ConcurrentSkipListMap is equivalent to thread-safe TreeMap,key is ordered, key and value are not allowed to be null, it uses jump table to replace the red-black tree, because the red-black tree needs to make rotation adjustment when inserting or deleting nodes, resulting in the granularity to be controlled is too large. The jump table uses linked lists and uses CAS algorithm to achieve high concurrency thread safety.

5.ConcurrentSkipListSet

The principle of ConcurrentSkipListSet is the same as that of ConcurrentListMap. It is a TreeSet that implements thread safety.

Strong consistency

After a certain data in the system is updated, any subsequent reading of the data will get the latest value, and the data in all nodes is the same at any time. For relational databases, it is required that the updated data can be seen by subsequent visits, which is highly consistent.

Weak consistency

After a certain data in the system is modified, the subsequent reading of the data may get the updated value and the pre-updated data, but after the inconsistent window, the subsequent reading of the data will get the changed value.

Final consistency

Is a special form of weak consistency, the storage system ensures that without updates, all access to the data will get the updated data. It is not guaranteed that the same data on any node at any time is the same, but with the migration of time, the same data on different nodes is always changing in the direction of convergence.

At this point, the study on "what is the role of Java synchronization container and concurrent container" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report