new Thread(task).start() VS ThreadPoolExecutor.submit(task) in Android

后端 未结 3 1969
情歌与酒
情歌与酒 2020-12-31 05:14

In my Android project I had a lot of places where I need to run some code asynchronously (a web request, call to db etc.). This is not long running tasks (maximum a few seco

3条回答
  •  星月不相逢
    2020-12-31 05:43

    To answer your question — Yes, using Executor is better than creating new threads because:

    1. Executor provides a selection of different thread pools. It allows re-use of already existing threads which increases performance as thread creation is an expensive operation.
    2. In case a thread dies, Executor can replace it with a new thread without affecting the application.
    3. Changes to multi-threading policies are much easier, as only the Executor implementation needs to be changed.

提交回复
热议问题