Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Timing is Everything
Search
katfishi
March 18, 2016
Programming
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Timing is Everything
katfishi
March 18, 2016
Transcript
58 slides
Timing is Everything Katheryn Shi @Katfishi
None
141 partners worldwide 1,825 courses 3 million downloads
Timer ScheduledThreadPoolExecutor Handler AlarmManager
Timer
Timer timer = new Timer(); TimerTask task = new TimerTask()
{ @Override public void run() { // task here } }
timer.schedule(task, new Date()); timer.schedule(task, TIME_DELAY);
timer.schedule( task, new Date(), PERIOD); timer.schedule( task, TIME_DELAY, PERIOD);
timer.scheduleAtFixedRate( task, new Date(), PERIOD); timer.scheduleAtFixedRate( task, TIME_DELAY, PERIOD);
Coffee Break
fixed-period fixed-rate 10 am 11 am 12 pm 1 pm
2 pm
task.cancel(); timer.cancel(); timer.purge();
Reusable?
Which Thread?
TimerTask lengthy = new TimerTask() { @Override public void run()
{ lengthyOperation(); } }
TimerTask task = new TimerTask() { @Override public void run()
{ flipBackgroundColor(); } }
timer.schedule(task, 1000); timer.schedule(lengthy, 0);
None
+ Simple – Memory leaks – Single thread – Not
reusable – Little control
None
None
ScheduledThreadPoolExecutor / ScheduledExecutorService
ScheduledExecutorService service = Executors .newSingleThreadScheduledExecutor();
ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
Runnable r = new Runnable() { @Override public void run()
{ // task here } }
ScheduledFuture future = service.schedule(r, 10, TimeUnit.SECONDS); future.cancel(true);
TimeUnit.DAYS TimeUnit.HOURS TimeUnit.MINUTES TimeUnit.SECONDS TimeUnit.MILLISECONDS TimeUnit.MICROSECONDS TimeUnit.NANOSECONDS
scheduleAtFixedRate(Runnable, long, long, TimeUnit) scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
scheduleAtFixedRate scheduleWithFixedDelay 10 am 11 am 12 pm 1 pm
2 pm
service.shutdown(); List<Runnable> unexecuted = service.shutdownNow();
Reusable?
Which Thread?
None
+ Multiple threads + More control + Reusable – Complicated
None
Handler
Message Queue Handler Handler Looper M M M
Message Queue Handler Handler Looper M M M
Message Queue Handler Handler Looper M M M
Message Queue Handler Handler Looper M M M
Message Queue Handler Handler Looper M M M
Handler handler = new Handler(); Runnable r = new Runnable()
{ @Override public void run() { // task here } }
handler.postDelayed(r, TIME_DELAY); handler.postAtTime(r, UPTIME_MILLIS);
Runnable r = new Runnable() { @Override public void run()
{ // task here handler.postDelayed(this, TIME_DELAY); } }
handler.removeCallbacks(r);
Reusable?
Which Thread?
+ Android + More control + Reusable – Long operations
AlarmManager
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
public static class AlarmReceiver extends BroadcastReceiver { @Override public void
onReceive(Context context, Intent intent) { // task here } }
Intent i = new Intent( MainActivity.this, AlarmReceiver.class); PendingIntent pi =
PendingIntent .getBroadcast( MainActivity.this, REQUEST_CODE, i, FLAGS);
am.set(AlarmManager.RTC_WAKEUP, TRIGGER_TIME, pi); am.setRepeating( AlarmManager.RTC_WAKEUP, TRIGGER_TIME, INTERVAL, pi);
AlarmManager.RTC_WAKEUP AlarmManager.RTC AlarmManager.ELAPSED_REALTIME_WAKEUP AlarmManager.ELAPSED_REALTIME
am.cancel(pi);
+ Sleep control – Complex
quick fix Timer more control ScheduledThreadPoolExecutor easy Android Handler wake
up AlarmManager
@Katfishi github.com/katfishi kshi@ .org
More Decks by katfishi
See All by katfishi
Introduction to React Native
katfishi
1
160
Fancy Acronyms for Fancy Architectures
katfishi
0
250
Other Decks in Programming
See All in Programming
仕様駆動開発入門 (何を作るかを先に決めて渡す) | 目指せ Kiro マスター 05
inariku
PRO
2
150
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
250
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.2k
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
190
プロジェクトコードをキレイキレイしよう!暗黙的な参照を読み解く静的解析
ostk0069
0
210
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.9k
Seven New Principles of Software Engineering
yegor256
0
490
[Kotlin Fest 2026] eBPF Tracing for Kotlin
takehaya
0
120
複数ドメインを一つのアプリに統合するFlutterの設計と運用_FlutterKaigi2026登壇資料(須永高弘)
takahirosunaga
0
180
Next.js: The final boss of Web Performance
guaca
0
400
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
160
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
2k
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
4 Signs Your Business is Dying
shpigford
187
23k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
750
Discover your Explorer Soul
emna__ayadi
2
1.3k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
Tell your own story through comics
letsgokoyo
1
1.1k
AI: The stuff that nobody shows you
jnunemaker
PRO
12
1.3k
Product Roadmaps are Hard
iamctodd
55
13k
A better future with KSS
kneath
240
18k
Designing for humans not robots
tammielis
254
26k
Why Our Code Smells
bkeepers
PRO
340
58k