Java Concurrent Programming (Submit method in Executorservice) to explain __ programming

Source: Internet
Author: User

submit (): This method receives the Runnable or callable object as an input parameter (this method inputs parameters refer to the previous Article Java thread Pool task submission (Runnable, callable, Futuretask). This article mainly talks about the return value of the Submit method and the use of the return value, and returns a future object. The call method in the task submitted by the Submit method returns an Integer, then the Submit method returns future<integer>; if the call method returns float, then the Submit method returns future< Float>;call method returns the List<integer>, then the Submit method returns FUTURE<LIST<INTEGER>> The return value of the Submit method is referenced using a reference of the corresponding type, depending on which one is returned. For example:

The call method returns a type of List<integer>, so the Submit method returns a value type of future<list<integer>>.

The results of the above sample program output are as follows:

Use of the return value of the Submit method, as we already know, the actual return value is list<integer>, but is encapsulated as FUTURE<LIST<INTEGER>> we can pass the Get () method to remove the List<integer> object from the inside. In the previous article (Java Thread Pool task submission (Runnable, callable, Futuretask)  ) We have explained that the Submit method is actually pseudo return (Retrospective: The Submit method, once executed, will immediately produce the return value of the future type, and the task just submitted may be queued in the task queue on the online pool). So in this case, when we call the Get () method on the future object returned by the Submit method to take out the inside list<integer>, the thread that the Get () method is in will block (blocking the call when the Get () method of the future Class). Until the task submitted by submit is actually executed in the pool and the result is produced.

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.