Home » Java programming language

Differences between submit() and execute() methods in Java

submit() vs execute() methods in Java: Here, we are going to learn what are the differences between submit() and execute() method in Java programming language?
Submitted by Preeti Jain, on August 08, 2019

submit() vs execute() methods in Java

Here, we will see how submit() differs from execute() method in Java?

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

Here, we will see how submit() differs from execute() method in Java?

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:

// 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.