Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

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.

Author Anders Goransson demonstrates the advantages and disadvantages of each technique, with sample code and detailed explanations for using it efficiently. The first part of the book describes the building blocks of asynchronous processing, and the second part covers Android libraries and constructs for developing fast, responsive, and well-structured apps.

  • Understand multithreading basics in Java and on the Android platform
  • Learn how threads communicate within and between processes
  • Use strategies to reduce the risk of memory leaks
  • Manage the lifecycle of a basic thread
  • Run tasks sequentially in the background with HandlerThread
  • Use Java’s Executor Framework to control or cancel threads
  • Handle background task execution with AsyncTask and IntentService
  • Access content providers with AsyncQueryHandler
  • Use loaders to update the UI with new data
1118175352
Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

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.

Author Anders Goransson demonstrates the advantages and disadvantages of each technique, with sample code and detailed explanations for using it efficiently. The first part of the book describes the building blocks of asynchronous processing, and the second part covers Android libraries and constructs for developing fast, responsive, and well-structured apps.

  • Understand multithreading basics in Java and on the Android platform
  • Learn how threads communicate within and between processes
  • Use strategies to reduce the risk of memory leaks
  • Manage the lifecycle of a basic thread
  • Run tasks sequentially in the background with HandlerThread
  • Use Java’s Executor Framework to control or cancel threads
  • Handle background task execution with AsyncTask and IntentService
  • Access content providers with AsyncQueryHandler
  • Use loaders to update the UI with new data
32.49 In Stock
Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

by Anders Goransson
Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

by Anders Goransson

eBook

$32.49  $42.99 Save 24% Current price is $32.49, Original price is $42.99. You Save 24%.

Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

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.

Author Anders Goransson demonstrates the advantages and disadvantages of each technique, with sample code and detailed explanations for using it efficiently. The first part of the book describes the building blocks of asynchronous processing, and the second part covers Android libraries and constructs for developing fast, responsive, and well-structured apps.

  • Understand multithreading basics in Java and on the Android platform
  • Learn how threads communicate within and between processes
  • Use strategies to reduce the risk of memory leaks
  • Manage the lifecycle of a basic thread
  • Run tasks sequentially in the background with HandlerThread
  • Use Java’s Executor Framework to control or cancel threads
  • Handle background task execution with AsyncTask and IntentService
  • Access content providers with AsyncQueryHandler
  • Use loaders to update the UI with new data

Product Details

ISBN-13: 9781449364090
Publisher: O'Reilly Media, Incorporated
Publication date: 05/22/2014
Sold by: Barnes & Noble
Format: eBook
Pages: 280
File size: 4 MB

About the Author

Anders Goransson is a software architect, developer, trainer and international speaker. He holds a M.Sc. in Engineering Physics and has spent his whole career in the software industry. He started with moving bits and bytes in industrial automation systems in 2001, but as of 2005 he has focused on software for mobile devices. He embraced the Android OS as the most exciting mobile platform for the future already when the first smartphone was released publicly in 2008. Ever since, he has helped major handset manufacturers, carriers, financial institutions, start-ups, etc., with the transfer into the smartphone era.

Table of Contents

Preface xi

1 Android Components and the Need for Multiprocessing 1

Android Software Stack 1

Application Architecture 2

Application 3

Components 3

Application Execution 5

Linux Process 6

Lifecycle 6

Structuring Applications for Performance 9

Creating Responsive Applications Through Threads 9

Summary 11

Part I Fundamentals

2 Multithreading in Java 15

Thread Basics 15

Execution 15

Single-Threaded Application 17

Multithreaded Application 17

Thread Safety 19

Intrinsic Lock and Java Monitor 20

Synchronize Access to Shared Resources 22

Example: Consumer and Producer 24

Task Execution Strategies 26

Concurrent Execution Design 27

Summary 27

3 Threads on Android 29

Android Application Threads 29

UI Thread 29

Binder Threads 30

Background Threads 30

The Linux Process and Threads 31

Scheduling 34

Summary 37

4 Thread Communication 39

Pipes 39

Basic Pipe Use 40

Example: Text Processing on a Worker Thread 42

