Java Core Technology Volume 1 multi-threaded----Thread-safe Collection (4)

Source: Internet
Author: User

If multithreading is to concurrently modify a data structure, such as a hash table, it can easily break this data structure. A thread may want to start inserting a new element into the table. Assume that you are deprived of control during the process of adjusting the link relationship between buckets of a hash table. If another thread also starts traversing the same linked list, it may use invalid links and cause confusion, throw an exception, or fall into a dead loop.

You can protect shared data structures by providing locks, but it may be easier to choose a thread-safe implementation as an alternative. The blocking queue discussed in the previous article is a thread-safe collection. Next, we discuss some additional thread-safe collections provided by the Java class Library.

  Efficient mapping of tables, collections, and queues

The J Ava.util.concurrent package provides efficient implementation of mapping tables, ordered collections, and queues: Concurrenthashmap, Concurrentskiplistmap, Concurrentskiplistset and Concurrentlinkedqueue.

Java.util.concurrent.concurrentlinkedqueue<e>
    • Concurrentlinkedqueue<e> () Constructs a borderless, non-blocking queue that can be securely accessed by multithreading.
    • Concurrentskiplistset<e> (comparateor<? Super E> Comp) constructs an ordered set that can be accessed securely by multithreading
Java.util.concurrent.concurrentskiplistmap<k,v>
    • Concurrenthashmap<k,v> ()
    • concurrenthashmap<k,v> (int initialcapacity)
    • concurrenthashmap<k,v> (int initialcapacity,float loadfactor,int concurrencylevel)

Parameters: The initial capacity of the Initialcapacity collection. Default Value 16

Loadfactor Control Adjustment: If the average load of each bucket exceeds this factor, the size of the table will be re-adjusted. The default value is 0.75.

Concurrencylevel The estimated number of concurrent writer ends.

    • Concurrentskiplistmap<k,v>
    • Concurrentskiplistset<k,v> (comparator<? Super K) Comp)

Constructs an ordered image table that can be accessed securely by multithreading. The first constructor requires a key to implement the comparable interface.

    • V putifabsent (K key,v value)

If the key does not appear in the image table, the given value is associated with the given key, and Null is returned. Otherwise, returns an existing value that is key to the key.

    • Boolean Remove (K key,v value)

Removes the given key and value and returns true if the given key is associated with the given value. Otherwise, returns false.

    • Boolean replace (K key,v oldvalue,v newvalue)

If the given key is currently associated with oldvalue, it is associated with the newvalue. otherwise returns false

Copy of the Write array

copyonwritearraylist and Copyonwritearrayset are thread-safe collections. all of the modified threads replicate the underlying array. This arrangement is useful if the number of threads iterating over the collection exceeds the number of modified threads. When constructing an iterator, it contains a reference to the current array. If the array is later modified, the iterator still references the old array, but the array of the collection has been replaced. Because the old iterators have a consistent view, Access does not require any synchronization overhead.

Java Core Technology Volume 1 multi-threaded----Thread-safe Collection (4)

Related Article

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.