Reader small image

You're reading from  Android High Performance Programming

Product typeBook
Published inAug 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785288951
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Emil Atanasov
Emil Atanasov
author image
Emil Atanasov

Emil Atanasov is an IT consultant with broad experience in mobile technologies. He has been exploring the field of mobile development since 2006. Emil has a MSc in Media Informatics from RWTH Aachen University, Germany and a MSc in Computer Science from Sofia Unversity "St. Kliment Ohridsky", Bulgaria. He has worked for several huge USA companies and has been a freelancer for several years. Emil has experience in software design and development. He was involved in the process of redesigning, improving and creating a number of mobile apps. Currently, he is focused on the rapidly growing mobile sector and manages a great team of developers that provides software solutions to clients around the world. As an Android team leader and project manager, Emil was leading a team that was developing a part of the Nook Color firmware -a e-magazine/ e-book reader, which supports the proprietary Barnes & Nobel and some other e-book formats. He is one of the people behind the "Getting Started with Flurry Analytics" book. He also contributed largely to the book "Objective C Memory Management". "I want to thank my family and friends for being so cool. Thank you for supporting me even though I'm such a bizarre geeky person, who is spending most of the time in the digital world. Thank you, guys!"
Read more about Emil Atanasov

Enrique López Mañas
Enrique López Mañas
author image
Enrique López Mañas

Enrique Lpez Maas is a Google Developer Expert and independent IT consultant. He has been working with mobile technologies and learning from them since 2007. He is an avid contributor to the open source community and a FLOSS (Free Libre Open Source Software) kind of guy, being among the top 10 open source Java contributors in Germany. He is a part of the Google LaunchPad accelerator, where he participates in Google global initiatives to influence hundreds of the best startups from all around the globe. He is also a big data and machine learning aficionado. In his free time he rides his bike, take pictures, and travels until exhaustion. He also writes literature and enjoys all kinds of arts. He likes to write about himself in third person. You can follow him on Twitter (@eenriquelopez) to stay updated on his latest movements.
Read more about Enrique López Mañas

Diego Grancini
Diego Grancini
author image
Diego Grancini

Diego Grancini has a degree in telecommunications and IT engineering from Perugia University. He has developed his skills on Android development for more than six years leading and contributed to several projects, teaching and sharing his skills during his career. He joined Engineering Ingegneria Informatica S.P.A. after his degree, defining his own knowledge about Java and Android development working as the lead Android developer for years. Then he joined J.P. Morgan & Chase, strengthening his skills about security and performance in software development and Android platform in particular.
Read more about Diego Grancini

View More author details
Right arrow

Chapter 5. Multithreading

When the mobile phone market started falling and the smartphones one boomed, it was clear that users needed a large computing capacity on a mobile device. The growing demand for calculation and the availability of suitable hardware has led to multicore CPUs on the devices, allowing parallel execution of multiple tasks. Android engineers knew this before it happened. Moreover, that is why we have many options to execute different tasks at the same time, with great flexibility and a lot of different components to choose to apply to our multithreading strategy. However, are we doing well? To answer this question, we will see all the threading facets, from the basics of threading Java framework inherited by the Android platform to all of the classes Android provides for this aim. We will also see how Android handles its processes and how we can correctly choose the right component to use in different situations, because not all of them are interchangeable.

It might seem...

Walkthrough


We will define here all we need to know to deal with a multithreading environment like Android. It is crucial to understand what a thread is and what problems can occur while dealing with one. That is why we are focusing on the Java framework for a while, because every Android developer should know those notions, and then we will focus on how that platform is defined in Android and integrated with more objects. This provides multiple ways to separate execution at all levels, from the multithreading inside an application to the communication between different processes, defining a particular language to reach the target. So, let's see what we are talking about.

Threading basics

We could think of a thread as a portion of instructions executed sequentially. These instructions are translated to be performed by the hardware of the device. When there are multiple portions of instructions to be executed, then the environment is called multithreaded. This technique is helpful to speed...

Best practices


With the threading concepts in mind, let's go through the code to understand how Google has improved the multithreading framework inherited from Java and which API the Android platform provides to the developer to deal with the main problem of separation between UI thread and worker threads. We will also see what problems may derive from this and which solutions Android introduced during its development.

