Cover image for Efficient Android Threading

Book description

Multithreading is essential if you want to create an Android app with a great user experience, but how do you know which techniques can help solve your problem? This practical book describes many asynchronous mechanisms available in the Android SDK, and provides guidelines for selecting the ones most appropriate for the app you’re building.

Table of Contents

  1. I. Introduction
    1. 1. Android Components and the Need for Multi-Processing
      1. Android Software Stack
      2. Application Architecture
        1. Application
        2. Components
          1. Activity
          2. Service
          3. ContentProvider
          4. BroadcastReceiver
      3. Application Execution
        1. Linux Process
        2. Lifecycle
          1. Application Start
          2. Application Termination
      4. Structuring Applications for Performance
        1. Creating Responsive Applications Through Threads
      5. Summary
  2. II. Fundamentals
    1. 2. Multithreading in Java
      1. Thread Basics
        1. Execution
        2. Single-Threaded Application
        3. Multi-Threaded Application
          1. Increased Resource Consumption
          2. Increased Complexity
          3. Data Inconsistency
      2. Thread Safety
        1. Intrinsic Lock and Java Monitor
        2. Synchronize Access to Shared Resources
          1. Using the Intrinsic Lock
          2. Using Explicit Locking Mechanisms
        3. Example: Consumer and Producer
      3. Task Execution Strategies
        1. Concurrent Execution Design
      4. Summary
    2. 3. Threads on Android
      1. Android Application Threads
        1. UI Thread
        2. Binder threads
        3. Background threads
      2. The Linux Process and Threads
        1. Scheduling
          1. Priority
          2. Control groups
      3. Summary
    3. 4. Thread Communication
      1. Pipes
        1. Basic Pipe Use
        2. Text processing on a worker thread
      2. Shared memory
        1. Signalling
      3. BlockingQueue
      4. Android Message Passing
        1. Basic Message Passing Example
        2. Classes Used in Message Passing
        3. MessageQueue
          1. MessageQueue.IdleHandler
          2. Example: Using IdleHandler to terminate an unused thread
        4. Message
          1. Initialized
          2. Pending
          3. Dispatched
          4. Recycled
        5. Looper
          1. Looper termination
          2. The UI thread Looper
        6. Handler
          1. Setup
          2. Message creation
          3. Message insertion
          4. Example: Two-way message passing
          5. Message processing
        7. Removing Messages from the Queue
        8. Observing the Message Queue
          1. Taking a snapshot of the current message queue
          2. Tracing the message queue processing
      5. Communicating with the UI Thread
      6. Summary
    4. 5. Interprocess Communication
      1. Android RPC
        1. Binder
      2. AIDL
        1. Synchronous RPC
        2. Asynchronous RPC
      3. Message-Passing Using the Binder
        1. One-way communication
        2. Two-way communication
      4. Summary
    5. 6. Memory Management
      1. Garbage collection
      2. Thread related memory leaks
        1. Thread execution
          1. Inner classes
          2. Static inner classes
          3. The lifecycle mismatch
        2. Thread communication
          1. Sending a data message
          2. Posting a task message
      3. Avoiding memory leaks
        1. Use Static Inner Classes
        2. Use Weak References
        3. Stop Worker Thread Execution
        4. Retain Worker Threads
        5. Clean Up the Message Queue
      4. Summary
  3. III. Asynchronous Techniques
    1. 7. Managing the Lifecycle of a Basic Thread
      1. Basics
        1. Lifecycle
        2. Interruptions
        3. Uncaught Exceptions
      2. Thread management
        1. Definition and start
          1. Anonymous inner class
          2. Public thread
          3. Static inner class thread definition
          4. Summary of options for thread definition
        2. Retention
          1. Retaining a Thread in an Activity
          2. Retaining a Thread in a Fragment
      3. Summary
    2. 8. HandlerThread: a High-Level Queueing Mechanism
      1. Fundamentals
      2. Lifecycle
      3. Use Cases
        1. Repeated Task Execution
        2. Related Tasks
          1. Example: Data persistence with SharedPreferences
        3. Task Chaining
          1. Example: chained network calls
        4. Conditional task insertion
      4. Summary
    3. 9. Control Over Thread Execution Through the Executor Framework
      1. Executor
      2. Thread Pools
        1. Predefined Thread Pools
        2. Custom Thread Pools
          1. ThreadPoolExecutor Configuration
          2. Thread configuration
          3. Extending ThreadPoolExecutor
        3. Lifecycle
        4. Shutting Down the Thread Pool
      3. Task management
        1. Task representation
          1. FutureTask
        2. Submitting Tasks
          1. Individual submission
          2. invokeAll
          3. invokeAny
        3. Rejecting Tasks
      4. ExecutorCompletionService
      5. Summary
    4. 10. Tying a Background task to the UI Thread with AsyncTask
      1. Fundamentals
        1. Creation and Start
        2. Cancellation
        3. States
          1. Example: Limiting an AsyncTask execution to one at the time
      2. Implementing the AsyncTask
        1. Example: Downloading Images
      3. Background Task Execution
        1. Application Global Execution
        2. Execution Across Platform Versions
        3. Custom Execution
          1. Example: non-global sequential execution
      4. AsyncTask Alternatives
        1. When an AsyncTask is trivially implemented
        2. Background Tasks that Need a Looper
        3. Local Service
        4. Using execute(Runnable)
      5. Summary
    5. 11. Services
      1. Why Use a Service For Asynchronous Execution?
      2. Local, Remote, and Global Services
      3. Creation and Execution
      4. Lifecycle
      5. Started Service
        1. Implementing onStartCommand
        2. Options for Restarting
        3. User-Controlled Service
          1. Example: Bluetooth Connection
        4. Task-Controlled Service
          1. Example: Concurrent Download
      6. Bound Service
        1. Local Binding
      7. Choosing an Asynchronous Technique
      8. Summary
    6. 12. IntentService
      1. Fundamentals
      2. Good Ways To Use An IntentService
        1. Sequential Ordered Tasks
          1. Example: Web Service Communication
        2. Asynchronous Execution in BroadcastReceiver
          1. Example: Periodical Long Operations
      3. IntentService Versus Service
      4. Summary
  4. About the Author
  5. Copyright