A collection of articles highly concurrent programming

The traditional set of thread-safe I cause problems, so we should use the thread-safe collection.

1, instead of using HashMap ConcurrentHashMap, internal ConcurrentHashMap further subdivided HashMap number of small segments, the default is 16.

2, ConcurrentSkipListMap: realize jump table. Is a Map, using the data structure of the table jump quickly find, features: orderly, balanced tree, a hierarchical list, jump query, space for time.

3, instead of using Vector ArrayList.

4, CopyOnWriteArrayList: reading how much occasion to write this List of performance is very good, much better than Vector. Read when unlocked, written when locked, and a copy of the original array will generate a new array, and then copy this array to write, and then replace the original array and then modified arrays, so, again At the same time writing, if there is a read operation, it will not conflict problem because there is no change in the original array.

5, ConcurrentLinkedQueue: efficient concurrent queue (queue best performance high concurrency environment), using the linked list implementation thread safe LinkdeList (the underlying mechanism used to achieve lock-CAS), the ConcurrentLinkedQueue (non-blocking unbounded queue) vulnerable, the columns do not wait, do not wait for a column may cause data loss.

6, BlockingQueue interface (blocking) uses only runnable interfaces used to store
    operating mode
        into the column
            when inserting data, exceeds the total number of the queue will wait (blocking)
        the column
            when acquiring data, is empty, waits (obstructive)
    type
        SynchronousQueue directly to the queue
            without capacity, each insert must wait deletion response, and vice versa, each corresponding to a delete operation must wait insertion operation. Will not save the task, directly to the thread of execution, there is no idle thread tries to create a new thread, the maximum number of threads is executed deny policy. Therefore, generally set maximumPoolSize great value, otherwise it is easy to perform denial strategy
        ArrayBlockingQueue bounded task queue
            given capacity, new tasks come in if the core is greater than the number of threads created, under the premise of not more than the maximum number of threads, will continue to create, if greater than the maximum number of threads, execute deny policy
            written
                offer () returns immediately when the queue is full false
                PUT () when the queue is full will wait until there is space in the queue position
            readout
                poll () returns null direct queue is empty
                take () waits until the queue has elements
        LinkedBlockingQueue unbounded task queue
            until system resources are exhausted, otherwise the mission will not cause the team failed
            lock isolated, different locks take () and put () operation uses, weakening the possibility of lock contention. To achieve the separation of data and write data fetch, it is both become available concurrent operation in the true sense
        ProorityBlockingQueue priority queue
            execution order with a priority queue, control tasks, special unbounded queue.

7, Collections tools can help us will be packed into any set-safe collection, usually enough in the concurrency level is not high, because the lock, causing all operations on the set of all entered the wait state
    Collections.synchronizedMap (new HashMap ());
    the Collections.synchronizedList (the LinkedList new new <String> ());

    ...... related Collections tools do not list them.

   

 

Published 18 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_32285039/article/details/103322609