简体   繁体   中英

ScheduledExecutorService vs Timer vs Handler

What are the pros and cons of using ScheduledExecutorService / Timer / Handler ? As I understand in Android instead of Timer it's need to use Handler , but what about ScheduledExecutorService ?

As I understand Handler and ScheduledExecutorService is only for relative time, right?

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. It does not allow you to do timing/delays/etc.

A ScheduledExecutorService is a very generic threading management solution. 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. The official API suggests to not use this class but roll your own ScheduledExecutor instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM