Java Concurrent Programming Combat notes: Task Execution __java

Source: Internet
Author: User

Excutor Framework

The Excutor framework leaves the commit and execution of the task, and the task submission is spread across the program, but the execution policy can be set in one place, such as executing in a thread pool or serial, thread pool fixed length or longer, and all display using the new Thread (Task) Place should be considered under the Excutor framework

the task of carrying results callable and future

There are three ways to build a thread to perform a task, 1. Inherit thread. 2. Implement Runnable interface. 3. Implement callable interface. The callable abstraction can have a return value that represents the result of the task, submitting the callable task in Excutor, returning the future object, the future object wrapping the lifecycle of the entire task, and getting the return value.

Task Execution Exception

Future get gets callable execution result, throws in ' Te ' Rru ' P ' tedexception,excutionexception,can ' CE ' lationexception,timeoutexception

When an exception is thrown in a task, the call to Future's get gets the result, throws Excuteexception, and can invoke E.getcause to get the real exception.

Completionservice

If you have more than one parallel task, call the corresponding future's get when you obtain the result, if you need to finish first, then this method is not possible. You can use Completionservice, where an excutor is associated to submit a task, and a Completionservice take method is invoked to return the future structure of the completed task

set a time limit for a task

The future Get method can set timeout and call the Cancel method (the Cancel method is generally invoked after timeout)

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.