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

20 Jun, 2021
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.  
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        // previouslu 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: 
absoluteastha
Java - util package
Java-concurrent-package
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Similarities and Difference between Java and C++
Difference between Internal and External fragmentation
Difference between Compile-time and Run-time Polymorphism in Java
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java