SingleThreadExecutor VS plain thread

前端 未结 4 1851
盖世英雄少女心
盖世英雄少女心 2021-02-11 19:08

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:51

    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.

提交回复
热议问题