Wednesday, September 13, 2023

Difference between ExecutorService.submit() and Executor.execute() methods in Java?

What is the difference between Executor.submit() and Executor.execute() method in Java? This is one of the good multi-threading questions for experienced Java programmers, mostly asked in Investment Banks like Barclays, Deutsche Bank, or Citibank. A main difference between the submit() and execute() method is that ExecuterService.submit()can return the result of computation because it has a return type of Future, but the execute() method cannot return anything because return type is void. The core interface in Java 1.5's Executor framework is the Executor interface which defines the execute(Runnable task) method, whose primary purpose is to separate the task from its execution.

Any task submitted to Executor can be executed by the same thread, a worker thread from a thread pool, or any other thread.

On the other hand, the submit() method is defined in the ExecutorService interface which is a sub-interface of Executor and adds the functionality of terminating the thread pool, along with adding the submit() method which can accept a Callable task and return a result of the computation.
This was the fundamental difference between the submit() and execute() method of the ExecutorService interface, let's see a couple of more points to answer this question better.

Btw, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at the Java Multithreading, Concurrency, and Performance Optimization course by Michael Pogrebinsky on Udemy. It's an advanced course to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance.

Difference between ExecutorService.submit() and Executor.execute() methods in Java?





Executor.execute() vs ExecutorService.submit() method

As I told in the first paragraph the key difference between the execute() and submit() method is that the former cannot return the result but later can result in computation. Before seeing more difference, let's see some similarities between the execute() and submit() as well:


1) Both submit() and execute() methods are used to submit a task to the Executor framework for asynchronous execution.

2) Both submit() and execute() can accept a Runnable task.

3) You can access submit() and execute() from the ExecutorService interface because it also extends the Executor interface which declares the execute() method.

You can also see Multithreading and Parallel Computing in Java for more details on how to thread pool works and how tasks are allocated between threads depending upon different types of thread pools.





Difference between the submit() and execute() method in Java Concurrency

Apart from the fact that the submit() method can return output and execute() cannot, the following are other notable differences between these two key methods of the Executor framework of Java 5.

1) The submit() can accept both Runnable and Callable tasks but execute() can only accept the Runnable task.

2) The submit() method is declared in the ExecutorService interface while the execute() method is declared in the Executor interface.

3) The return type of submit() method is a Future object but the return type of execute() method is void.

Btw, Cay S. Horstmann has also covered this essential topic in good detail in his classic book, Core Java Volume 1 - Fundamentals, 10th Edition. You can refer to that book for further reading on this topic.

difference between submit and execute methods in Java




When to use submit() and execute() method in Java

Once you understand the difference between Executor.execute() and ExecutorService.submit() method you have the knowledge to decide when to use submit() and when to use the execute() method.

In general, if you are doing computational tasks e.g. calculating some risk stats, calculating the factorial of large numbers, or doing some time-consuming computation e which results in some value then use the submit() method. It immediately returns a Future object, which can be later queried to get the value of computation by calling the get() method.

Remember, get() is a blocking call so always call the version which accepts a timeout. While you can use the execute() method if you just want your code to be run in parallel by worker threads of the thread pool.

Here is a nice summary of key differences between submit() vs execute() methods in Java:

ExecutorService.submit() vs Executor.execute()


That's all about difference between ExecutorService.submit() and Executor.execute() method in Java. Remember, the key difference between them is that submit() return a Future but execute() return nothing.

The thread pools created by the Executors class always return a reference of ExecutorService, which provides access to both the submit() and execute() method as it also extends the Executor interface, which is a source of execute() method.

Use submit() if you're doing computation like calculating the value of pie, and use execute() if you just want the code to be run in parallel by worker threads of the thread pool.

Further Learning
Multithreading and Parallel Computing in Java
Java Concurrency in Practice - The Book
Applying Concurrency and Multi-threading to Common Java Patterns
Java Concurrency in Practice Course by Heinz Kabutz


Similar frequently asked thread interview questions you may like
  • Difference between start() and run() method in Java? (answer)
  • How to join multiple threads in Java? (answer)
  • How to stop a thread in Java? (answer)
  • Top 50 Thread Interview Questions for experienced programmers (list)
  • How to avoid deadlock in Java? (answer)
  • 10 Free Java Courses for Beginners and Intermediate developers (courses)
  • How Happens Before works in Java?  (answer)
  • 10 Java Multithreading and Concurrency Best Practices (article)
  • Understanding the flow of data and code in Java program (answer)
  • Top 5 Books to Master Concurrency in Java (books)
  • Is Java Concurrency in Practice still valid? (answer)
  • Difference between CyclicBarrier and CountDownLatch in Java? (answer)
  • 10 Tips to become a better Java Developer (tips)
  • How to solve the producer-consumer problem using a blocking queue in Java? (solution)
  • How to do inter-thread communication in Java using wait-notify? (answer)
  • Top 5 Courses to Learn Java Multithreading in-depth (courses)

Thanks for reading this article of far. If you find this submit() vs execute() method comparison useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note, happy to clarify any doubt you may have. 

P. S. - If you are a Java beginner and want to learn multithreading and concurrency and looking for some free courses to start with then you can also check out this free Java Multithreading course on Udemy. It is a good free course to learn  Java Concurrency as well.

7 comments:

  1. I have a doubt with point number 2 Both submit() and execute() can accept a Runnable task(using java 8).

    I think submit method takes callable and execute method takes Runnable only. Please clarify if I have misunderstood the point.

    ReplyDelete
  2. ExecutorService.submit() can accept Runnable task. However the returned Future object will return null when you tried to call get() method

    ReplyDelete
  3. It can take both runnable and callable. If you check the java docs you will also notice that the submit method also accepts a runnable and a result attribute

    ReplyDelete
  4. ExecutorService.execute() method is available in API kindly recheck.

    ReplyDelete
  5. Another difference would be when we call execute method and any unchecked exception comes up in the run method then it will print stack trace and exit. While in submit method the exception is wrapped around the Future object and throws exception only when we call get method of future. the latter case can be controlled by overriding after Execute method of executor and handling the exception properly/\.

    ReplyDelete
  6. @Anonymous - I don't see any execute() method in ExecutorService interface.
    https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

    ReplyDelete
    Replies
    1. Executor has execute method. As ExecutorService extends Executor interface, it will also reuse the methods from Executor.

      Delete