Saturday 7 May 2016

Processes and Threads in Android

Hello Guys!!! Wishing you all Doing Well !!!!

Today I am going to discussion Process and Thread in Android application.
When an android application clicked and open by user. Then android application starts its core component in a new Linux process for the application with a single thread of execution. the only condition is that application does not have any other components running at that time. If not then it starts it component in that time running process.

                                       Example Source Code on Github
Processes
By default, all components of the same application run in the same process and most applications should not change this. However, if we find that our need to control which process a certain component belongs to, We can do so in the manifest file.
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.
We can set this attribute so that each component runs in its own process or so that some components share a process while others do not.
We can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Android system is deciding which processes to kill, based on apps  relative importance to the user. For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities.
Some Important points :-

  1. The Android system tries to maintain an application process for as long as possible, 
  2. To reclaim memory for new or more important processes, it follow hierarchy system. 
  3. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" 
  4. Processes with the lowest importance are eliminated first from hierarchy
There are five levels in the importance hierarchy. The following list presents the different types of processes in order of importance (the first process is most important and is killed last):

Process lifecycle

  1. Foreground process
  2. Visible process
  3. Service process
  4. Background process
  5. Empty process
For detail you can click here

Threads
When an application is launched, the system creates a thread of execution for the application, called "main." This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. 
It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.

 The Andoid UI toolkit is not thread-safe. So, we must not manipulate our UI from a worker thread. We  must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android's single thread model:
  1. Do not block the UI thread
  2. Do not access the Android UI toolkit from outside the UI thread
Android offers several ways to access the UI thread from other threads. Here is a list of methods that can help:

  1. Activity.runOnUiThread(Runnable)
  2. View.post(Runnable)
  3. View.postDelayed(Runnable, long)
  4. Handler
  5. AsyncTask


3 comments:

  1. hi sourabh please can you share a tutorial in which one can set swipeable frames in while my camera is open , n then save the image,but can swipe frames afterwards too ...

    ReplyDelete
  2. Hi Khalid you can check my app
    https://play.google.com/store/apps/details?id=com.sks.realapp101.photoframing
    If you are wanting this type feature then i will share code in my blog.

    Otherwise you can also refer my previous blog
    http://saurabhsharma123k.blogspot.in/2013/05/set-live-frame-on-camera-capturing.html

    ReplyDelete
  3. Very useful information that you have shared and it is very useful to me.Thanks for sharing the information with us.

    ios app development company in chennai

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...