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
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
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.3k
AgentCoreとHuman in the Loop
har1101
5
230
CSC307 Lecture 07
javiergs
PRO
0
550
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
180
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
200
なぜSQLはAIぽく見えるのか/why does SQL look AI like
florets1
0
450
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
3.8k
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.5k
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1k
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
4
270
Featured
See All Featured
Designing for Performance
lara
610
70k
Marketing to machines
jonoalderson
1
4.6k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
230
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
240
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Documentation Writing (for coders)
carmenintech
77
5.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
92
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
350
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
110k
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