ExecutorService.submit和ExecutorService.execute之间有什么区别吗?

11 浏览
0 Comments

ExecutorService.submit和ExecutorService.execute之间有什么区别吗?

我认为两者都会在将来的某个时间执行线程任务,为什么有两种方法?

0
0 Comments

execute does not return any value, so you cannot obtain any information about the task after it is complete.

The difference between ExecutorService.submit and ExecutorService.execute arises from the need to have more control over the tasks submitted to an ExecutorService.

ExecutorService.submit() method returns a Future object that represents the pending completion of the task, allowing you to obtain the result of the task or cancel it if necessary. The Future object can be used to check if the task is done, retrieve the result of the task, or block until the task is complete.

On the other hand, ExecutorService.execute() method does not return any value. It simply submits the task for execution and does not provide any way to obtain the result or check the status of the task.

Here is an example to demonstrate the difference between ExecutorService.submit and ExecutorService.execute:

ExecutorService executor = Executors.newFixedThreadPool(5);
// Using submit
Future future = executor.submit(() -> {
    // Perform some task
    return 42;
});
// Using execute
executor.execute(() -> {
    // Perform some task
});
try {
    // Get the result of the task submitted using submit
    Integer result = future.get();
    System.out.println("Result: " + result);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}
executor.shutdown();

In this example, the submit method is used to submit a task that returns an integer value. The execute method is used to submit a task that does not return any value.

After submitting the tasks, we can use the Future object obtained from the submit method to retrieve the result of the task. In this case, the result is 42. On the other hand, there is no way to retrieve the result of the task submitted using the execute method.

To summarize, if you need to obtain the result of a task or check its status, you should use the submit method of ExecutorService. If you do not need to obtain the result or check the status of the task, you can use the execute method.

0
0 Comments

有时候我们在使用ExecutorService接口时会遇到两种不同的方法:execute()和submit()。这两个方法在功能上有一些区别。

首先,execute()方法没有返回值,而submit()方法返回一个Future对象,可以用来获取执行代码的返回值。

execute()方法存在的原因是它是Executor接口的一部分,而ExecutorService接口继承了Executor接口。很可能只是为了有一个非常简单的接口。

然而,如果我们需要获取任务的返回值,那么submit()方法会更加有用。通过submit()方法提交的任务会被封装成一个Callable对象,它可以返回一个结果。我们可以使用Future对象的get()方法来获取返回值,该方法会阻塞直到任务完成并返回结果。

下面是使用execute()方法的示例代码:

ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.execute(() -> System.out.println("Hello, World!"));

下面是使用submit()方法的示例代码:

ExecutorService executorService = Executors.newFixedThreadPool(10);
Future future = executorService.submit(() -> {
    System.out.println("Hello, World!");
    return "Task completed";
});
try {
    String result = future.get();
    System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

在这个例子中,通过submit()方法提交的任务可以返回一个结果,我们可以通过调用future.get()方法来获取这个结果。而通过execute()方法提交的任务没有返回值,因此我们无法获取任务的执行结果。

,execute()方法和submit()方法在功能上有一些区别。如果我们需要获取任务的返回值,那么应该使用submit()方法。如果我们只是要执行一个任务而不关心返回值,那么可以使用execute()方法。

0