Android developers need to understand the nature of a thread, and what it means when an application is running more than one thread at a time. On any sort of computer, whether it be a desktop, a laptop, a tablet, or a cell phone, while one instruction is being executed, the CPU can’t do anything else. On modern computing devices, these tasks can be incredibly fast, but there’s still only one thing happening. The CPU just can’t do two things at the same time.
- [Instructor] Android developers need to understand the nature of a thread and what it means when an application is running more than one thread at a time. On any sort of computer, whether it be a desktop, a laptop, a tablet, or a cell phone, a CPU executes a sequence of instructions. While one instruction is being executed, the CPU can't do anything else. Each instruction takes as much time as it needs and then the CPU goes on to the next instruction. On modern computing devices, these tasks can be incredibly fast, but there's still only one thing happening.
The CPU can't do two things at the same time. When you execute multiple simultaneous operations, that's called multithreading. Contemporary Android devices have multiple CPUs, also sometimes known as cores. The operating system is able to assign multiple threads to the different cores as needed and that's how it accomplishes running multiple threads simultaneously. On an Android device, the user interface, what the user sees, runs on a single foreground thread, also known as the main thread.
If you see many things happening at the same time, such as a list scrolling while an animation is happening, or if you know that a network request is happening in the background and it's taking a little bit of time, but you can still interact with the application, that means that Android is doing some of that work on another thread. If you try to do those same operations that take a while on the main thread, it can freeze the user interface. You won't see animations happening, notifications won't be displayed, and the user won't be able to interact with the app.
And there are also certain operations that you can't execute on the main thread. For example, if you want to make a network request, that has to be done in a background thread. If you were allowed to make a request over the web on the main thread, it would block and freeze the user interface until the response came back. On older versions of Android you could do that, even if it was a bad idea. Starting with Android 4 though, network requests must be processed in background threads and if you try to do it in a foreground thread, you'll get an exception.
There are many ways to set up and execute background threads in Android, starting with classes and interfaces from the Core Java API, and continuing with a number of abstractions that are unique to Android. What they all have in common is that these APIs let you, the developer, set up a background thread, manage which tasks will be executed in that thread, and then exert some control over when it happens. I'll start with the lowest-level tools from Java's Core API, including the Thread and Handler classes.
I'll also cover some other tools from Java's Concurrency framework that aren't unique to Android. I'll then move on to APIs that are built for Android, starting with the AsyncTask class that's a wrapper for Threads and Handlers, and moving on to IntentServices. An IntentService is a particular kind of service that executes tasks in the background in its own thread and then terminates itself when it's finished. I'll also talk about the JobScheduler API, a critical tool that lets you manage tasks in the background and overcome certain restrictions that are new to Android 8 Oreo.
I'll then move on to some higher-level approaches to using background threads including Loaders, a particular kind of framework that you can use to acquire data from a number of different kinds of data sources. And I'll also give a demonstration of Retrofit, an important library that makes network requests easy and incorporates multithreading. By the end of this course, you'll have seen a variety of methods for managing concurrent operations in Android apps. All of which can help you keep your app responsive to the user while the required work is getting done in the background.
First, discover how to create and start simple background threads, and how to use handlers to manage a thread's message queue. Then, learn various methods for optimizing the scheduling and performance of background tasks in Android with AsyncTask, intent services, and the JobScheduler API. Plus, explore tools that help you implement multithreading for different tasks in Android: Loader, for asynchronous data loading, and the open-source API Retrofit, for making HTTP requests.
Note: To get the most out of this course, you should be comfortable programming with Java, and should understand the most basic skills that are needed to build Android apps with the Android SDK and Android Studio.
- Creating and running a background thread
- Sending messages to the UI from threads
- Managing multiple background threads
- Managing threads with AsyncTask
- Managing long-running tasks with services
- Scheduling background tasks with JobScheduler
- Using other APIs for concurrent programming