What is the difference between ExecutorService.submit and ExecutorService.execute in this code in Java?

前端 未结 8 1399
你的背包
你的背包 2021-01-30 00:05

I am learning to use ExectorService to pool threads and send out tasks. I have a simple program below

import java.util.concurrent.Execu         


        
8条回答
  •  没有蜡笔的小新
    2021-01-30 00:50

    The difference is that execute simply starts the task without any further ado, whereas submit returns a Future object to manage the task. You can do the following things with the Future object:

    • Cancel the task prematurely, with the cancel method.
    • Wait for the task to finish executing, with get.

    The Future interface is more useful if you submit a Callable to the pool. The return value of the call method will be returned when you call Future.get. If you don't maintain a reference to the Future, there is no difference.

提交回复
热议问题