# Primer on Threading and Handlers in Android

DevFeed: [Primer on Threading and Handlers in Android](<https://devfeed.tech/articles/primer-on-threading-and-handlers-in-android-25310.md>)

Original publisher: [Read original article](<https://kau.sh/blog/primer-on-threading-and-handlers-in-android/>)

Author: Kaushik Gopal

Published: 2014-06-20T07:00:00Z

Content type: tutorial

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [Java](<https://devfeed.tech/topics/java.md>), [Mobile](<https://devfeed.tech/topics/mobile.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [code](<https://devfeed.tech/tags/code.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [interface](<https://devfeed.tech/tags/interface.md>), [java](<https://devfeed.tech/tags/java.md>), [main-thread](<https://devfeed.tech/tags/main-thread.md>), [multitasking](<https://devfeed.tech/tags/multitasking.md>), [multithreading](<https://devfeed.tech/tags/multithreading.md>), [processor](<https://devfeed.tech/tags/processor.md>), [threading](<https://devfeed.tech/tags/threading.md>)

## AI overview

This tutorial introduces threading for Android applications, explains how threads support asynchronous or parallel processing in Java, and begins discussing Android's main thread and Handlers.

## Source excerpt

Mobile devices are getting pretty fast, but they aren't infinitely fast yet. If you want your app to be able to do any serious work without affecting the user experience by locking up the interface, you'll have to resort to running things in parallel. On Android, this is done with "threads". Grab yourself a cup of coffee and read this post line by line. I'll introduce you to the concept of threads, talk about how Java uses threads and explain how "Handlers" in Android help with threading. Whenever you want to do asynchronous/parallel processing, you do it with threads. Threads you say ? ## A thread or "thread of execution" is basically a sequence of instructions (of program code), that you send to your operating system. Image Courtesy: Wikipedia. "Typically" your CPU can process one thread, per core, at any time. So a multi-core processor (most Android devices today) by definition can handle multiple-threads of execution (which is to say, they can do multiple things at once). Truth to multi-core processing and single-core multitasking ## I say "typically" because the corollary to the above statement is not necessarily true. Single-core devices can "simulate" multithreading using multitasking. Every "task" that's run on a thread can be broken down into multiple instructions. These instructions don't have to happen all at once. So a single-core device can switch to a thread "1" finish an instruction 1A, then switch to thread "2" finish an instruction 2A, switch back to 1 finish 1B, 1C, 1D, switch to 2, finish 2B, 2C and so on... This switching between threads happens so fast that it appears, even on a single-core device, that all the threads are making progress at exactly the same time. It's an illusion caused by speed, much like Agent Brown appearing to have multiple heads and arms. Now on to some code. Threads in core Java ## In Java, when you want to do parallel processing, you execute your code in a Runnable either by extending the Thread class or implementing the R