SingleThreadExecutor VS plain thread

前端 未结 4 1294
情话喂你
情话喂你 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 20:05

    The major difference is in task execution policy.

    By creating a Thread instance or subclassing Thread you are basically executing a single task.

    Using Executors.newSingleThreadExecutor() on the other hand allows you to submit multiple tasks. Since those tasks are guaranteed not to be executed concurrently, this allows you to exploit the following thread confinement benefits:

    • No synchronization required when accessing objects that are not thread-safe
    • Memory effects of one task are guaranteed to be visible to the next task

提交回复
热议问题