O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Android™ Concurrency

Book Description

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

Table of Contents

  1. About This E-Book
  2. Title Page
  3. Copyright Page
  4. About the Android Deep Dive Series
    1. About the Series Editor
  5. Dedication Page
  6. Contents at a Glance
  7. Table of Contents
  8. Preface
    1. Who Should Read This Book
    2. How This Book Is Organized
    3. Example Code
    4. Conventions Used in This Book
  9. About the Author
  10. Acknowledgements
  11. 1. Understanding Concurrency
    1. Concurrency Made Hard
      1. Concurrency in Software
      2. Concurrency in Hardware
    2. Concurrency Made Simple
      1. Threads
      2. Atomic Execution
      3. Visibility
    3. Summary
  12. 2. Java Concurrency
    1. Java Threads
      1. The Thread Class
      2. Runnables
    2. Synchronization
      1. Mutexes
      2. Common Synchronization Errors
    3. Volatile
    4. Wait and Notify
      1. Wait
      2. Notify
    5. The Concurrency Package
      1. Safe Publication
      2. Executors
      3. Futures
    6. Summary
  13. 3. The Android Application Model
    1. Lifecycles and Components
      1. Process Priority
      2. Component Lifecycles
    2. Android Applications as Web Apps
    3. The Android Process
      1. Application Startup
      2. The Android Main Thread
    4. Summary
  14. 4. Async Tasks and Loaders
    1. Async Task Architecture
      1. Async Task Basics
      2. AsyncTask Execution
      3. AsyncTask Completion
    2. Using AsyncTasks
      1. AsyncTask: Considered Dangerous
      2. Getting It Right
    3. Loaders and Cursor Loaders
    4. AsyncTasks: What Went Wrong
    5. Summary
  15. 5. Looper/Handler
    1. Introducing the Looper/Handler
      1. Basic Looper/Handler
      2. Delegating Execution
    2. Some Gory Details
      1. Handlers and Messages
      2. Starting a Looper
      3. The Native Looper
      4. Scheduling and the Sync-Barrier
    3. Summary
  16. 6. Services, Processes, and Binder IPC
    1. Service Basics
      1. Started Service Essentials
      2. Bound Service Essentials
    2. Intents
    3. The Intent Service
    4. Bound Services
      1. A Simple Bound Service
      2. Binding A Service
      3. Unbinding A Service
      4. Binding Multiple Services
      5. Service Lifecycle
      6. Priorities and Flags
      7. A Local Bound Service
    5. Inter-Process Communication
      1. Parcelables
      2. Messengers
      3. Using AIDL
      4. Creating Processes
    6. Binder, Considered Briefly
      1. Binder Threads
      2. Binder Data Transfer Limits
      3. Binding to Death
    7. Summary
  17. 7. Periodic Tasks
    1. Task Characteristics
      1. Thread Safety
      2. Lifecycle-Aware
      3. Smart Use of Process Priority
      4. Power-Thrifty
      5. The Scorecard
    2. Timer and TimerTask
    3. Looper/Handler
    4. Custom Service-based Scheduler
    5. Alarm Manager and Intent Service
      1. The Alarm Manager Service
      2. Schedulable Tasks
    6. Sync-Adapters
      1. Defining a Sync-adapter
      2. How Sync-adapters Work
      3. Implementing the Sync-adapter
      4. Scoring the Sync-adapter
    7. The JobScheduler
      1. Scheduling a Task
      2. Running a Task
      3. Task Implementation
      4. Scoring the JobScheduler
    8. Summary
  18. 8. Concurrency Tools
    1. Static Analysis
      1. Android Studio
      2. Findbugs
    2. Annotations
      1. JCIR Annotations
      2. Support Library Annotations
    3. Assertions
    4. Conclusion and Best Practices
  19. Bibliography
  20. Index
  21. Code Snippets