Sayfalar

26 Eylül 2014 Cuma

Multi-Threading In Android and Handler Class - I

Multi-Threading In Android and Handler Class - I 

Multi-Threading in Android

When I started research this topic first question about threading is why i need threads? There is always tags like fast,usable,multicore .etc. All of them good tags which i interest in all of type programming language :)With multi-thereading our app's will be more fast and more useable,but WHY ? Java,C or C++ are written in very past , and all of them create for single core processor but today we have a lot of processer even in mobile phones.And today we can use multi-cores with same ways in these languages.In Java multi-threads is a way to use this power.In android we can use regular threads as in Java but we must be consider about Android threads(UI Threads) and some classes.

In android there is a class which responsible to handle  topic about threads,and  it's name is Handler class:).With messages all of the threads comminicate with each other in this class.It is simple to create a handler

Handler handlerObject = new Handler(); 

We use the Runnablers for multi-threadings.They are type of thread whic can execute their body or call another functions etc.They include a abstract method void run() all of the threads must be have this method.

class ClassName implements Runnable 
{
Public void run() 
{
// Body of method
}
}

When we use the Handler in our android project we must be add import android.os.Handler; lines in aour projects.
And in our project we create a handler like on the top.

Handler maygun = new Handler(); 

And then we can create a Runnable

Runnable run = new Runnable() 
{
public void run()
{
// Our functions
  }
};


I will continue with Handler Mechanism and AsyncTask in next blog post.

Hiç yorum yok:

Yorum Gönder