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

  • This method is available in java.util.concurrent package.
  • submit() method is used to submit a task to ThreadPool.
  • This method is an overloaded method.
  • submit() method accepts task either Runnable or Callable task (i.e This method takes only one argument either it is Runnable or Callable).
  • submit() is a static method of ExecutorService interface so this method is accessible with the classname too.
  • The return type of this method is a Future object so it return Future type object which contains calculation of pending results.
  • ExecutorService interface is a Child interface of Executor.
  • The syntax of submit() method is given below:
    Future f_obj = ExecutorService_obj . submit(new Runnable(){});
    Future f_obj = ExecutorService_obj . submit(new Callable(){});
  • We should go for submit() if we want to calculate a larger number of calculations like calculate the value of pie etc and return results in computation.

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

  • This method is available in java.util.concurrent package.
  • execute() method is used to execute a task to ThreadPool.
  • execute() method accepts only Runnable (i.e This method takes only one argument and it is Runnable and it does not accept Callable task like as submit() method).
  • execute() is a static method of Executor interface so this method is accessible with the class name too.
  • The return type of this method is void so it returns nothing and it will not give any results.
  • Executor interface is a parent interface of ExecutorService.
  • Executor interface declared execute(Runnable) method whose main purpose is to separate the task from its execution.
  • The syntax of the execute() method is given below:
    ExecutorService_obj . execute(new Runnable(){});
  • We should go for execute() if we want to execute our code by worker thread of the Thread pool and does not return anything.

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.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.