Java concurrent collection class

Source: Internet
Author: User

The first associated collection class in the Java class library isHashtable.HashtableIt provides an easy-to-use, thread-safe, and associated map function. However, the price for thread security is ――HashtableAll methods are synchronized.HashtableSuccessorHashmapIs a part of the collection framework in JDK. It provides an unsynchronized base class and a synchronous package.Collections. synchronizedmap
To solve the thread security problem. By separating basic functions from thread security,Collections. synchronizedmapUsers who need to be synchronized can have synchronization, but users who do not need to be synchronized do not have to pay the price for synchronization.

Synchronized set packageSynchronizedmapAndSynchronizedlistIs also calledConditional thread security-All single operations are thread-safe, but the operation sequence composed of multiple operations may result in data contention, because the control flow in the Operation Sequence depends on the result of the previous operation. For example, for such a function, put-if-absent statement block-if an entry is notMap. This function is enabledContainskey ()
Method andPut ()Methods. To ensure atomic operations, you must add a synchronization lock to the statement block.

Queue Interface

The Java. util package provides a new basic interface for the set: Java. util. queue, which is a first-in-first-out (FIFO) data structure.

Priorityqueue class

Blockingqueue Interface

A blocking queue is essentially a one-bit distorted FIFO data structure. It provides blocking put and take methods. If the queue is full, the put method will be blocked until there is space available. If the queue is empty, the take method will be blocked until there are available elements. The length of a queue can be limited or unlimited. The infinite queue will never be full, so its put method will never be blocked. The blocking queue supports the producer-consumer design mode. Blockingqueue can use any number of producers and consumers, thus simplifying the implementation of producer-consumer design.

Several major implementation classes of the blockingqueue interface:

  • Arrayblockingqueue: A Bounded queue supported by arrays.
  • Linkedblockingqueue: An optional bounded queue supported by a linked node.
  • Priorityblockingqueue: An unbounded priority queue supported by the priority heap.
  • Delayqueue: A time-based scheduling queue supported by the priority heap.
  • synchronousqueue : A simple aggregation mechanism using the blockingqueue interface.

    priorityblockingqueue is a queue with unlimited capacity, it uses the comparable sorting order of the contained elements to maintain the elements in a logical order. It can be seen as a possible substitute for treeset . For elements that do not have a natural order, you can provide a comparator for the constructor. However, for priorityblockingqueue, there is a special note: from
    iterator () the returned iterator instance does not have a priority. If all elements must be traversed in the order of priority, use the toarray () method and sort them by yourself, such as arrays. sort (PQ. toarray () .

    synchronousqueue is not a real queue because it does not maintain any storage space for queue elements. However, it maintains a list of queued threads waiting for elements to be added to or removed from the queue. It directly transfers data, reducing the delay time for moving data between producers and consumers. Direct handover will also bring more feedback about the task status to the producer: When the handover is accepted, it will know that the consumer has obtained the task. Because synchronousqueue does not have the storage capability, put and take will be blocked until another thread is ready to participate in the handover. It is suitable for situations where consumers are sufficient.

    copyonwritearraylist and copyonwritearrayset , it is most suitable for read operations that generally exceed write operations.

  • 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.