Concurrenthashmap
Concurrenthashmap is a version of HashMap in a concurrent environment, and you may want to ask, Now that you can get a thread-safe mapping container by Collections.synchronizedmap, why do you need Concurrenthashmap? Because the thread-safe HashMap obtained through the Collections tool class locks the entire container object when reading and writing data, other threads that use the container will no longer be able to acquire the object's locks anyway, It also means waiting for the previous thread to get the lock to leave the synchronized code block before it can be executed. In fact, the HASHMAP is the hash function to determine the bucket that holds the key value pair (the bucket is introduced to resolve the Greek conflict), the modification of the HashMap does not need to lock the entire container, only need to lock the "bucket" will be modified. The HASHMAP data structure is shown in the following figure.
In addition, Concurrenthashmap provides a method of atomic manipulation, as follows: Putifabsent: If there is no corresponding key-value pair mapping, add it to the HashMap. Remove: If the key exists and the value is equal to the current state (the Equals comparison is true), the key value pair is removed by atomic means to map Replace: Replace the atomic operations of the elements in the map copyonwritearraylist
Copyonwritearraylist is a substitute for ArrayList in concurrent environments. Copyonwritearraylist to avoid problems caused by concurrent access by adding write-time copy semantics, which means that any modification will create a copy of the list at the bottom, meaning that previous iterators do not encounter unexpected modifications. This approach is useful for not strictly reading and writing synchronization scenarios because it provides better performance. Remember, try to minimize the use of locks, because that will result in a drop in performance (not the same with concurrent access to data in the database). If you can, you should give up the pessimistic lock and use optimistic lock, copyonwritearraylist is obviously also through the sacrifice of space to obtain time (in the computer World, time and space is often irreconcilable contradictions, can sacrifice space to enhance efficiency to obtain time, Of course, you can reduce the use of space by sacrificing time.
Queue
Queues are a ubiquitous and wonderful concept that provides a simple and reliable way to distribute resources to a processing unit (or to allocate a unit of work to a resource to be processed, depending on how you look at the problem). Many of the concurrent programming models in the implementation depend on queues, because they can pass work units across threads.
The Blockingqueue in Java 5 is a very useful tool in a concurrency environment, when the put method is invoked to insert an element into the queue, if the queue is full, it makes the thread that inserts the element wait for space in the queue, and if the queue is empty when the Take method is invoked to take an element from the queue, The thread that takes out the element is blocked.