
What is difference between ExecutorService submit and execute method
Carvia Tech | December 04, 2019 | 2 min read | 17 views
A common multi-threading question that’s generally asked in investment banking interviews (Barclays, Citibank, Morgan Stanley). |
Lets first see the relation between ExecutorService and Executor interface.

-
The execute() method is declared in Executor interface while submit() method is declared in ExecutorService interface.
-
The submit() method can accept both Runnable as well as Callable tasks, but execute() method can only accept a Runnable Task.
-
execute() method returns void while submit() method returns Future object. Using future you can cancel the execution or retrieve the results of computation done by runnable/callable task.
-
There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don’t have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked exception or not, is then part of the task’s return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will re-throw this exception, wrapped in an ExecutionException.
Finally, a working example of ExecutorService.
import java.util.concurrent.*;
public class ExecutorExample {
public void example() {
ExecutorService executor = Executors.newFixedThreadPool(2);
Callable<String> futureTask =
new Callable<String>() {
@Override
public String call() throws Exception {
return searcher.search(target);
}
};
final Future<String> future = executor.submit(futureTask);
try {
String result = future.get();
System.out.println("result = " + result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
executor.shutdownNow();
}
public static void main(String[] args) {
ExecutorExample test = new ExecutorExample();
test.example();
}
}
Top articles in this category:
- What is difference between sleep() and wait() method in Java?
- What is difference between Vector and ArrayList, which one shall be preferred
- What is difference between HashMap and HashSet
- Difference between Callable and Runnable Interface
- Difference between Implementing Runnable and Extending Thread
- What is purpose of Collections.unmodifiableCollection
- Difference between HashMap, LinkedHashMap and TreeMap
Find more on this topic:
Subscribe to Interview Questions
Recommended books for interview preparation:
Similar Posts
- Code review checklist for Java developers
- Count word frequency in Java
- Secure OTP generation in Java
- HmacSHA256 Signature in Java
- Submit Form with Java 11 HttpClient - Kotlin
- Java Exception Class Hierarchy
- Http download using Java NIO FileChannel
- CRC32 checksum calculation Java NIO
- Precision and scale for a Double in java
- Difference between HashMap, LinkedHashMap and TreeMap