ebook img

Efficient Android Threading: Asynchronous Processing Techniques for Android Applications PDF

279 Pages·2014·12.03 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Efficient Android Threading: Asynchronous Processing Techniques for Android Applications

Efficient Android Threading Anders Göransson Efficient Android Threading by Anders Göransson Copyright © 2014 Anders Göransson. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editors: Andy Oram and Rachel Roumeliotis Indexer: Ellen Troutman-Zaig Production Editor: Melanie Yarbrough Cover Designer: Karen Montgomery Copyeditor: Eliahu Sussman Interior Designer: David Futato Proofreader: Amanda Kersey Illustrator: Rebecca Demarest May 2014: First Edition Revision History for the First Edition: 2014-05-21: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449364137 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Efficient Android Threading, the cover image of mahi-mahi, and related trade dress are trade‐ marks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-36413-7 [LSI] To Anna, Fabian, and Ida. 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 v 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 vi | Table of Contents 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 Table of Contents | vii Thread Pool Uses Cases and Pitfalls 145 Task Management 146 Task Representation 146 Submitting Tasks 147 Rejecting Tasks 151 ExecutorCompletionService 152 Summary 154 10. Tying a Background Task to the UI Thread with AsyncTask. . . . . . . . . . . . . . . . . . . . . . . 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 Execution? 177 Local, Remote, and Global Services 179 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 viii | Table of Contents

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 mos
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.