Concurrent libraries for Jdk5

Source: Internet
Author: User

A. Traditional collection
Collection
List:arraylist LinkedList Vector Stack
Set:hashset TreeSet
Map:hashmap TreeMap
Second, the simple no longer say, only say no!
1. Vector: Create an Object array of size 10 by default (so it is an array-based implementation) Add, the Remove method adds the Synchronized keyword, so thread-safe
Vector enlargement array is twice times more thread-safe to expand
ArrayList widening *1.5+1 Thread is unsafe
2. Stack: Advanced post-Exit
Inherit from Vector, so it's thread safe!
Push Pop Peek
3
HashSet based on HASHMAP implementation, using key in HashMap, no capacity limit, non-thread safe
TreeSet based on TREEMAP implementation, support sequencing, non-thread safe
4
HashMap based on entry array implementation, Key,value entry, no capacity limit, non-thread-safe
TreeMap based on red and black trees, no capacity limit, non-thread-safe
Three, various realization way
ArraylIst Array
LinkedList doubly linked list
Vector Array
Stack Array
HashSet Based on HASHMAP implementation
TreeSet Based on TREEMAP implementation
HashMap array implementation based on entry object
TreeMap based on red and black trees
Iv. some performance of traditional collections
1, set and map type lookup is through the hash, and then to the list to find, so the speed is relatively fast!
2, when the quantity exceeds million levels:
Insert: ArrayList vector has a large influence, the other basic nothing changes
Find and delete; list's performance degradation is serious, set and map are basically unaffected
V. Concurrent COLLECTIONS
The following are thread-safe:
The Concurrenthashmap is protected with a lock on each segment, which allows finer granularity of the lock, better concurrency, and hashmap No locking mechanism, not thread-safe.
Copyonwritearraylist in read-write operation without lock ArrayList
Cosyonwritearrayset Based on copyonwritearraylist implementation
Arrayblockingqueue array-based, FIFO, block read/write
Arrayblockingqueue (int capacity) creates a arrayblockingqueue with the given (fixed) capacity and the default access policy. Simultaneous initialization of locks and two locked condition, one for Notempty and one for Notfull
VI. Atomic operation
Atomicinteger Atomiclong Atomicboolean
Seven, executors
Newcachedthreadpool ()
Create a thread pool that can create new threads as needed, but reuse them when previously constructed threads are available.
Newfixedthreadpool (int nthreads)
Create a thread pool that reuses the number of fixed threads and run them in a shared, unbounded queue.
Newsinglethreadexecutor ()
Create a Executor that uses a single worker thread to run the thread in a unbounded queue.
Newscheduledthreadpool (int corepoolsize)
Creates a thread pool that can schedule a command to run after a given delay or perform it on a regular basis.
Suitable for: Timed or deferred tasks, scenarios where timeout callbacks are required in asynchronous operations

Concurrent libraries for Jdk5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.