Sie sind auf Seite 1von 27

Dispozitive mobile de calcul

Processes and Threads

Academia Tehnica Militara

source

Processes in Android
When an application is launched and no other
component of the application is running, a new process
is spawned from the zygote
If at least one component is running, then the
application is launched within the same process
Not only same process, but also same execution thread,
called main

Android components

Activities
Services
BroadcastReceivers
ContentProvider

Processes in Android
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.
Components can share processes
Apps can share processes IF they share the signature
certificate

Why would I have components


running different processes?

source

Because
Android cleans up by killing
processes that take up a lot of
resources
Split your app into edible
components, and have the
lighter ones run for longer
source

Process importance
Most important

1.
2.
3.
4.
5.

Foreground process
Visible process
Service process
Background process
Empty process
Least important

A process is foreground if any:


It hosts an Activity that the user is interacting with
It hosts a Service that's bound to the activity that
the user is interacting with
It hosts a Service that's running "in the foreground"
It hosts a Service that's executing one of its
lifecycle callbacks
It hosts a BroadcastReceiver that's executing its
onReceive() method

A process is visible if any:


It hosts an Activity that is not in the foreground, but
is still visible to the user (its onPause() method has
been called). This might occur, for example, if the
foreground activity started a dialog, which allows
the previous activity to be seen behind it.
It hosts a Service that's bound to a visible (or
foreground) activity.

Service process

A process that is running a service that has been


started with the startService() method and does not
fall into either of the two higher categories
Why?

Service process
Service processes are not directly
tied to anything the user sees, but
they are generally doing things that
the user cares about (such as
playing music in the background or
downloading data on the network)
source

Background process

A process holding an activity that's not currently


visible to the user
Many of these are existent at a given time, to
optimize launch time
LRU list to destroy them

Empty process

A process that doesn't hold any active application


components.
The only reason to keep this kind of process alive is
for caching purposes, to improve startup time the
next time a component needs to run in it.

Empty memory is wasted memory


To run an app it has to be in memory
Most apps run frequently throughout the day
Killing them and freeing resources will cause the
system to reload it in some time, doing extra clocks
to re-fetch what was earlier cleared
All modern OSs cache processes by keeping them
in RAM
If your Android is slow, is running out of RAM

Threads

source

Threads
All processes have one thread called main
It is responsible with everything in the
eventloop, including drawing stuff
Often referred to as the UI thread
Jamming this one blocks the UI

Main thread
All components of a process share this
thread
System calls from are dispatched from this
thread
Methods that respond to system callbacks
run on this thread

Main thread
Bad programmers always
did network IO on main
thread
IDE now throws compile
exception when you do
such a horrible thing!
source

Main thread

Since main thread drew the UI, it owns the


views
No other thread can touch the UI

Background threads (workers)


source

Background threads

Overcomes the single thread model


java threads work just fine

Background threads
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
mImageView.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
}
}).start();
}

Async tasks
AsyncTask allows you to perform
asynchronous work on your user interface.
It performs the blocking operations in a
worker thread and then publishes the results
on the UI thread, without requiring you to
handle threads and/or handlers yourself.

Async tasks

Async tasks
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>{
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}

Das könnte Ihnen auch gefallen