JUC concurrent Tools

Original please indicate the source: https://www.cnblogs.com/agilestyle/p/11449367.html

 

java.util.concurrent and its sub-packages, focused on a variety of basic tools like Java concurrency, including several specific areas:

  • Provides more advanced than the synchronized synchronization of various structures, including CountDownLatch, CyclicBarrier, Semaphore, etc., can achieve a richer multi-threaded operation, such as the use of the number of threads Semaphore resource controller, limit simultaneous work.

  • A variety of thread-safe container, such as the most common ConcurrentHashMap, orderly ConcunrrentSkipListMap, or through a similar snapshot mechanism to achieve thread-safe dynamic array CopyOnWriteArrayList and so on.

  • Various concurrent queue implementation, such as various BlockedQueue achieved, typical ArrayBlockingQueue, SynchorousQueue PriorityBlockingQueue or the like for a specific scene.

  • Executor powerful framework that can create a variety of different types of thread pools, scheduled tasks such as running, in most cases, no longer need to re-implement their own thread pools and task scheduler.

 

Map concurrent thread-safe bag provided, List, and Set. Class reference to the following FIG.

If the application focuses on the Map or get into the speed, and do not care about the order, mostly recommended ConcurrentHashMap

Otherwise use ConcurrentSkipListMap; if large amounts of data need to be modified very frequently, ConcurrentSkipListMap also likely to show superiority.

 

About two CopyOnWrite container, in fact CopyOnWriteArraySet is achieved by wrapping the CopyOnWriteArrayList.

CopyOnWrite Its principle is that any modification operations, such as post-add, set, remove, will copy the original array, modify replace the original array, such a defensive way to achieve alternative thread safe.

So this data structure, is relatively less suitable for reading and writing operations, or modify the cost is still very obvious.

 

Guess you like

Origin www.cnblogs.com/agilestyle/p/11449367.html