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:
-
UI Thread Should Not be Blocked
-
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:-
-
Using Worker Thread
-
Using Async Task
-
Thread Safe method
-
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.