You are on page 1of 33

Threading and AsyncTask

In computer science, a thread of execution is the smallest


sequence of programmed instructions.

BY : ENG. HAZEM A. AL REKHAWI 2


• Basically is UI thread and the main thread is same in the android
OS, When application is launch in default process created in
android OS.
• Inside the process one main thread is created.
• For better clarity looks below the diagram.

BY : ENG. HAZEM A. AL REKHAWI 3


BY : ENG. HAZEM A. AL REKHAWI 4
• The Main thread, that is responsible for handling the UI events like Draw,
Listen and receive the UI events.
• And also it is responsible for interact with running components of the UI
toolkit for the corresponding application that belongs to.

BY : ENG. HAZEM A. AL REKHAWI 5


• Every UI actions are handled by the Main thread, So if our application fails to
respond the event about 5 seconds android will show the error “Android Not
Responding“ ANR.

• So only it is widely suggested to do the light processes in the UI thread.

BY : ENG. HAZEM A. AL REKHAWI 6


UI Thread and Main Thread

BY : ENG. HAZEM A. AL REKHAWI 7


BY : ENG. HAZEM A. AL REKHAWI 8
Example 2

BY : ENG. HAZEM A. AL REKHAWI 9


BY : ENG. HAZEM A. AL REKHAWI 10
What is Worker Thread in Android ?
• Worker threads are background threads. They are the threads that are created
separately, other than the UI thread.

BY : ENG. HAZEM A. AL REKHAWI 11


BY : ENG. HAZEM A. AL REKHAWI 12
We can update android UI from background thread by using:
• runOnUiThread
• Post
• PostDelayed
• Handler

BY : ENG. HAZEM A. AL REKHAWI 13


Example 4

BY : ENG. HAZEM A. AL REKHAWI 14


Example 5

BY : ENG. HAZEM A. AL REKHAWI 15


UI Thread and Main Thread

BY : ENG. HAZEM A. AL REKHAWI 16


BY : ENG. HAZEM A. AL REKHAWI 17
• AsyncTask enables proper and easy use of the UI thread. This class allows to
perform background operations and publish results on the UI thread without
having to manipulate threads and/or handlers.
• AsyncTask is designed to be a helper class around Thread and Handler and
does not constitute a generic threading framework.
• AsyncTasks should ideally be used for short operations (a few seconds at the
most.)

BY : ENG. HAZEM A. AL REKHAWI 18


• onPreExecute() : This method contains the code which is executed before the
background processing starts.
• doInBackground() : This method contains the code which needs to be
executed in background. In this method we can send results multiple times to
the UI thread by publishProgress() method. To notify that the background
processing has been completed we just need to use the return statements.

BY : ENG. HAZEM A. AL REKHAWI 19


• onProgressUpdate() : This method receives progress updates from
doInBackground method, which is published via publishProgress method,
and this method can use this progress update to update the UI thread.
• onPostExecute() : This method is called after doInBackground method
completes processing. Result from doInBackground is passed to this method.

BY : ENG. HAZEM A. AL REKHAWI 20


The three generic types used in an android AsyncTask class are given below :
• Params : The type of the parameters sent to the task upon execution.
• Progress : The type of the progress units published during the background
computation.
• Result : The type of the result of the background computation.

BY : ENG. HAZEM A. AL REKHAWI 21


Example 1

You can calling it by:


DownloadAsyncTask().execute()

BY : ENG. HAZEM A. AL REKHAWI 22


• Sometimes we need a function where we can pass n number of parameters,
and the value of n can be decided at runtime. Kotlin provides us to achieve
the same by defining a parameter of a function as vararg.
• We can pass n number of parameters to a vararg variable of the defined
datatype or even of a generic type.

BY : ENG. HAZEM A. AL REKHAWI 23


BY : ENG. HAZEM A. AL REKHAWI 24
Execution
Steps

BY : ENG. HAZEM A. AL REKHAWI 25


Threading
Type for
methods

BY : ENG. HAZEM A. AL REKHAWI 26


AsyncTask
Example 2

BY : ENG. HAZEM A. AL REKHAWI 27


You can call it by:
MyAsyncTask().execute(100)

BY : ENG. HAZEM A. AL REKHAWI 28


BY : ENG. HAZEM A. AL REKHAWI 29
AsyncTask
Example 3

BY : ENG. HAZEM A. AL REKHAWI 30


Void is Something!

You can calling it by:


MyAsyncTask().execute()

BY : ENG. HAZEM A. AL REKHAWI 31


BY : ENG. HAZEM A. AL REKHAWI 32
hrekhawi@iugaza.edu.ps
h.alrekhawi@gmail.com

BY : ENG. HAZEM A. AL REKHAWI 33

You might also like