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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Java.util.concurrent.ExecutorService interface with Examples
- Execute main() multiple times without using any other function or condition or recursion in Java
- Difference between Method Overloading and Method Overriding in Java
- Difference between == and .equals() method in Java
- Difference between Stream.of() and Arrays.stream() method in Java
- Difference between static and non-static method in Java
- Difference between a Java Application and a Java Applet
- Difference between Core Java and Advanced Java
- Difference between Method Overloading and Method Overriding in Python
- Difference between Java and Core Java
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.ArrayList.addall() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.