The difference between submit and execute in multithreaded Executorservice

Source: Internet
Author: User
Tags stack trace

Both submit and Execute are Executorservice methods, all of which are added threads to the thread pool. Difference

Three differences:

1, the received parameters are different

2. Submit has a return value, and execute has no

Method Submit extends base method Executor.execute by creating and returning a future so can be used to cancel execution and/or wait for completion.

Using the example of the return value, for example, I have a lot of tasks to do validation, I want all the tasks to be executed, and then each task tells me the result of its execution, whether it succeeds or fails, and if it fails, what the reason is. Then I can synthesize all the reasons for failure and send them to the caller.

Personally think that the cancel execution is not very useful, there is little need to cancel execution.

And the greatest use should be the 2nd.

3, submit convenient exception processing

There is a difference if looking at exception handling. If your tasks throws an exception and if it is submitted with this exception would go to the execute uncaught exception hand Ler (when you don't have provided one explicitly, the default one would just print the stack trace to System.err ). If you submitted submit the task with any thrown exception, checked or not, was then part of the task ' s return stat us. For a task is submitted with submit and that terminates with an exception, the would Future.get rethrow this exception, WRA Pped in an ExecutionException .

This means that if you throw checked or unchecked exception in your task, and you want callers outside to be able to perceive these exception and make timely processing, then you need to use submit, Throws an exception by capturing Future.get.

When we examine the handling of exceptions, we find another difference. When you throw an exception with the task submitted by execute, this exception is handled by the unhandled exception handling process (Uncaught exception handler), when you do not explicitly specify an exception handler, By default, the error stack is printed only by System.err. When you submit a task with submit, the task throws an exception (regardless of whether it is a run-time exception), and the exception is part of the task's return object. In such a case, when you call the Future.get () method, this method will re-throw the exception and will be wrapped using executionexception

The difference between submit and execute in multithreaded Executorservice

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.