11 - Performance and Multithreading, Android Graphics
11 - Performance and Multithreading, Android Graphics
APPLICATION
DEVELOPMENT
By Rohit Singh
Department of Computer Engineering
National Institute of Technology, Kurukshetra
PERFORMANCE AND
MULTITHREADING
Developers multithread mobile applications in order to improve their performance and
usability.
By spinning off processor or resource-intensive tasks into their own threads, the rest of the
program can continue to operate while these processor intensive tasks finish working.
This can include separating the thread for the graphical user interface from the threads that
fetch network data.
The JVM automatically manages these multiple tasks, so Android developers do not have to
concern themselves with optimizing thread performance for a particular set of hardware.
PERFORMANCE AND
MULTITHREADING
When application component starts and the application does not have any other components
running, the Android system starts:
a new Linux process with a single thread of execution (called the "main" thread).
to control any certain component belongs to which process, use manifest file.
The manifest entry for each type of component element— <activity>, <service>,
<receiver>, and <provider>— supports an android:process attribute that can specify a
process in which that component should run.
<application> element also supports an android:process attribute, to set a default value that applies to
all components.
Main thread is very important because it is incharge of dispatching events to the appropriate user
interface widgets
PERFORMANCE AND
MULTITHREADING
A thread is a unit of a process and multiple such threads combine to form a process.
When a process is broken, the equivalent number of threads are available.
The main thread is also sometimes called the UI thread.
All components that run in the same process are instantiated in the UI thread.
For instance, when the user touches a button on the screen, app's UI thread dispatches the
touch event to the widget, which in turn sets its pressed state and posts an invalidate request to
the event queue. The UI thread dequeues the request and notifies the widget that it should
redraw itself.
There are simply two rules:
1. Do not block the UI thread, it will present "application not responding" (ANR) dialog.
2. Do not access the Android UI toolkit from outside the UI thread.
PERFORMANCE AND
MULTITHREADING
To prevent blocking, create "background" or "worker" threads.
One can update the UI from UI thread or "main" thread only.
To access the UI thread from other threads use following method:
Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)
PERFORMANCE AND
MULTITHREADING
Threads and app activity lifecycles:
The app lifecycle can affect how threading works in your application.
Decide, if a thread should, or should not, persist after an activity is destroyed.
Threads continue to execute, uninterrupted, regardless of the creation or destruction of activities, although
gets terminated together with the application process once there are no more active application components.
It’s important to set threads priority using setThreadPriority()
• If set too high, Thread may interrupt the UI thread.
• If set too low, will make async tasks (such as image loading) slower.
By default, system assigns each thread its own priority value, using the Process class.
• THREAD_PRIORITY_DEFAULT represents the default value for a thread.
• THREAD_PRIORITY_BACKGROUND for threads that are executing less-urgent work.
• THREAD_PRIORITY_LESS_FAVORABLE and THREAD_PRIORITY_MORE_FAVORABLE constants as incrementers
to set relative priorities.
(Learn more at https://developer.android.com/reference/android/os/Process)
PERFORMANCE AND
MULTITHREADING
A HandlerThread is effectively a long-running thread that grabs work from a queue and
operates on it.
For example, when app delegates the Camera.open() command, the associated
onPreviewFrame() callback lands on the handler thread, rather than the UI thread. So, while
doing long-running work, handler thread may be a better solution.
ThreadPoolExecutor is a helper class to make this process easier. This class manages:
• the creation of a group of threads.
• sets their priorities.
• manages how work is distributed among those threads.
PERFORMANCE AND
MULTITHREADING
A HandlerThread is effectively a long-running thread that grabs work from a queue and
operates on it.
For example, when app delegates the Camera.open() command, the associated
onPreviewFrame() callback lands on the handler thread, rather than the UI thread. So, while
doing long-running work, handler thread may be a better solution.
ThreadPoolExecutor is a helper class to make this process easier. This class manages:
• the creation of a group of threads.
• sets their priorities.
• manages how work is distributed among those threads.
PERFORMANCE AND
MULTITHREADING
How many threads should you create?
• Code has the ability to create hundreds of threads, but it creates performance issues.
• App shares limited CPU resources with background services, audio engine, networking, and
more.
• CPUs really only have the ability to handle a small number of threads in parallel.
• Everything runs on priority and scheduling, so it’s important to only create as many threads
as your workload needs.
• Threads also takes memory, minimum of 64k of memory.
• If app can reuse an existing threadpool, than reuse to increase performance by reducing load
on memory and processing resources.
ANDROID GRAPHICS
Graphics are images or visual representation of objects as well as shapes displayed on screen.
android.graphics provides low level graphics tools such as canvases, color filters, points, and rectangles
that let you handle drawing to the screen directly.
Camera A camera instance can be used to compute 3D transformations and generate a matrix that can be applied, for instance, on a Canvas.
Color The Color class provides methods for creating, converting and manipulating colors.
ColorFilter A color filter can be used with a Paint to modify the color of each pixel drawn with that paint.
ColorMatrix 4x5 matrix for transforming the color and alpha components of a Bitmap.
FontVariationAxis Class that holds information about single font variation axis.
AnimationDrawable An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a
View object's background.
BitmapDrawable A Drawable that wraps a bitmap and can be tiled, stretched, or aligned.
Drawable A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the
type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an
underlying visual resource that may take a variety of forms.
Icon An umbrella container for several serializable graphics representations, including Bitmaps, compressed bitmap images
(e.g. JPG or PNG), and drawable resources (including vectors).
PaintDrawable Drawable that draws its bounds in the given paint, with optional rounded corners.
PictureDrawable Drawable subclass that wraps a Picture, allowing the picture to be used wherever a Drawable is supported.
ANDROID GRAPHICS
android.graphics.drawable.shapes contains classes for drawing geometric shapes.
Shape Defines a generic graphical "shape."Any Shape can be drawn to a Canvas with its own draw() method,
but more graphical control is available if you instead pass it to a ShapeDrawable.
ANDROID GRAPHICS
android.graphics.pdf contains classes for manipulation of PDF content.
PdfDocument This class enables generating a PDF document from native Android content.
5. An agent can send the requests to a computing system as well as generate responses for
requests from the system.
6. An agent thus has certain similarities to peer-to-peer architecture.
7. The connection protocol and the connecting network between host and source are immaterial
THANK YOU