Multi-threaded and highly concurrent 7- container

Here Insert Picture Description
Queue Queue: mainly high concurrency exists, there is a carry out there.
Pun Deque queue: head and tail can access
BlockingQueue blocking queue:
PriorityQueue priority queue: FIFO order
DelayQueue delay queue: in order to be removed only after the expiration of the queue object

HashTable,Vector

Synchronous generation container, all the methods are locked, the basic need

HashTable,HashMap,SynchroizedHashMap,ConcurrentHashMap

HashTable: JDK1.0 era, all methods were locked, poor performance
HashMap: high efficiency but no lock
SynchronizedHashMap: HashMap conversion method to solve the problem of lock-free, Collections tools provided. HashTable locked directly to the method, the efficiency is higher by a lock object.

Collections.synchronizedMap(new HashMap<>);

ConcurrentHashMap: Concurrent table

On the write efficiency, it mainly depends on the number of concurrent threads and, if small concurrency, sync type of map does not necessarily lower than ConcurrentHashMap. (Sync lock upgrade). Use ConcurrentHashMap high concurrency.
On reading efficiency, the number of times higher than ConcurrentHashMap HashTable efficiency and SyncTable

ArrayList,Vector,Queue

ArrayList thread-safe, generate multiple threads access oversold
Vector thread-safe, but all methods were locked
ConcurrentLinkedQueue in multithreaded conditions, can not be repeated if there is no thread, try to use Queue. queue offers a lot of multithreaded friendly api [offer () peek () poll ()]
for blockingqueue [put () take () ]

Queue q = new ConcurrentLinkedQueue<>();
q.add();
q.poll();

ConcurrentHashMap,ConcurrentSkipListMap

Non-thread synchronization: hashMap, treeMap (red-black tree sort, query speed)
thread synchronization: ConcurrentHashMap, ConcurrentSkipListMap (. Jump table, CAS Tree too complex to implement, jump table is implemented by redis zset bottom)
Here Insert Picture Description
jump table structure, the bottom is still is a list (sorting), elected parent, the parent who is still linked list structure (parallel chain), if the elements more election again. Query, the range between the parent node and, ultimately, to the node.
Much faster than the efficiency of the linked list (O (n), O ( logn))

CopyOnWriteArrayList,CopyOnWriteArraySet

Applications: multi-threaded, multi-read-write operations less operating conditions. The writing of sync, by Arrays.copyOf new array of new elements after the exchange. Not locked (except new elements, other elements do not need to exactly the same lock) read
copy-on-write, when adding a new element, a copy Array element into the final position, and then point to the new address.

BlockingQueue

LinkedBlockingQueue unbounded (not more than Integer.MAX_VALUE)
ArrayBlockingQueue with bounded
DelayQueue ordered in time
Synchronousqueue transfer tasks between the two threads
Transferqueue more set q

Queue commonly used methods:

the offer () added elements, added elements if successful returns a return value
peek () remove the element, and does not remove the element
poll () remove the elements, and the elements removed

BlockingQueue common method (born achieve the production of consumers):

put () into the element, if the full waits
take () remove elements, such as elements not desirable, will block

ArrayBlockingQueue

After the constructor method may determine the size, reaches a threshold value:
PUT method is performed, the elements are not added, and blocked;
the Add method is performed, will throw an exception, Queue Full;
the offer execution method, does not add element returns false, the value of the constructor can be passed in time to try to add

DelayQueue

Tasks can be performed based on time, the timing task scheduling

PriorityQueue

Without removing the insertion order, ordered by priority, the minimum of the first

SynchronusQueue

Capacity is zero, the effect is not used to load elements, but the thread assigned to other tasks.

Use add () abnormally, because the capacity is 0, the elements can not be added. Use only put () perform a blocking operation, waiting for take () elements removed

TransferQueue

LinkedTransferQueue
Transfer ( "the Test"); after adding elements, obstruction, wait removed elements.
Scene: the consumer must be determined removed elements.

Published 25 original articles · won praise 0 · Views 580

Guess you like

Origin blog.csdn.net/RaymondCoder/article/details/105104367