Architecture Components

Threads and Processes in Android

As a developer there is quite a lot of confusion going on regarding, how are thread and process behave when application starts. I will try it very clear about what is the thread? and what is the process?

Main Thread or UI Thread

Now when we start our application, what happened in that in android framework? It simply creates a default process for our application and within the default process, a thread is actually created. The default thread is known as Main Thread or UI thread.

So right now just remember when our application starts a default process is created and default thread knows as Main Thread or UI Thread attached to our process.

Now within the main thread, all the application components are present. So we can say that all the application components run in same process, Now what is the mean of application components.

The application components I’m talking about are the Activites, Services, Broadcast Receiver and also the ContentProvider. So all of these application components present in the main thread. And suppose later you create another activity such as LoginActivity then this application component as well will operate within this main thread or UI thread and will use same default process the applications.

Now tell we are having just one thread that is the main thread. Now android frameworks give freedom to create as many threads we want to. Apart from main thread if we create another thread that thread will we know as the background thread or worker thread and you want to create another thread then absolutely allows to-do.

Purpose of Main Thread and Background Thread

Now, what is purpose of main thread and background thread within our application? Lets us understand this with help of below example

what is thread , what is process

Suppose this is our application so as usual whenever application start a default process is created and within this process, by default, we have the Main Thread (UI Thread ), within wish all application components are present. Now within the main UI Thread, you can perform only short running operation.

thread-process

Lets us assume in this arrow in our main thread so within main UI thread you can only perform small operation that takes less number of time as for example you can perform operation such as UI interactions like click on the button some zoom in zoom out animation an so on.

That is all UI interaction takes place in main UI thread. Apart from these small operations, you can perform some mathematical operation such as addition, multiplication, division and so on. Along with this, you can also write some code that is our logical operation code such as if-else condition, statement, for loop, while loop and so on. So all these small operations that consume less number of time will be done in the Main UI thread.

What about the background thread?

Which is also known as worker thread. So right now let’s represent with help of second arrow in diagram.

Now the worker and background thread always remember is used to perform a long-running operation such as heavy network operation, for example, download and upload of a file. Playing music in the background. Along with this heavy database query. So all the operation that takes a lot of time to execute. Should always perform be performed background thread or worker thread. The diagram says it all.

I think, now it is petty clear what is thread and what is the two different type of thread. That is the Main UI thread and worker or background thread

So few keypoint to related thread

  • Each Android app has a process – when your application starts it has its own default Linux process
  • Each of the processes has a default Main Thread attached to it
  • Main Thread is also known as UI Thread – because all the user interaction takes place in UI thread
  • All android architecture components such as Activity, Service, Broadcast Receiver and Content Provider run in main thread as simple as that
  • Performing operation
    • Use main thread to perform short operations
    • Use background thread to perform long operation
  • Never execute heavy operations in Main UI thread
    • Doing so will block the UI and app will freeze

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Saket Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Saket
Guest
Saket

Good post, nicely explained, but i expected to get example (code) for creating new thread and executing our function on it.