The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java.util.concurrent package. It defines methods that execute the threads that return results, a set of threads and that determine the shutdown status. In this article, we will see the difference between the two such methods called execute() and submit().
In Java, in order to perform asynchronous tasks, the runnable interface is implemented. In order to do this, one such interface available is the Executor interface. The executor interface contains the execute() method. Apart from that, there is another interface available which is the ExecutorService interface which extends the executor interface. This interface contains the submit() method. The following image illustrates the relationship between these two interfaces.
Execute Method: This function executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation. This method is a void method meaning it doesn’t return any function. Once the task is assigned in the execute() method, we won’t get any response and we can forget about the task. The following is an implementation of the execute method.
Java
// Java program to demonstrate // the behavior of the // execute() method import java.util.concurrent.*; public class GFG { public static void main(String[] args) throws Exception { // Creating the object of the // Executor Service ExecutorService executorService = Executors.newSingleThreadExecutor(); // execute() method cannot return // anything because it's return type // is void. // By using execute(), we are accepting // a Runnable task executorService.execute( new Runnable() { // Override the run method public void run() { System.out.println( "This is execute() " + "method example" ); } }); // This method performs all the // previously submitted tasks // before termination executorService.shutdown(); } } |
Output:
Submit Method: This function executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation. Unlike the execute method, this method returns a future. In Java, the future represents the result of an asynchronous computation. The future object is used to handle the task after the execution has started. Therefore, when we need the result of the execution, then we can use the submit() method of the future object. In order to get the result, we can use the get() methods on the Future. The get() method returns an object if we call the get() method before the task has completed, it will block until the result is ready and may throw checked exception or if the task is completed, then the future object holds a result which is returned which can then be used later. The following is an implementation of the submit method:
Java
// Java program to demonstrate // the behavior of the // submit() method import java.util.concurrent.*; public class GFG { public static void main(String[] args) throws Exception { // Creating the object of the // Executor service interface ExecutorService executorService = Executors.newFixedThreadPool( 1 ); // submit() method can return the // result of the computation // because it has a return type of Future. // By using submit(), we are // accepting a Callable task Future obj = executorService.submit( new Callable() { // Overriding the call method public Object call() { System.out.println( "This is submit() " + "method example" ); return "Returning Callable " + "Task Result" ; } }); // This method will return the result // if the task has finished perfectly. // The submit() method returns a // Java Future object which is // used to check when the Runnable // has completed. // As it implements Future, // get() method is called // to get the result System.out.println(obj.get()); executorService.shutdown(); } } |
Output:
The following table demonstrates the difference between the execute method and the submit method:
Execute Method | Submit Method |
---|---|
This method is declared in the Executor interface. | This method is declared in the ExecutorService interface. |
This method can accept only runnable task. | This method can accept both runnable and callable tasks. |
This method has a return type of void. | This method has a return type of Future. |
This method is used when we are not bothered about the result but want the code to run in parallel by the worker threads of the thread pool. | This method is used when we care about the result and need it from the task which has been executed. |