SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Write Apps for Maximum Performance and Responsiveness
“Threading and concurrency are as important in mobile as they are in large, distributed systems. This book does an excellent job of re-introducing us to the basic concepts and describing how to apply them to the Android framework. Even the most experienced Android developer should pick up at least a few tricks from these pages.”
—Dave Smith, PE, Google Developer Expert for Android
Mastering concurrency is critical to developing software with superior performance and responsiveness. This is especially true for Android, where interruptions are frequent and abrupt, and in order to be correct, programs must be aware of component and process lifecycles in addition to being thread safe.
You need a deep, Android-specific understanding of concurrency—and Android Concurrency delivers it. This guide in Addison-Wesley’s Android Deep Dive series for experienced Android developers helps you leverage today’s multi-core processors and heavily cached architectures, as well as major improvements built into Android 5 (Lollipop).
Top Android developer and consultant Blake Meike strips the magic and mystery from concurrent programming and presents intensely practical solutions for everything from inter-thread communication to network communication. Meike introduces a simple but powerful architectural framework you can use to address new issues whenever they arise, and offers expert guidance for debugging even highly complex concurrency issues.
Android Concurrency combines in-depth knowledge, proven patterns and idioms, and world-class insights for avoiding performance-killing mistakes. For serious Android developers, it will be an indispensable resource.
You will
• Gain new clarity about what concurrency really is, and how concurrent processes work
• Master best practices for writing concurrent code that’s more robust and less susceptible to weird, hard-to-diagnose errors
• Review the Java concurrency mechanisms Android’s constructs are built upon
• Shape an approach to concurrency that reflects the unique characteristics of the Android environment
• Avoid widespread misconceptions that lead Android developers into trouble
• Make the most of AsyncTask—but only when it’s the right tool for the job
• Leverage the powerful, lightweight Looper/Handler framework to support scheduled, asynchronous tasks and execute many message types
• Use the Android Service component to separate business logic from UI
• Understand the differences between started and bound services and use them effectively for intra- and inter-process communication
• Perform scheduled tasks, including tasks requiring polling and explicit scheduling
• Track down problems via static analysis, annotations, and assertions
Download the sample pages (includes Chapter 1 and Index)
Preface xiii
Chapter 1 Understanding Concurrency 1
Concurrency Made Hard 1
Concurrency in Software 2
Concurrency in Hardware 3
Concurrency Made Simple 4
Threads 4
Atomic Execution 5
Visibility 6
Summary 7
Chapter 2 Java Concurrency 9
Java Threads 9
The Thread Class 10
Runnables 11
Synchronization 11
Mutexes 12
Common Synchronization Errors 15
Volatile 17
Wait and Notify 20
Wait 20
Notify 22
The Concurrency Package 23
Safe Publication 23
Executors 25
Futures 26
Summary 27
Chapter 3 The Android Application Model 29
Lifecycles and Components 29
Process Priority 31
Component Lifecycles 33
Android Applications as Web Apps 35
The Android Process 36
Application Startup 36
The Android Main Thread 38
Summary 40
Chapter 4 Async Tasks and Loaders 41
Async Task Architecture 41
Async Task Basics 45
AsyncTask Execution 48
AsyncTask Completion 50
Using AsyncTasks 52
AsyncTask: Considered Dangerous 52
Getting It Right 58
Loaders and Cursor Loaders 59
AsyncTasks: What Went Wrong 67
Summary 68
Chapter 5 Looper/Handler 69
Introducing the Looper/Handler 69
Basic Looper/Handler 71
Delegating Execution 73
Some Gory Details 81
Handlers and Messages 81
Starting a Looper 84
The Native Looper 86
Scheduling and the Sync-Barrier 87
Summary 88
Chapter 6 Services, Processes, and Binder IPC 91
Service Basics 92
Started Service Essentials 93
Bound Service Essentials 95
Intents 96
The Intent Service 99
Bound Services 103
A Simple Bound Service 103
Binding A Service 105
Unbinding A Service 106
Binding Multiple Services 107
Service Lifecycle 109
Priorities and Flags 111
A Local Bound Service 112
Inter-Process Communication 114
Parcelables 116
Messengers 116
Using AIDL 120
Creating Processes 123
Binder, Considered Briefly 125
Binder Threads 125
Binder Data Transfer Limits 126
Binding to Death 126
Summary 126
Chapter 7 Periodic Tasks 127
Task Characteristics 127
Thread Safety 128
Lifecycle-Aware 128
Smart Use of Process Priority 128
Power-Thrifty 129
The Scorecard 130
Timer and TimerTask 130
Looper/Handler 130
Custom Service-based Scheduler 133
Alarm Manager and Intent Service 134
The Alarm Manager Service 135
Schedulable Tasks 139
Sync-Adapters 142
Defining a Sync-adapter 143
How Sync-adapters Work 145
Implementing the Sync-adapter 147
Scoring the Sync-adapter 152
The JobScheduler 155
Scheduling a Task 156
Running a Task 159
Task Implementation 161
Scoring the JobScheduler 165
Summary 166
Chapter 8 Concurrency Tools 167
Static Analysis 167
Android Studio 168
Findbugs 169
Annotations 177
JCIR Annotations 177
Support Library Annotations 178
Assertions 179
Conclusion and Best Practices 180
Bibliography 183
Index 185