SingleThreadExecutor VS plain thread

前端 未结 4 1292
情话喂你
情话喂你 2021-02-11 19:39

Apart from the fact that the Executor interface has some advantages over plain threads (management, for example), is there any real internal difference (big perform

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-11 19:58

    It is an abstraction and those always come at "cost":

    • some (potential) amount of "performance penalty"
    • a reduced amount of "control" ( that is the whole point - you don't need to deal with the low level details, so, if you had to, ... )

    The major difference is that the service enables you to submit multiple tasks, whereas the thread can run exactly one Runnable. On the other hand, you have to worry about things such as "shutting down" the service.

    A rule of thumb: performance aspects should be close to "ignorable" here. Because of that, you prefer the "more abstract" executor service solution. Because that allows you to separate your concerns from the actual threading. And more importantly: if you ever choose to use a different kind of implementation for that service ... the rest of your code should not need to care about that.

    Long story short: abstractions cost, but in this case, you typically prefer the "more abstract" solution. Because in the end, that reduces the complexity of your solution.

提交回复
热议问题