Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Timing is Everything
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
katfishi
March 18, 2016
Programming
0
160
Timing is Everything
katfishi
March 18, 2016
Tweet
Share
More Decks by katfishi
See All by katfishi
Introduction to React Native
katfishi
1
160
Fancy Acronyms for Fancy Architectures
katfishi
0
240
Other Decks in Programming
See All in Programming
new(1.26) ← これすき / kamakura.go #8
utgwkk
0
2.3k
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
500
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
13
8k
どんと来い、データベース信頼性エンジニアリング / Introduction to DBRE
nnaka2992
1
280
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.1k
Vuetify 3 → 4 何が変わった?差分と移行ポイント10分まとめ
koukimiura
0
130
受け入れテスト駆動開発(ATDD)×AI駆動開発 AI時代のATDDの取り組み方を考える
kztakasaki
2
560
How to stabilize UI tests using XCTest
akkeylab
0
120
コードレビューをしない選択 #でぃーぷらすトウキョウ
kajitack
3
920
AHC061解説
shun_pi
0
370
nuget-server - あなたが必要だったNuGetサーバー
kekyo
PRO
0
240
maplibre-gl-layers - 地図に移動体たくさん表示したい
kekyo
PRO
0
260
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
GraphQLの誤解/rethinking-graphql
sonatard
75
11k
How GitHub (no longer) Works
holman
316
140k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
170
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
86
Context Engineering - Making Every Token Count
addyosmani
9
740
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
280
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Thoughts on Productivity
jonyablonski
75
5.1k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Transcript
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