Difference between ExecutorService.submit() and Executor.execute() methods in Java?
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.
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.
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.
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.
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:
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.