[英]ScheduledExecutorService vs Timer vs Handler
What are the pros and cons of using ScheduledExecutorService
/ Timer
/ Handler
? 使用
ScheduledExecutorService
/ Timer
/ Handler
优缺点是什么? As I understand in Android instead of Timer
it's need to use Handler
, but what about ScheduledExecutorService
? 据我了解,在Android中需要使用
Handler
而不是Timer
,但是ScheduledExecutorService
呢?
As I understand Handler
and ScheduledExecutorService
is only for relative time, right? 据我了解,
Handler
和ScheduledExecutorService
仅用于相对时间,对吗?
All three allow you to execute tasks on a different (eg. non-main) thread. 所有这三个功能都允许您在不同的线程(例如非主线程)上执行任务。 The Handler allows you to use a message passing Actor pattern to communicate safely between thread.
该处理程序允许您使用消息传递Actor模式在线程之间安全地通信。 It does not allow you to do timing/delays/etc.
它不允许您执行定时/延迟/等操作。
A ScheduledExecutorService is a very generic threading management solution. ScheduledExecutorService是一种非常通用的线程管理解决方案。 You initialize it with a certain number to worker threads and then give it work units.
您使用一定数量的工作线程初始化它,然后为其分配工作单元。 You can delay/time and repeat work units.
您可以延迟/时间并重复工作单位。
The Timer class has a simple API which resembles a ScheduledExecutorService for one-time, one-thread usage. Timer类具有一个简单的API,该API与ScheduledExecutorService相似,可以一次性使用一个线程。 The official API suggests to not use this class but roll your own ScheduledExecutor instead.
官方API建议不要使用此类,而应使用自己的ScheduledExecutor。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.