In layman's language 14 Java concurrent containers

Foreword

Without considering multi-threaded concurrent container classes generally use ArrayList, HashMap such as thread-safe class, more efficient. In concurrency scenarios, often used ConcurrentHashMap, ArrayBlockingQueue containers such as thread-safe, although at the expense of some efficiency, but it was safe.

Thread-safe container mentioned above are in the java.util.concurrent package, this package a lot of concurrent containers, today all turned out to tinker with it.

Do only briefly, and then the follow-up, respectively, in-depth exploration.

 

Concurrent containers introduction

  1. ConcurrentHashMap: Concurrent version HashMap

  2. CopyOnWriteArrayList: Concurrent version of ArrayList

  3. CopyOnWriteArraySet: Concurrent Set

  4. ConcurrentLinkedQueue: concurrent queue (based on the list)

  5. ConcurrentLinkedDeque: concurrent queue (based on two-way linked list)

  6. ConcurrentSkipListMap: Concurrent Map-based jump table

  7. ConcurrentSkipListSet: Set-based Concurrent jump table

  8. ArrayBlockingQueue: blocking queue (array-based)

  9. LinkedBlockingQueue: blocking queue (based on list)

  10. LinkedBlockingDeque: blocking queue (based on two-way linked list)

  11. PriorityBlockingQueue: security thread priority queue

  12. SynchronousQueue: read queue pairs

  13. LinkedTransferQueue: based on data exchange queue list

  14. DelayQueue: Latency Queuing

1.ConcurrentHashMap concurrent version HashMap

One of the most common concurrent containers can be used as a cache under concurrent scenarios. The bottom is still a hash table, but with no small change in JAVA 8, and JAVA 7 JAVA 8 and are used more releases, and both versions will often do some comparison implementations (such as interview).

A relatively large difference is, JAVA 7 with sub locks to reduce the lock of competition, JAVA 8 in abandoned lock segments using CAS (an optimistic locking), and in order to prevent serious conflict degenerate into a hash list ( after conversion generates a list of conflict in this position, the object is the same hash value chain together), reaches a threshold value (8) into a red-black tree in the chain length (compared to the list, tree query efficiency is more stable).

2.CopyOnWriteArrayList concurrent version ArrayList

Concurrent version of the ArrayList, the underlying structure is the array, and the difference is that the ArrayList: creates a new array of elements when adding and deleting time, add or exclude specific object in a new array, and finally replace the original with a new array of arrays .

Applicable scene: As the read operation does not lock, write (add, delete, change) operation lock, therefore less suitable for reading and writing scene.

Limitations: Because time will not read lock (read high efficiency, and the general, like ArrayList), a current copy read, it may be read dirty data. If the mind, do not recommend.

Look at the source feelings:

 
 

3.CopyOnWriteArraySet concurrent Set

Based CopyOnWriteArrayList realization (CopyOnWriteArrayList contains a member variable), that is to say the bottom is an array, means that each add must traverse the entire collection in order to know whether there is a need to insert (lock) does not exist.

Applicable scene: at a plus applicable CopyOnWriteArrayList scene, not too large collections (all traverse the injury can not afford).

4.ConcurrentLinkedQueue concurrent queue (based on the list)

Based on the list to achieve concurrent queue, use optimistic locking (CAS) to ensure thread safety. Because the data structure is the list, so in theory there is no queue size limit, that is to add data will succeed.

5.ConcurrentLinkedDeque concurrent queue (based on two-way linked list)

Achieve concurrent bidirectional linked list queue head and tail can be operated separately, so in addition to FIFO (the FIFO), may be out (FILO) advanced after, of course, that it should be advanced after the call stack.

6.ConcurrentSkipListMap jump table based concurrency Map

SkipList i.e. jump table, jump table is a spatial data structure for time, redundant data, the list index layer by layer, to achieve a similar effect binary search

 
 

7.ConcurrentSkipListSet jump table based on concurrent Set

Similar to the relationship of HashSet and HashMap, ConcurrentSkipListSet which is a ConcurrentSkipListMap, he would not elaborate.

8.ArrayBlockingQueue blocking queue (array-based)

Based blocking queue can be implemented in an array, the array size when the structure must be developed, when entered, put things if the array is full will block until the position (also supports direct return and wait timeout), by a lock ReentrantLock guarantee thread safety.

 
 

At first glance a bit confused, reading and writing are the same lock, that if the air just in time to read a thread here would not have been blocking it?

The answer lies in notEmpty, notFull years, these two little things from the lock so that the lock had similar synchronized + wait + notify functions. Portal → finally get to know the sleep / wait / notify / notifyAll

9.LinkedBlockingQueue blocking queue (based on the list)
based on the blocking list queue implementation, and not blocking the ConcurrentLinkedQueue like, it is more of a capacity limitation, if the maximum value is not set by default int. Ratio

10.LinkedBlockingDeque blocking queue (based on two-way linked list)

Similarly LinkedBlockingQueue, but provides a doubly linked list of unique operations.

11.PriorityBlockingQueue priority queue thread-safe

You can pass when constructing a comparator, can be seen as a bag element will be sorted and then spending time reading order. Some low-priority elements that may not be long-term consumption, because there have been a higher priority elements come in.

12.SynchronousQueue handshake data queue

A false queue, because it had no real space for storing elements, each insert operation must have a corresponding remove operation can not continue to put out when not.

 
 

可以看到,写入的线程没有任何sleep,可以说是全力往队列放东西,而读取的线程又很不积极,读一个又sleep一会。输出的结果却是读写操作成对出现。

JAVA中一个使用场景就是Executors.newCachedThreadPool(),创建一个缓存线程池。

 
 

13.LinkedTransferQueue 基于链表的数据交换队列

实现了接口TransferQueue,通过transfer方法放入元素时,如果发现有线程在阻塞在取元素,会直接把这个元素给等待线程。如果没有人等着消费,那么会把这个元素放到队列尾部,并且此方法阻塞直到有人读取这个元素。和SynchronousQueue有点像,但比它更强大。

14.DelayQueue 延时队列

可以使放入队列的元素在指定的延时后才被消费者取出,元素需要实现Delayed接口。

总结

上面简单介绍了JAVA并发包下的一些容器类,知道有这些东西,遇到合适的场景时就能想起有个现成的东西可以用了。想要知其所以然,后续还得再深入探索一番。

感谢你看完我的长篇大论,如果你觉得本次分享对你有帮助的话,可以帮我点个赞。有不足的地方,也欢迎在下方评论指出。

或者也可以关注我的公众号【Java技术zhai】,不定期的技术干货内容分享,带你重新定义架构的魅力!

Guess you like

Origin www.cnblogs.com/lfs2640666960/p/11884568.html