Callable, Future & amp; blocking queue & amp; blocking stack, callablefuture

Source: Internet
Author: User

Callable, Future & blocking queue & blocking stack, callablefuture
Simple Applications of Callable and Future

Before Java 5, the thread does not return values. It often takes a long time to "have" the returned values, and the code is not easy to write. Or simply bypass this hurdle and go another way. Now Java finally has a task that can return values (also called a thread.

The Callable interface must be implemented for a task that can return values. Similarly, a task without a return value must be a Runnable interface. After executing the Callable task, you can obtain a Future Object. On this Object, you can call get to obtain the Object returned by the Callable task.

Code
Public class Test {public static void main (String [] args) throws ExecutionException, InterruptedException {// create a thread pool ExecutorService pool = Executors. newFixedThreadPool (2); // create two tasks with returned values: Callable c1 = new MyCallable ("A"); Callable c2 = new MyCallable ("B "); // execute the task and obtain the Future object Future f1 = pool. submit (c1); Future f2 = pool. submit (c2); // obtain the return value of the task from the Future object and output it to the console System. out. println (">>>" + f1.get (). toString (); System. out. println (">>>" + f2.get (). toString (); // close the thread pool. shutdown () ;}} class MyCallable implements Callable {private String oid; MyCallable (String oid) {this. oid = oid ;}@ Override public Object call () throws Exception {return oid + "content returned by the task ";}}
>>> Content returned by task A >>> content returned by Task B
Blocking simple queue applications

Blocking queue is the content in the new features of Java 5 threads. java defines the interface Java. util. concurrent. BlockingQueue for blocking queues. The concept of blocking queues is,A queue of the specified length. If the queue is full, the operation to add new elements will be blocked and waited until there is space available.. Similarly, when the queue is empty, operations on the Request queue element will also block waiting until there are available elements.

With this function, it opens a convenient channel for implementing the model of multi-thread queuing waiting, which is very useful.

Code
Public class Test {public static void main (String [] args) throws InterruptedException {BlockingQueue bqueue = new ArrayBlockingQueue (20); for (int I = 0; I <30; I ++) {// Add the specified element to this queue. If there is no available space, it will wait (if necessary ). Bqueue. put (I); System. out. println ("added element to the blocked queue:" + I);} System. out. println ("the program stops running till now and is about to exit ----");}}
Element: 0 is added to the blocking queue. element: 1 is added to the blocking queue. element: 2 is added to the blocking queue: 3. added element to the blocking queue: 4. added element to the blocking queue: 5. added element to the blocking queue: 6. added element to the blocking queue: 7. An element is added to the blocking queue: 8. element: 9 is added to the blocking queue. element: 10 is added to the blocking queue: 11 added element to the blocking queue: 12 added element to the blocking queue: 13 added element to the blocking queue: 14 added element to the blocking queue: 15 element added to the blocking queue: 16 element added to the blocking queue: 17 element added to the blocking queue: 18 element added to the blocking queue: 19

It can be seen that when the output is to element 19, it is always in the waiting state, because the queue is full and the program is blocked.

Blocking simple stack applications

Stack blocking is similar to queue blocking. The difference is that the stack is the "first-in-first-out" structure. Each operation is the top of the stack, while the queue is the "first-in-first-out" structure. Each operation is the queue header. Java defines an interface for blocking stacks: java. util. concurrent. BlockingDeque.

Code
Public class Test {public static void main (String [] args) throws InterruptedException {BlockingDeque bDeque = new LinkedBlockingDeque (20); for (int I = 0; I <30; I ++) {// Add the specified element to this blocking stack. If there is no available space, it will wait (if necessary ). BDeque. putFirst (I); System. out. println ("added element to the blocking Stack:" + I);} System. out. println ("the program stops running till now and is about to exit ----");}}
Element: 0 is added to the blocking stack. element: 1 is added to the blocking stack. element: 2 is added to the blocking stack: 3. added element to the blocking Stack: 4. added element to the blocking Stack: 5. added element to the blocking Stack: 6. added element to the blocking Stack: 7. added element to the blocking Stack: 8. added element to the blocking Stack: 9. added element to the blocking Stack: 10. added element to the blocking Stack: 11. element added to the blocking Stack: 12. element added to the blocking Stack: 13. element added to the blocking Stack: 14. element added to the blocking Stack: 15. An element is added to the blocking stack. element: 16 is added to the blocking stack. element: 17 is added to the blocking stack. element: 19 is added to the blocking stack.

The preceding result shows that,The program is not over. Second, it is blocked because the stack is full and the append element operations are blocked.

I am the dividing line of tiantiao

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.