In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the concurrent container J.U.C in java. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
The concurrency container is a package name provided by JDK: java.util.concurrent
ArrayList-> CopyOnWriteArrayList
CopyOnWriteArrayList is thread-safe and is copied when writing. When a new element is added to the CopyOnWriteArrayList, it is first copied from the original list, and then the operation is written on the new list. After writing, the original list is pointed to the new list. The whole operation is carried out under the protection of the lock. In order to prevent multiple copies of multiple add operations under multiple threads, the final data is not what we expected.
CopyOnWriteArrayList has several disadvantages:
Because the array needs to be copied during the write operation, it consumes more memory. When there are too many elements, it will cause Full GC.
For scenarios that cannot be used for real-time reading, it takes time to copy the array, so after calling a set operation, the read data may still be old, although the final consistency can be achieved, but can not meet the real-time requirements. Therefore, CopyOnWriteArrayList is more suitable for scenarios with more reading and less writing. If you don't know how many times add or set operates, you'd better use this CopyOnWriteArrayList with caution.
HashSet, TreeSet- > CopyOnWriteArraySet, ConcurrentSkipListSet
CopyOnWriteArraySet is also thread-safe, and the underlying implementation is CopyOnWriteArrayList, so CopyOnWriteArraySet is suitable for small set collections where read-only operations are greater than write operations, because the underlying array needs to be copied, so it is expensive for variable operations (add set). Iterators are used to iterate quickly without thread safety problems.
ConcurrentSkipListSet, like TreeSet, supports natural sorting, and you can customize the comparator at construction time. In the case of multithreading, contains (), add () and remove () in ConcurrentSkipListSet are thread safe, and multiple threads can perform insertion, removal and access operations concurrently, but for batch operations such as addAll (), removeAll (), retainAll () and containsAll (), they are not guaranteed to be executed atomically. These operations can be interrupted by other threads, requiring additional locks. Because their implementations call contains (), add (), and remove (), respectively. Because the concurrent container can only guarantee the atomicity of each contains (), add (), remove () operation, but can not guarantee that each batch operation will not be interrupted by other threads. That is, multiple add, multiple remove operations when other threads come in.
HashMap, TreeMap-> ConcurrentHashMap, ConcurrentSkipListMap
ConcurrentHashMap does not allow null. In practical applications, except for a few insert operations and delete operations, most of us use read operations, and most of the read operations are successful. Based on this premise, ConcurrentHashMap does a lot of optimization for read operations, so this class has high concurrency and performs well in high concurrency scenarios.
ConcurrentSkipListMap is a thread-safe version of TreeMap. The interior is implemented using the structure of the skipList hopping table. The access speed of ConcurrentHashMap is about 4 times that of ConcurrentSkipListMap, but the key of ConcurrentSkipListMap is orderly, but ConcurrentHashMap can not. ConcurrentSkipListMap supports higher concurrency, and the access time of ConcurrentSkipListMap is independent of the number of threads. In the case of a certain amount of data, the more concurrent threads, the more ConcurrentSkipListMap can show its advantage.
In the case of low concurrency, Collections.synchronizedSortedMap () can be used to implement it, or it can provide better efficiency. ConcurrentSkipListMap can be used to provide a higher degree of concurrency in cases of high concurrency. You can use ConcurrentSkipListMap when you want to sort key-value pairs.
@ Slf4j@ThreadSafepublic class ConcurrentHashMapExample {/ / Total number of requests public static int clientTotal = 5000; / / number of threads executing concurrently public static int threadTotal = 200; private static Map map = new ConcurrentHashMap (); public static void main (String [] args) throws InterruptedException {/ / thread pool ExecutorService executorService = Executors.newCachedThreadPool (); / / define semaphore final Semaphore semaphore = new Semaphore (threadTotal) / / define counter final CountDownLatch countDownLatch = new CountDownLatch (clientTotal); for (int I = 0; I
< clientTotal; i++) { final int count = i; executorService.execute(() ->{try {semaphore.acquire (); update (count); semaphore.release ();} catch (InterruptedException e) {log.error ("exception", e);} countDownLatch.countDown ();}) } countDownLatch.await (); executorService.shutdown (); log.info ("size: {}", map.size ());} public static void update (int I) {map.put (iMagneI);}}
The output is correct.
ConcurrentSkipListMap:
Private static Map map = new ConcurrentSkipListMap ()
J.U.C
Secure shared object Policy-Summary
Thread restriction: an object restricted by a thread that is exclusive to the thread and can only be modified by the thread that owns it.
Shared read-only: a shared read-only object that can be accessed concurrently by multiple threads without additional synchronization, but no thread can modify it.
Thread-safe object: a thread-safe object or container that ensures thread safety internally through a synchronization mechanism, so that other threads can access it freely through the public interface without additional synchronization.
Guarded object: the guarded object can only be accessed by acquiring a specific lock.
So much for sharing about how to use the concurrent container J.U.C in java. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.