We will then deal with advanced techniques to manage main components and the AIDL and messenger for inter-process communications.

Threads

The standard Java threads are the base for the other frameworks we will see in the following pages. They wrap threads or runnables to achieve some platform requirements like the communication with the UI thread. They are still the lightweight solution for brief background operations that don't need to be notified to the UI thread.

Tip

As a general rule to observe, when using threads, avoid the synchronization inside loops because acquiring and...

Debugging tools


We have seen different techniques to create our multithreaded application and when to use them. The right structure to use depends on a lot of various factors; it's up to the developer to treasure what we said and apply the appropriate framework in each case. However, our primary goal is to provide a fluid UI to the user, avoiding the Application Not Responding dialog, lags, and any obstacle to the correct execution of the UI thread. For this, Android provides some tools that we are about to see in the following pages.

StrictMode

We already dealt with this tool in Chapter 4, Memory, while talking about memory leaks. However, this tool can also help us find and notify threading problems.

To use it we need to know what we are searching for and how to be informed that a threading problem is occurring. For this, we need to set ThreadPolicy to the StrictMode class, using a ThreadPolicy.Builder class. This way we can be notified of the following occurring problems:

  • detectCustomSlowCalls...

Summary


Starting from the basic definition of thread, through the Java threading framework, we got to talk about Android process management, thread types, and the messaging framework. We analyzed pitfalls in multithreading environments, defining thread safety. Indicating what we can do with multiple threads in an application, we described the primary goal for an Android developer from a multithreading performance point of view. The UI thread should just deal with the UI, and any other operation should be executed in the background using a worker thread. Because of this, we evaluated a lot of different solutions provided by the platform for various situations, defining when they can or shouldn't be used. Anyway, the choice of the right framework depends on the particular situation the developer is dealing with, but, knowing all the possibilities, he has more chances to improve the performance of the application. At the end of the chapter, we saw which tools we have for the detection of threading...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android High Performance Programming
Published in: Aug 2016Publisher: PacktISBN-13: 9781785288951
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Emil Atanasov

Emil Atanasov is an IT consultant with broad experience in mobile technologies. He has been exploring the field of mobile development since 2006. Emil has a MSc in Media Informatics from RWTH Aachen University, Germany and a MSc in Computer Science from Sofia Unversity "St. Kliment Ohridsky", Bulgaria. He has worked for several huge USA companies and has been a freelancer for several years. Emil has experience in software design and development. He was involved in the process of redesigning, improving and creating a number of mobile apps. Currently, he is focused on the rapidly growing mobile sector and manages a great team of developers that provides software solutions to clients around the world. As an Android team leader and project manager, Emil was leading a team that was developing a part of the Nook Color firmware -a e-magazine/ e-book reader, which supports the proprietary Barnes & Nobel and some other e-book formats. He is one of the people behind the "Getting Started with Flurry Analytics" book. He also contributed largely to the book "Objective C Memory Management". "I want to thank my family and friends for being so cool. Thank you for supporting me even though I'm such a bizarre geeky person, who is spending most of the time in the digital world. Thank you, guys!"
Read more about Emil Atanasov

author image
Enrique López Mañas

Enrique Lpez Maas is a Google Developer Expert and independent IT consultant. He has been working with mobile technologies and learning from them since 2007. He is an avid contributor to the open source community and a FLOSS (Free Libre Open Source Software) kind of guy, being among the top 10 open source Java contributors in Germany. He is a part of the Google LaunchPad accelerator, where he participates in Google global initiatives to influence hundreds of the best startups from all around the globe. He is also a big data and machine learning aficionado. In his free time he rides his bike, take pictures, and travels until exhaustion. He also writes literature and enjoys all kinds of arts. He likes to write about himself in third person. You can follow him on Twitter (@eenriquelopez) to stay updated on his latest movements.
Read more about Enrique López Mañas

author image
Diego Grancini

Diego Grancini has a degree in telecommunications and IT engineering from Perugia University. He has developed his skills on Android development for more than six years leading and contributed to several projects, teaching and sharing his skills during his career. He joined Engineering Ingegneria Informatica S.P.A. after his degree, defining his own knowledge about Java and Android development working as the lead Android developer for years. Then he joined J.P. Morgan & Chase, strengthening his skills about security and performance in software development and Android platform in particular.
Read more about Diego Grancini