Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Efficient Android Threading
Dedication
Preface
Audience
Contents of This Book
Conventions Used in this Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgements
1. Android Components and the Need for Multiprocessing
Android Software Stack
Application Architecture
Application
Components
Activity
Service
ContentProvider
BroadcastReceiver
Application Execution
Linux Process
Lifecycle
Application start
Application termination
Structuring Applications for Performance
Creating Responsive Applications Through Threads
Summary
I. Fundamentals
2. Multithreading in Java
Thread Basics
Execution
Single-Threaded Application
Multithreaded Application
Increased resource consumption
Increased complexity
Data inconsistency
Thread Safety
Intrinsic Lock and Java Monitor
Synchronize Access to Shared Resources
Using the intrinsic lock
Using explicit locking mechanisms
Example: Consumer and Producer
Task Execution Strategies
Concurrent Execution Design
Summary
3. Threads on Android
Android Application Threads
UI Thread
Binder Threads
Background Threads
The Linux Process and Threads
Scheduling
Priority
Control groups
Summary
4. Thread Communication
Pipes
Basic Pipe Use
Example: Text Processing on a Worker Thread
Shared Memory
Signaling
BlockingQueue
Android Message Passing
Example: Basic Message Passing
Classes Used in Message Passing
MessageQueue
MessageQueue.IdleHandler
Example: Using IdleHandler to terminate an unused thread
Message
Initialized
Pending
Dispatched
Recycled
Looper
Looper termination
The UI thread Looper
Handler
Setup
Message creation
Message insertion
Example: Two-way message passing
Message processing
Removing Messages from the Queue
Observing the Message Queue
Taking a snapshot of the current message queue
Tracing the message queue processing
Communicating with the UI Thread
Summary
5. Interprocess Communication
Android RPC
Binder
AIDL
Synchronous RPC
Asynchronous RPC
Message Passing Using the Binder
One-Way Communication
Two-Way Communication
Summary
6. Memory Management
Garbage Collection
Thread-Related Memory Leaks
Thread Execution
Inner classes
Static inner classes
The lifecycle mismatch
Thread Communication
Sending a data message
Posting a task message
Avoiding Memory Leaks
Use Static Inner Classes
Use Weak References
Stop Worker Thread Execution
Retain Worker Threads
Clean Up the Message Queue
Summary
II. Asynchronous Techniques
7. Managing the Lifecycle of a Basic Thread
Basics
Lifecycle
Interruptions
Uncaught Exceptions
Thread Management
Definition and Start
Anonymous inner class
Public thread
Static inner class thread definition
Summary of options for thread definition
Retention
Retaining a thread in an Activity
Retaining a thread in a Fragment
Summary
8. HandlerThread: A High-Level Queueing Mechanism
Fundamentals
Lifecycle
Use Cases
Repeated Task Execution
Related Tasks
Example: Data persistence with SharedPreferences
Task Chaining
Example: Chained network calls
Conditional Task Insertion
Summary
9. Control over Thread Execution Through the Executor Framework
Executor
Thread Pools
Predefined Thread Pools
Custom Thread Pools
ThreadPoolExecutor configuration
Designing a Thread Pool
Sizing
Dynamics
Bounded or unbounded task queue
Thread configuration
Extending ThreadPoolExecutor
Lifecycle
Shutting Down the Thread Pool
Thread Pool Uses Cases and Pitfalls
Favoring thread creation over queuing
Handling preloaded task queues
The danger of zero core threads
Task Management
Task Representation
Submitting Tasks
Individual submission
invokeAll
invokeAny
Rejecting Tasks
ExecutorCompletionService
Summary
10. Tying a Background Task to the UI Thread with AsyncTask
Fundamentals
Creation and Start
Cancellation
States
Example: Limiting an AsyncTask execution to one at the time
Implementing the AsyncTask
Example: Downloading Images
Background Task Execution
Application Global Execution
Execution Across Platform Versions
Custom Execution
Example: Nonglobal sequential execution
AsyncTask Alternatives
When an AsyncTask Is Trivially Implemented
Background Tasks That Need a Looper
Local Service
Using execute(Runnable)
Summary
11. Services
Why Use a Service for Asynchronous Execution?
Local, Remote, and Global Services
Creation and Execution
Lifecycle
Started Service
Implementing onStartCommand
Options for Restarting
User-Controlled Service
Example: Bluetooth connection
Task-Controlled Service
Example: Concurrent download
Bound Service
Local Binding
Choosing an Asynchronous Technique
Summary
12. IntentService
Fundamentals
Good Ways to Use an IntentService
Sequentially Ordered Tasks
Example: Web service communication
Asynchronous Execution in BroadcastReceiver
Example: Periodical long operations
IntentService Versus Service
Summary
13. Access ContentProviders with AsyncQueryHandler
Brief Introduction to ContentProvider
Justification for Background Processing of a ContentProvider
Using the AsyncQueryHandler
Example: Expanding Contact List
Understanding the AsyncQueryHandler
Limitations
Summary
14. Automatic Background Execution with Loaders
Loader Framework
LoaderManager
initLoader vs restartLoader
LoaderCallbacks
AsyncTaskLoader
Painless Data Loading with CursorLoader
Using the CursorLoader
Example: Contact list
Adding CRUD Support
Example: Use CursorLoader with AsyncQueryHandler
Implementing Custom Loaders
Loader Lifecycle
Background Loading
Example: Simple custom loader
Content Management
Delivering Cached Results
Example: Custom File Loader
Handling Multiple Loaders
Summary
15. Summary: Selecting an Asynchronous Technique
Keep It Simple
Thread and Resource Management
Message Communication for Responsiveness
Avoid Unexpected Task Termination
Easy Access to ContentProviders
A. Bibliography
Books
Articles
Presentations
Index
Colophon
Copyright
← Prev
Back
Next →
← Prev
Back
Next →