Reputation: 5767
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?
Upvotes: 17
Views: 7452
Reputation: 661
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.
Upvotes: 5