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
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
contenteditable と日本語入力に向き合う
colorful12
0
200
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
パズルゲームの作り方 / how to make puzzle games
kaityo256
PRO
2
290
Railsバージョンを4倍にした話
optim
0
400
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
5.2k
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
ジョジョの世界をXRで再現する
mofurune
0
170
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
170
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
130
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
590
Between Models and Reality
mayunak
4
500
Building Adaptive Systems
keathley
44
3.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
WCS-LA-2024
lcolladotor
0
890
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
570
RailsConf 2023
tenderlove
30
1.6k
Thoughts on Productivity
jonyablonski
76
5.4k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
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