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 method 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.
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:
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.