Home »
Java programming language
Java - Difference Between submit() and execute() Methods
Java | submit() Vs. execute() methods: In this tutorial, we will learn about the submit() and execute() methods in Java and the differences between submit() and execute() methods.
By Preeti Jain Last updated : March 30, 2024
submit() Method
Example: submit() to accept Runnable task
// Java program to demonstrate the behavior of submit() method
// of ExecutorService interface
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class SubmitATaskBySubmitMethod {
public static void main(String[] args) throws Exception {
// Allow one thread from ThreadPool
ExecutorService exe_ser = Executors.newFixedThreadPool(1);
// By using submit() we are accepting Runnable task
Future f = exe_ser.submit(new Runnable() {
// Override run() method and will define a job inside it
public void run() {
System.out.println("Submitting a task by using submit() method");
}
});
// This method will return null if task has finished perfectly
// (i.e. without any error)
System.out.println(f.get());
}
}
Output
Submitting a task by using submit() method
null
Example: submit() to accept Callable task
// Java program to demonstrate the behavior of submit() method
// of ExecutorService interface
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class SubmitATaskBySubmitMethod {
public static void main(String[] args) throws Exception {
// Allow one thread from ThreadPool
ExecutorService exe_ser = Executors.newFixedThreadPool(1);
// By using submit() we are accepting Callable task
Future f = exe_ser.submit(new Callable() {
// Override call() method and will define a job inside it
public Object call() {
System.out.println("Submitting a Callable task by using submit() method");
return "Callable Task";
}
});
// This method will return null if task has finished perfectly
// (i.e. without any error)
System.out.println(f.get());
}
}
Output
Submitting a Callable task by using submit() method
Callable Task
execute() Method
Example of execute() Method
// Java program to demonstrate the behavior of execute() method
// of Executor interface
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SubmitATaskByExecuteMethod {
public static void main(String[] args) throws Exception {
// Allow one thread from ThreadPool
ExecutorService exe_ser = Executors.newFixedThreadPool(1);
// By using execute() we are accepting Runnable task
exe_ser.execute(new Runnable() {
// Override run() method and will define a job inside it
public void run() {
System.out.println("Submitting a task by using execute() method");
}
});
// This method performs previous submitted task before termination
exe_ser.shutdown();
}
}
Output
Submitting a task by using execute() method
Difference Between submit() and execute() Methods in Java
The following table shows the differences between submit() and execute() methods in Java:
Sr. No. |
submit() Method |
execute() Method |
1. |
submit() method is used with Callable or Runnable tasks. |
execute() method is used with Runnable tasks only. |
2. |
submit() method returns a Future representing the task. |
execute() method returns void; does not return any value. |
3. |
submit() method requires explicit handling of exceptions. |
In execute() method the exceptions are not handled explicitly. |
4. |
submit() method has an synchronous execution. |
execute() method has a synchronous execution. |
5. |
submit() method returns immediately without blocking. |
execute() method blocks until the task is completed. |
6. |
submit() method was introduced in ExecutorService interface. |
execute() method is a part of the Executor interface. |
7. |
submit() method allows monitoring and control of tasks. |
execute() method provides basic execution without much control. |
8. |
submit() method is more flexible in handling tasks. |
execute() method is limited to executing Runnable tasks. |
9. |
While using the submit() method, errors need to be handled manually. |
While using the execute() method , rrrors are handled internally by the executor. |
10. |
submit() method facilitates tracking task completion. |
In execute() method , there is no built-in mechanism for tracking task completion. |