Difference between Executors.newFixedThreadPool(1) and Executors.newSingleThreadExecutor()

后端 未结 4 887
傲寒
傲寒 2020-12-29 23:12

My question is : does it make sense to use Executors.newFixedThreadPool(1)??. In two threads (main + oneAnotherThread) scenarios is it efficient to use execut

4条回答
  •  伪装坚强ぢ
    2020-12-29 23:49

    Is creating a new thread directly by calling new Runnable(){ } better than using ExecutorService?

    If you want to compute something on the returned result after thread compilation, you can use Callable interface, which can be used with ExecutorService only, not with new Runnable(){}. The ExecutorService's submit() method, which take the Callable object as an arguement, returns the Future object. On this Future object you check whether the task has been completed on not using isDone() method. Also you can get the results using get() method. In this case, ExecutorService is better than the new Runnable(){}.

提交回复
热议问题