The armed forces can seize the handsome also, bastard not to seize the ambition also. ——— The Analects of Confucius
The previous article mentioned the potential problems of the synchronization container class, which can be solved by two methods.
Can be resolved by a client lock.
You can use concurrent container classes to troubleshoot problems.
Client Lock method We already know, so, this article introduces the principle of concurrent container class, see how it solves these problems.
Let's look at the frame diagram of the concurrent container:
Write a picture description here
As we can see, they fall into five categories: Map, List, Set,collection,queue, and synchronous container classes inherit from these five base classes, all of which are thread-safe.
The way that the synchronization container class implements thread safety is that all methods hold a lock and concurrent containers are different, and here are some examples of concurrent container classes.
Concurrenthashmap
It is not that every method is synchronized in the same lock so that only one thread can access the container at a time. Instead, a finer-grained locking mechanism is used to achieve a greater degree of sharing, a mechanism called segmented locks . In this mechanism, any number of read threads can access the map concurrently, the thread that performs the read operation and the thread that performs the write operation can access the map concurrently, and a certain number of write threads can concurrently modify the map. So in a concurrent environment, it achieves higher throughput and loses very little performance in a single-threaded environment.
Because Concurrenthashmap cannot lock to perform exclusive access, we cannot create new atomic operations through client Gathalai. However, some common compound operations have been implemented. The following interfaces are included:
Write a picture description here
When you need these extra atomic operations, then you can consider using it.
copyonwritearraylist
when you copy a container on write, you can understand that when you add an element to a container, the current container is copied, a new container is generated, and the element is added to the new container before the reference to the original container is pointed to the new container.
The advantage is that the Copyonwrite container can be read concurrently without locking because no element is added at this time. Copyonwrite is a read-write separation of ideas, read and write operations are different containers.
It can be used instead of synchronizing the List, but the underlying array is copied each time the container is modified. For example, add (), set () and other operations, it requires a certain amount of overhead, especially when the size of the container is large, it is more suitable for read and write less concurrent scenarios.
Arrayblockingqueue
It is an array-implemented thread-safe, bounded blocking queue (FIFO) that supports multi-tasking concurrent operations. It protects competing resources internally through mutual exclusion Lock, realizes multi-threading mutually exclusive access to competing resources, bounded is the length of exponential group is fixed; blocking means that when a competing resource is already acquired by a thread, other threads that want to acquire the resource need to block waiting.
Its core function is to ensure thread synchronization through reentrant lock Reentrantlock. Include put (), offer (), take (), poll (), etc. At the same time, there are two conditions Condition (Notempty, Notfull), the join element is, if the queue is full, you must wait, when the element is taken out, if the queue is empty, you must wait.
Summary
Concurrent containers are designed for concurrent access to multiple threads, and concurrent containers instead of synchronous containers can greatly improve scalability and reduce risk. The iterations provided by the concurrent container do not throw concurrentmodificationexception, so there is no need to lock the container during the iteration. In addition, concurrent containers can only guarantee the final consistency of the data, and cannot guarantee real-time consistency. In other words, the modified data of the container is not guaranteed to be able to react to the iterator's traversal in real time.
Reference
"Java Concurrency Programming"
Http://www.cnblogs.com/leesf456/p/5428630.html
[Java concurrent programming] A brief introduction to the concurrent container framework