Shared Memory 44

Signaling 45

BlockingQueue 46

Android Message Passing 47

Example: Basic Message Passing 49

Classes Used in Message Passing 51

Message 55

Looper 58

Handler 60

Removing Messages from the Queue 68

Observing the Message Queue 70

Communicating with the UI Thread 73

Summary 74

5 Interprocess Communication 75

Android RPC 75

Binder 76

AIDL 77

Synchronous RPC 79

Asynchronous RPC 81

Message Passing Using the Binder 83

One-Way Communication 84

Two-Way Communication 86

Summary 87

6 Memory Management 89

Garbage Collection 89

Thread-Related Memory Leaks 91

Thread Execution 92

Thread Communication 98

Avoiding Memory Leaks 101

Use Static Inner Classes 101

Use Weak References 101

Stop Worker Thread Execution 102

Retain Worker Threads 102

Clean Up the Message Queue 102

Summary 103

Part II Asynchronous Techniques

7 Managing the Lifecycle of a Basic Thread 107

Basics 107

Lifecycle 107

Interruptions 108

Uncaught Exceptions 110

Thread Management 112

Definition and Start 112

Retention 114

Summary 119

8 HandlerThread: A High-Level Queueing Mechanism 121

Fundamentals 121

Lifecycle 123

Use Cases 124

Repeated Task Execution 125

Related Tasks 125

Task Chaining 128

Conditional Task Insertion 131

Summary 131

9 Control over Thread Execution Through the Executor Framework 133

Executor 133

Thread Pools 136

Predefined Thread Pools 136

Custom Thread Pools 137

Designing a Thread Pool 138

Lifecycle 142

Shutting Down the Thread Pool 143

Thread Pool Uses Cases and Pitfalls 145

Task Management 146

Task Representation 146

Submitting Tasks 146

Rejecting Tasks 151

ExecutorCompletionService 152

Summary 154

10 Tying a Background Task to the UI Thread with AsycnTask 157

Fundamentals 157

Creation and Start 160

Cancellation 161

States 162

Implementing the AsyncTask 163

Example: Downloading Images 164

Background Task Execution 167

Application Global Execution 169

Execution Across Platform Versions 170

Custom Execution 172

AsyncTask Alternatives 173

When an AsyncTask Is Trivially Implemented 173

Background Tasks That Need a Looper 174

Local Service 174

Using execute(Runnable) 174

Summary 175

11 Services 177

Why Use a Service for Asynchronous Execmion? 177

Local, Remote, and Global Services 177

Creation and Execution 181

Lifecycle 181

Started Service 183

Implementing onStartCommand 184

Options for Restarting 184

User- Controlled Service 186

Task-Controlled Service 190

Bound Service 192

Local Binding 194

Choosing an Asynchronous Technique 197

Summary 198

12 IntentService 199

Fundamentals 199

Good Ways to Use an IntentService 201

Sequentially Ordered Tasks 201

Asynchronous Execution in BroadcastReceiver 204

IntentService Versus Service 207

Summary 207

13 Access ContentProviders with AsyncQueryHandier 209

Brief Introduction to ContentProvider 209

Justification for Background Processing of a ContentProvider 211

Using the AsyncQueryHandier 212

Example: Expanding Contact List 214

Understanding the AsyncQueryHandier 217

Limitations 218

Summary 218

14 Automatic Background Execution with Loaders 219

Loader Framework 220

LoaderManager 221

Loader Callbacks 224

AsyncTaskLoader 225

Painless Data Loading with CursorLoader 226

Using the CursorLoader 227

Example: Contact list 227

Adding CRUD Support 229

Implementing Custom Loaders 233

Loader Lifecycle 233

Background Loading 234

Content Management 236

Delivering Cached Results 237

Example: Custom File Loader 238

Handling Multiple Loaders 241

Summary 242

15 Summary: Selecting an Asynchronous Technique 243

Keep It Simple 244

Thread and Resource Management 244

Message Communication for Responsiveness 245

Avoid Unexpected Task Termination 246

Easy Access to ContentProviders 247

A Bibliography 249

From the B&N Reads Blog

Customer Reviews