About Java util. Concurrent five things you don’t know — turn
•
Java
Part 1
http://www.ibm.com/developerworks/cn/java/j-5things4.html
Concurrent collections is Java ™ 5, but many Java developers ignore them in the dispute over annotations and generics. In addition (or to be honest), many developers avoid using this package because they think it must be complex, just like the problem it solves.
In fact, Java util. Concurrent contains many classes, which can effectively solve common concurrency problems without complex processes. Read this article to learn about Java util. How can concurrent classes, such as copyonwritearraylist and BlockingQueue, help you solve the thorny problems of multithreaded programming.
Although it is not a collections class in essence, Java util. concurrent. The timeunit enumeration makes the code easier to read. Using timeunit frees developers who use your methods or APIs from millisecond "tyranny". Timeunit includes all time units, from milliseconds and microseconds to days and hours, which means that it can handle almost all the time range types required by a developer. At the same time, because the conversion method is declared on the enumeration, it becomes even easier to convert hours back to milliseconds when the time is accelerated. Creating a new copy of an array is too expensive, both in terms of time and memory overhead, so it is rarely considered in common use; Developers often turn to using synchronized ArrayList. However, this is also a costly option, because whenever you iterate across the collection content, you have to synchronize all operations, including read and write, to ensure consistency. This brings the cost structure back to such a scenario: many readers are reading the ArrayList, but few people will modify it. Copyonwritearraylist is a clever little baby that can solve this problem. Its Javadoc defines copyonwritearraylist as a "thread safe variant of ArrayList. In this variant, all changeable operations (addition, setting, etc.) can be realized by copying a new array". The collection copies its contents internally to a new array without modification, so that readers will not incur synchronization costs when accessing the contents of the array (because they never operate on volatile data). In essence, copyonwritearraylist is very suitable for dealing with situations where ArrayList often makes us fail: collections with frequent reads but few write operations, such as listeners of JavaBean events. The BlockingQueue interface indicates that it is a queue, which means that its items are first in first out (FIFO) sequential storage. Items inserted in a particular order are retrieved in the same order - but additional assurance is required that any attempt to retrieve an item from an empty queue will block the calling thread until the item is ready to be retrieved. Similarly, attempts to insert an item into a full queue will also block the calling thread until the storage space of the queue is available. BlockingQueue E cleanly solves the problem of how to "pass" the items collected by one thread to another thread for processing, without considering the synchronization problem. The guarded blocks trial version of java tutorial is a good example. It builds a single slot bound cache that uses manual synchronization and wait() / notifyall() to send messages between threads when new items are available and the slot is ready to accept new items. (see for details.) although the code in the guarded blocks tutorial is effective, it is time-consuming, confusing, and not completely intuitive. Back to the Java platform earlier, yes, Java developers had to get entangled in this code; but now it's 2010 - hasn't the situation improved? Listing 1 shows a rewritten version of the guarded blocks code, which I use An arrayblockingqueue is instead of a handwritten drop. class Producer implements Runnable { private BlockingQueue drop; List messages = Arrays.asList( "Mares eat oats","Does eat oats","Little lambs eat ivy","Wouldn't you eat ivy too?"); public Producer(BlockingQueue
d) { this.drop = d; }
d) { this.drop = d; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码