Java Concurrency Futuretask

Source: Internet
Author: User

Futuretask implements the runnable and future interfaces and is an asynchronous task that can be canceled. 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. It is more important to implement a callback method that completes a task. This is good.

@Test Public voidtest2 () {myfuturetask task=NewMyfuturetask (NewCallable<string>() {@Override PublicString Call ()throwsException {return"Gaoxing";                }        }); NewThread (Task). Start (); }}classMyfuturetaskextendsfuturetask{ PublicMyfuturetask (callable arg0) {Super(arg0); }    protected voidDone () {Try{System.out.println ( This. Get ()); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(executionexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Java Concurrency Futuretask

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.