Javatpoint Logo
Javatpoint Logo

Difference between ExecutorService execute() and submit() method in Java

The ExecutorService.execute() and submit() methods serve the purpose of submitting tasks to an ExecutorService object. The execute() method accepts a Runnable task, while the submit() method accepts both Runnable and Callable tasks. The execute() method does not have a return value, whereas the submit() method returns a Future object. The Future object can be used to get the task result or to check if the task has finished executing.

The submit() method is generally more flexible than the execute() method. If you need to get the result of a task or if you need to check if a task has finished executing, then you should use the submit() method. However, if you don't need the result of the task or if you don't need to check if the task has finished executing, then you can use the execute() method.

Difference between ExecutorService execute() and submit() method in Java

Execute Method:

The execute() method is a part of the Executor interface in Java. It is employed to submit a task to an Executor object. The Executor interface is a generic interface that takes a Runnable task as an argument. The execute() method does not return any value. The task can be any object that implements the Runnable interface.

The execute() method is a non-blocking method. The "execute()" method does not wait for the task to complete its execution. The execute() method submits the task to the Executor object and then returns. The Executor object will then execute the task at some later time.

The execute() method is useful for submitting tasks that do not need to return any value. For example, you could use the execute() method to submit a task that prints a message to the console.

Filename: ExecuteExample.java

Output:

Hello, world!

Submit Method:

The submit() method is used in Java's ExecutorService interface. The "submit()" method is utilized to submit a task to the ExecutorService for asynchronous execution. When using the submit() method, you can provide a Callable object as input, and it will return a Future object. The Future object can be used to get the task result or to check if the task has finished executing.

The submit() method is a very powerful tool for asynchronous execution in Java. The submit() method allows for the submission of any task to the ExecutorService. The submit() method can also be used to get the task result or to check if the task has finished executing.

Filename: SubmitExample.java

Output:

Hello, world!

Difference between ExecutorService execute() and submit() method

Difference execute() method submit() method
Method Declaration Declared in Executor interface Declared in ExecutorService interface
Task Acceptance Accepts only Runnable tasks Accepts both Runnable and Callable tasks
Return Type Void Future object
Result Retrieval No direct way to obtain the result The future object provides access to the task's result
Exception Handling Exceptions are handled by an uncaught exception handler, or the default handler prints the stack trace to System.err Exceptions are captured and can be retrieved through the Future object using get(), which wraps exceptions in an ExecutionException
Task Cancellation No explicit way to cancel the task The future object provides cancellation capabilities through cancel()
Task Completion No explicit indication of task completion The future object provides methods to check if the task is done (isDone()) and wait for completion (get())






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA