Synchronization containers, concurrent containers, blocking queues, double-ended queues

Source: Internet
Author: User
Tags concurrentmodificationexception

One, synchronous container

The main representatives are vectors and Hashtable, and collections.synchronizedxxx and so on.

The granularity of the lock is the whole of the current object.

An iterator is a timely failure that, when found to be modified during an iteration, throws a concurrentmodificationexception.

Second, concurrent containers

The main representatives are Concurrenthashmap, Copyonwritearraylist, Concurrentskiplistmap, Concurrentskiplistset.

The granularity of the lock is scattered, fine-grained, that is, the read and write are used with different locks.

Iterators have weak consistency and can tolerate concurrent modifications without throwing concurrentmodificationexception.

Third, blocking the queue

The main representatives are Linkedblockingqueue, Arrayblockingqueue, Priorityblockingqueue (Comparable,comparator), Synchronousqueue.

provides a blocking put and take method, as well as support for timed offer and poll methods.

Suitable for producer, consumer mode (thread pool and Work queue-executor), but also synchronous container

Four, two-terminal queue

The main representatives are Arraydeque and Linkedblockingdeque.

meaning: Just as the blocking queue applies to the producer consumer model, the double-ended queue also works with another mode, that is, work-out. In producer-consumer design, all consumers share a single task queue, while in a work-in-process, each consumer has its own double-ended queues.

If a consumer completes all the work in his own double-ended queue, he can get a secret job from the end of the other consumer's double-ended queue. Have better scalability, because worker threads do not compete on a single shared task queue .

Most of the time, they are simply accessing their own double-ended queues, which greatly reduces competition. When a worker thread needs to access another queue, it gets work from the tail of the queue instead of the head, thereby further reducing contention on the queue.

Applies to: Web crawler and other tasks

Synchronization containers, concurrent containers, blocking queues, double-ended queues

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.