5 things about java.util.concurrent you don't know, part 1th

Source: Internet
Author: User

Multithreaded programming through concurrent collections

Concurrent Collections is a huge add-on to java™5, but many Java developers ignore them in the arguments about annotations and generics. In addition (or, to be more honest), many developers avoid using this packet because they think it must be complex, just as it solves the problem.

In fact, Java.util.concurrent contains many classes that can effectively solve common concurrency problems without complex operations. Read this article to learn how the Java.util.concurrent classes, such as Copyonwritearraylist and Blockingqueue, can help you solve the thorny problems of multithreaded programming.

1. Timeunit

Although not essentially a collections class, the Java.util.concurrent.TimeUnit enumeration makes the code easier to read. Use Timeunit to liberate developers who use your methods or APIs from the "tyranny" of milliseconds.

Timeunit includes all time units, from milliseconds and microseconds to days and HOURS, which means it can handle almost any type of time range a developer needs. At the same time, because the conversion method is declared on the enumeration, it becomes even easier to convert HOURS back to milliseconds when time is accelerating.

2. Copyonwritearraylist

Creating a fresh copy of an array is an expensive operation, whether from time to memory overhead, and therefore rarely considered in common use; developers often resort to ArrayList with synchronization. However, this is also a costly choice, because every time you iterate over the collection, you have to synchronize all the actions, including read and write, to ensure consistency.

This then returns the cost structure back to the scenario where many readers are reading ArrayList, but few are going to change it.

Copyonwritearraylist is a clever little baby that can solve the problem. Its Javadoc defines copyonwritearraylist as a "ArrayList thread-safety variant, in which all variable operations (additions, settings, etc.) can be implemented by copying a completely new array."

The collection internally copies its contents to a new array that has not been modified, so that the reader does not produce synchronization costs when accessing the array contents (since they are never manipulated on volatile data).

In essence, copyonwritearraylist to deal with ArrayList Let's fail this scenario is ideal: read frequently, but rarely have a collection of write operations, such as the Listeners of JavaBean events.

3. Blockingqueue

The Blockingqueue interface indicates that it is a Queue, meaning its items are stored in first-in-first-out (FIFO) order. Items inserted in a particular order are retrieved in the same order-but with additional guarantees, any attempt to retrieve an item from an empty queue blocks the calling thread until the item is ready to be retrieved. Similarly, attempts to insert an item into a full queue can also cause blocking of the calling thread until the queue's storage space is available.

Blockingqueue cleanly solves the problem of how to "pass" an item that a thread collects to another thread for processing, regardless of synchronization issues. The guarded Blocks trial version of Java Tutorial is a good example. It builds a single slot-bound cache, where new items are available and slots are ready to accept new items, using manual synchronization and Wait ()/notifyall () to send letters between threads.

Although the code in the guarded Blocks tutorial works, it is time-consuming, confusing, and not entirely intuitive. Back to the Java platform earlier, yes, Java developers had to get entangled in this code, but it's 2010-and it's not going to improve?

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.