Java中的Submit()与execute()方法 (submit() vs execute() methods in Java)
Here, we will see how submit() differs from execute() method in Java?
在这里,我们将看到Submit()与Java中的execute()方法有何不同?
Submit()方法 (submit() Method)
This method is available in java.util.concurrent package.
此方法在java.util.concurrent包中可用。
submit() method is used to submit a task to ThreadPool.
Submit()方法用于将任务提交到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()方法接受Runnable或Callable任务(即,该方法仅接受一个参数,即Runnable或Callable)。
submit() is a static method of ExecutorService interface so this method is accessible with the classname too.
Submit()是ExecutorService接口的静态方法,因此也可以使用类名访问此方法。
The return type of this method is a Future object so it return Future type object which contains calculation of pending results.
此方法的返回类型是Future对象,因此它返回包含未决结果计算的Future类型对象。
ExecutorService interface is a Child interface of Executor.
ExecutorService接口是Executor的子接口。
The syntax of submit() method is given below:
下面给出了commit()方法的语法:
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.
如果我们要计算大量计算,例如计算pie的值等并返回计算结果,则应使用Submit() 。
Example: Submit() to accept Runnable task
示例:Submit()接受可运行任务
// 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
示例:Submit()接受可调用任务
// 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?
在这里,我们将看到Submit()与Java中的execute()方法有何不同?
execute()方法 (execute() Method)
This method is available in java.util.concurrent package.
此方法在java.util.concurrent包中可用。
execute() method is used to execute a task to ThreadPool.
execute()方法用于执行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()方法仅接受Runnable(即,此方法仅接受一个参数,并且它是Runnable,并且不接受诸如Submit()方法之类的Callable任务)。
execute() is a static method of Executor interface so this method is accessible with the class name too.
execute()是Executor接口的静态方法,因此也可以使用类名访问此方法。
The return type of this method is void so it returns nothing and it will not give any results.
此方法的返回类型为void,因此不返回任何内容,也不会给出任何结果。
Executor interface is a parent interface of ExecutorService.
Executor接口是ExecutorService的父接口。
Executor interface declared execute(Runnable) method whose main purpose is to separate the task from its execution.
执行程序接口声明了execute(Runnable)方法,其主要目的是将任务与其执行分开。
The syntax of the execute() method is given below:
下面给出了execute()方法的语法:
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.
如果要通过线程池的工作线程执行代码,并且不返回任何内容,则应使用execute() 。
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
翻译自: https://www.includehelp.com/java/differences-between-submit-and-execute-methods-in-java.aspx