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
170
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
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
260
DroidKnights 2026 Android A2UI Jetpack Compose 기반의 안전한 AI UI 렌더링 - 박제창
itsmedreamwalker
1
260
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
260
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
240
Distance field of Julia set
kinakomoti321
0
330
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
790
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
190
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
610
Vue.jsで作る空間解析アプリとCapacitorプラグインのData Bridge
hal_spidernight
1
180
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
270
imagewebp()の向こう側: WebPはなぜ JPEG・PNGより軽いのか
yuzneri
0
150
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
180
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.9k
Why Our Code Smells
bkeepers
PRO
340
58k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
460
The Language of Interfaces
destraynor
162
27k
GitHub's CSS Performance
jonrohan
1033
470k
Site-Speed That Sticks
csswizardry
13
1.6k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
2
540
Everyday Curiosity
cassininazir
0
420
For a Future-Friendly Web
brad_frost
184
11k
Google's AI Overviews - The New Search
badams
0
1.7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.7k