Java Multithreaded Learning-java.util.concurrent detailed (iv) Exchanger

Source: Internet
Author: User

Reproduced in: http://janeky.iteye.com/blog/769965

Let's start by learning more about this class in the JDK1.5 API:

"Cancels the asynchronous computation. This class provides a basic implementation of the future, taking advantage of the method of starting and canceling the calculation, the method of querying whether the calculation is complete, and the method of obtaining the result of the calculation. The result is only available when the calculation is complete, and if the calculation is not completed, the Get method is blocked. Once the calculation is complete, you cannot start or cancel the calculation again.
You can use Futuretask to wrap callable or Runnable objects. Because Futuretask implements the Runnable, Futuretask can be submitted to Executor for execution.
In addition to being a standalone class, this class provides protected functionality that can be useful when creating custom task classes. “

Application Example: Our algorithm has a very time-consuming operation, in programming, we want to separate it into a module, called when it is immediately returned, and can be canceled at any time

Package Com.winterbe.java8.samples.concurrent;import Java.util.concurrent.callable;import Java.util.concurrent.executionexception;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.futuretask;public class TestFutureTask {public static void Main (string[] args) {Executorservice exec = Executors.newcachedthreadpool (); futuretask<string> task = new futuretask<string> (new callable<string> () {// The Futruetask constructor parameter is a callable interface @overridepublic String call () throws Exception {Thread.Sleep (8 *); return Thread.CurrentThread (). GetName () + ": 0";//This can be an asynchronous operation}}); futuretask<string> Task1 = new futuretask<string> (new callable<string> () {// The Futruetask constructor parameter is a callable interface @overridepublic String call () throws Exception {Thread.Sleep (4 *); return Thread.CurrentThread (). GetName () + ": 1";//This can be an asynchronous operation}); try {Exec.execute (Task);// Futuretask is actually a thread thread.sleep (1 * 1000);//Sleep 2S, equivalent to wait for the method to execute 2S, if not completed, then cancel the execution of the threadif (!task.isdone ()) {System.out.println ("time out,cancel the" + Thread.CurrentThread (). GetName () + ": 0" + "."); Task.cancel (TRUE);//Cancels}if (!task.iscancelled ()) {String result = Task.get ();//Gets the result of an asynchronous calculation, if not returned, will always block waiting System.out.printf ("get:%s%n", result);} Exec.execute (TASK1); Thread.Sleep (4 * +), if (!task1.isdone ()) {System.out.println ("time out,cancel the" + Thread.CurrentThread (). GetName () + ": 1" + "."); Task1.cancel (TRUE);//Cancel}if (!task1.iscancelled ()) {String result1 = Task1.get (); System.out.printf ("get:%s%n", RESULT1);}} catch (Interruptedexception e) {e.printstacktrace ();} catch (Executionexception e) {e.printstacktrace ()}}}

Summary: Futuretask is actually a new thread that executes independently, allowing the thread to have a return value that facilitates program writing

Java Multithreaded Learning-java.util.concurrent detailed (iv) Exchanger

Related Article

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.