3. � UIThread e Multithreading Android
� AsyncTask
� HandlerThread
� Service e IntentService
Agenda
4. UI Thread e il multithreading
Threads in Android
5. Main Thread (o UI Thread)
Processo
Main Thread (UI Thread)
System Events Input Events ApplicationService Alarm UI Drawing
Nostro Codice
6. Disegno della UI
Processo
Main Thread (UI Thread)
App Events UI Drawing UI DrawingUI Drawing UI Drawing UI Drawing
{
16 ms
{
16 ms
{
16 ms
{
16 ms
{
16 ms
1000 ms / 60 frames = 16,666666667 ms / frame
7. Disegno della UI - Dropped Frames
Processo
Main Thread (UI Thread)
App Events UI Drawing UI Drawing UI DrawingNostra fantastica funzione!
{
16 ms
{
16 ms
{
16 ms
{
16 ms
{
16 ms
Dropped Frames
Input event
11. � Comunicazione HTTP
� Lettura / Scrittura di un file
� Operazioni su Database
� Lettura / Scrittura nelle SharedPreferences
� Elaborazione di immagini
� Parsing di testo / JSON / XML
� ...tutto ciò che non riguarda la UI ;-)
Quali operazioni gestire in background thread
12. Cosa ci mette a disposizione Android per
gestire i threads?
13. AsyncTask
Permette di eseguire il lavoro in background e pubblicare i risultati sul UIThread senza gestire
direttamente i threads
HandlerThread
Permette di eseguire dei tasks in sequenza in una MessageQueue
IntentService
Permette di eseguire Intents fuori dal UIThread
Componenti per gestire i threads
15. � I componenti core del multithreading nel SDK di Android sono:
� Looper, Handler, Message e MessageQueue
� Lo UIThread è basato su tali componenti
� Tutti i worker threads, creati con i componenti messi a
disposizione dal SDK, sono costruiti sopra i componenti core
� Permette di rendere safe la gestione dei threads
Il core del multithreading in Android
16. Dettaglio del core del multithreading
Looper MessageQueue MessageHandler
1 1 0…*11…* 1
20. Gestire il lavoro pesante in background e interagire con la UI
AsyncTask
21. � Si crea estendendo la classe AsyncTask<Params, Progress,
Result>
� E’ ideale per operazioni non molto lunghe e che aggiornano la UI
� Di default più esecuzioni di un AsyncTask sono eseguite in
sequenza
� E’ possibile interrompere un AsyncTask
� ATTENZIONE! - E’ facile creare dei memory leaks!!!
AsyncTask - Cos’è?
22. Lavorare in background con un AsyncTask
Main Thread (UI Thread)
onPreExecute()
Worker Thread
doInBackground(){
Nostra fantastica funzione!
}
onPostExecute()onProgressUpdate() onProgressUpdate()
25. � Si crea estendendo la classe HandlerThread oppure è possibile
utilizzare la classe fornita dal SDK
� E’ ideale per eseguire lunghe operazioni in background
mantenendo il controllo dell’ordine di esecuzione
� ATTENZIONE! - definire la priority. Di default ha la stessa priority
del UIThread (usare la priority di Linux es.:
Process.THREAD_PRIORITY_BACKGROUND)
� ATTENZIONE! - invocare i metodi quit() o quitSafely()
quando si vuole terminare il thread!!!
HandlerThread - Cos’è?
28. � Si creano estendendo rispettivamente le classi Service e
IntentService
� Devono essere dichiarati nel AndroidManifest.xml con il tag
<service>
� Sono ideali per eseguire lunghe operazioni in background
nascoste all’utente o invocabili da altre applicazioni
� Se il processo è terminato dal sistema operativo hanno la
possibilità di ripartire automaticamente
� ATTENZIONE! - Il Service è eseguito sullo UI Thread
� ATTENZIONE! - L’IntentService estende Service ed è reso
disponibile dal SDK per gestire operazioni su un Worker Thread
Service e IntentService - Cosa sono?
29. Service - Esempio
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) { /* Unico metodo obbligatorio da implementare */}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
Intent intent = new Intent(context, MyService.class);
context.bindService(intent, mConnection,
Context.BIND_AUTO_CREATE);
Start Service
Bind Service
30. Data la sequenza di metodi invocati: startService(),
startService(), bindService(), stopService(),
unbindService(), stopService()
Quando viene invocato il metodo onDestroy() del Service?
A.Al primo stopService()
B.Dopo il metodo unbindService()
C.Dopo l’esecuzione dei due stopService()
Quiz Time
https://medium.com/google-developers/developing-for-android-i-understanding-the-mobile-context-fd2351b131f8#.kcbu40usv
ciclo : Measure->Layout->Draw
In onDraw(Canvas) Canvas object generates (or updates) a list of OpenGL-ES commands (displayList) to send to the GPU.
RenderThread (added in L) is a thread helping the busy UI thread with the conversion of display lists to OpenGL commands, and sends them to the GPU. During which, the UI thread can start processing next frame.