Thread Handling in Android

Posted By : Gaurav Jain | 11-Mar-2016

 

Making efficient use of the threads is one of the major challenges encountered by the android developers. As the android system initializes, the main thread components begin to  execute. Android system has at least one main thread which is created at the same time the application class is created for the app. The main task of this thread is to handle user interaction. Some of the tasks include drawing pixels on the screen during the process of system boot up, launch application activities etc.

 

Whenever a code is added to the main activity, it runs only when there are some resources left over after the UI thread has been executed. This ensures lag free performance of the main thread. To make better use of the leftover resources of the main thread, developers might try to put some code into the main thread. Apps can be featured with additional attributes so as to utilize the resources of the main thread. However, if we make use a fair amount of resources, and our code runs for a longer period of time, then, it is likely that main thread may disregard our application and that might also result our application to be crashed.

 

For instance, overutilization of resources in main thread by a network component can generate an exception “NetworkOnMainThreadException”. One of the questions that is likely to arise is “How come a shared resources have impact?”. The answer to this question is that the shared memory of the main thread lets you utilize its resources to some extent. But if our application makes continuous resource utilization in the UI section, it might also have an impact on the visuals and the graphics performance.

The Android UI toolkit in the main thread is not thread-safe. And for the reason. one must not manipulate the UI from a worker thread. All the manipulation must be done to the user interface from the UI thread.

So, basically, there are two conditions to Android's single thread model:

  1. UI Thread Should Not be Blocked

  2. Android UI Toolkit must not be accessed from outside the UI kit

In order to avoid the blocking of UI Thread, some other threading mechanism are used. They are:-

  1. Using Worker Thread

  2. Using Async Task

  3. Thread Safe method

  4. Interprocess communication

Oodles Technologies is one of the leading IT companies with a huge client base on Android Development. We aim to develop robust and well-built android applications based on the varying client’s requirement. Our expert team has years of experience in developing cost-effective and time-efficient apps for business enterprises as well as individual firms.  




 

About Author

Author Image
Gaurav Jain

As the head of your Digital Marketing Business Unit, Gaurav likely plays a crucial role in developing and executing marketing strategies to drive online visibility, customer engagement, and business growth. He is responsible for team management, setting marketing goals and budgets, collaborating with other departments, staying updated on industry trends, and exploring new opportunities for business growth. Within the Digital Marketing Business Unit involves strategic planning, overseeing various marketing activities, and driving results through effective online marketing techniques.

Request for Proposal

Name is required

Comment is required

Sending message..