ExecutorService vs Casual Thread Spawner

后端 未结 2 1140
野趣味
野趣味 2020-12-01 16:15

I have a basic question about how ExecutorService works in Java.

It is quite hard to see the difference between simply creating Threads to

2条回答
  •  温柔的废话
    2020-12-01 16:44

    ExecutorService provides many advantages compared to plain threads

    1. You can create/manage/control life cycle of Threads & optimize thread creation cost overheads
    2. You can control processing of tasks ( Work Stealing, ForkJoinPool, invokeAll) etc.
    3. You can schedule tasks in Future time
    4. You can monitor the progress and health of threads

    Even for a single Thread, I prefer to use Executors.newFixedThreadPool(1);

    Have a look at related SE questions:

    Java's Fork/Join vs ExecutorService - when to use which?

    What are the advantages of using an ExecutorService?

提交回复
热议问题