Java Concurrency Toolkit java.util.concurrent user's Guide __java

Source: Internet
Author: User
Tags throw exception
Java Concurrency Toolkit java.util.concurrent user's GuideTags: multithreading concurrent asynchronous Java.util.concurrentJava multithreading 2015-03-03 09:40 122991 people read comments (20) Collection Report Category: Multithreaded programming (14)

Directory (?) [+] translation Sequence

This guide is based on Jakob Jenkov's latest blog translation, please keep an eye on blog updates: http://tutorials.jenkov.com/java-util-concurrent/index.html.
This guide has been made into a Chinese-English version of the PDF document, interested friends can go to the Java Concurrency Toolkit java.util.concurrent User's Guide in English and Chinese reading version. pdf[with bookmarks.



1. The Java.util.concurrent-java concurrency Toolkit Java 5 Adds a new package to the Java platform, java.util.concurrent package. This package contains a series of classes that make Java concurrent programming simpler and easier. Before this package is added, you need to do it yourself to implement your own related tool classes.
In this article I'll take you one by one to know these classes in the Java.util.concurrent package, and then you can try how to use them in your project. I'm going to use the Java 6 version in this article, and I'm not sure if it's any different from the Java 5 version.
I'm not going to explain the core issues of Java concurrency-the rationale behind it, i.e., if you're interested in those things, refer to the Java Concurrency Guide.
semi-finished Products

This article is largely a "semi-finished product," so please be patient when you find some of the missing classes or interfaces. I'll add them in my spare time.

The Blockingqueue interface in the


2. Blocking queue Blockingqueue java.util.concurrent packet represents a line Cheng Ann the queue where the instance is placed and fetched. In this section I will show you how to use this blockingqueue.
This section does not discuss how to implement one of your own blockingqueue in Java. If you are interested in that, refer to the Java concurrency guide
blockingqueue usage blockingqueue is typically used for one thread production object, while another thread consumes the scene of those objects. The following illustration illustrates this principle:


A thread goes inside, and another thread takes a blockingqueue from inside.
A thread will continue to produce the new object and insert it into the queue until the queue reaches the critical point it can hold. In other words, it is limited. If the blocking queue reaches its critical point, the thread responsible for the production will block when the new object is inserted inside. It will remain blocked until the consuming thread takes an object from the queue. The thread that is responsible for consuming the
will always take the object out of the blocking queue. If a consumer thread tries to extract an object from an empty queue, the consumer thread will be blocked until a production line throws an object into the queue. The
Blockingqueue method Blockingqueue has 4 different sets of methods for inserting, removing, and checking elements in a queue. Each method behaves differently if the requested operation is not immediately executed. These methods are as follows:

Throw exception specific value blocking timeout
Insert Add (o) Offer (O) Put (o) Offer (o, timeout, timeunit)
Removed from Remove (o) Poll (o) Take (o) Poll (timeout, timeunit)
Check Element (O) Peek (o)

Four groups of different ways of behaving are explained:
Throw Exception: If the attempted operation cannot be executed immediately, throw an exception. Specific Values: Returns a specific value (often true/false) if the attempted operation cannot be executed immediately. Blocking: If an attempted operation cannot be executed immediately, the method call will block until it can be executed. Timeout: If an attempted operation cannot be executed immediately, the method call will block until it is able to execute, but the wait time will not exceed the given value. Returns a specific value to tell whether the operation was successful (typically True/false). Cannot insert null into a blockingqueue. If you try to insert a null,blockingqueue will throw a nullpointerexception.
You can access all the elements in the blockingqueue, not just the start and end elements. For example, you put an object in the queue to wait for processing, but your application wants to cancel it. Then you can call the Remove (O) method to remove a particular object from the queue. But that's not very efficient. The data structure based on the queue is not efficient at getting other objects except at the start or end, so try not to use this kind of method unless you really have to.
the realization of BlockingqueueBlockingqueue is an interface and you need to use one of its implementations to use Blockingqueue. Java.util.concurrent has the following implementations of the Blockingqueue interface (Java 6):
Arrayblockingqueue delayqueue linkedblockingqueue priorityblockingqueue synchronousqueue examples of using blockingqueue in JavaHere is an example of using Blockingqueue in Java. This example uses the Arrayblockingqueue implementation of the Blockingqueue interface.
First, the Blockingqueueexample class starts a Producer and a Consumer in two separate threads. Producer inject strings into a shared blockingqueue, and Consumer takes them out.
[Java]  View plain  copy  print? public class blockingqueueexample {          public  Static void main (String[] args)  throws Exception {               blockingqueue queue = new arrayblockingqueue ( 1024);              producer producer =  new producer (queue);           Consumer  Consumer = new consumer (queue);               new thread (producer). Start ();           new  thread (consumer). Start ();               Thread.Sleep (4000);       }  }  
The following is the Producer class. Notice how it sleeps for one second each time you put () the call. This will cause Consumer to block while waiting for objects in the queue.
[Java]View Plain copy print?          public class Producer implements runnable{protected blockingqueue queue = null;       Public Producer (Blockingqueue queue) {this.queue = queue; public void Run () {try {

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.