Site icon C1CTech

Android Thread Example

<p>In this article&comma; we will talk about what is thread and how to create a separate worker thread in our android application to achieve asynchronous processing with a basic example&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Thread<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Android follows a multitasking design&period; This allows a number of applications to run at the same time&period; Let&&num;8217&semi;s say you are playing a song in Ganna app and you are typing something on WhatsApp at the same time&period; You are using two tasks at the same time which we called as multitasking&period; But let&&num;8217&semi;s say if we talk about one <strong><span style&equals;"color&colon; &num;008000&semi;">task&sol;process&sol;App<&sol;span><&sol;strong> i&period;e WhatsApp then in WhatsApp&comma; parallelly while you are typing a word at the same time it is showing some words related to what you are typing and this is achieved by making use of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>threads<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>A thread is a lightweight subprocess&comma; the smallest unit of processing&period; It is a separate path of execution&period;<&sol;p>&NewLine;<p>Threads are independent&period; If there occurs exception in one thread&comma; it doesn&&num;8217&semi;t affect other threads&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1198" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Screenshot-2019-08-26-15&period;31&period;16&period;png" alt&equals;"Screenshot 2019-08-26 15&period;31&period;16" width&equals;"1446" height&equals;"624" &sol;>As shown in the above figure&comma; At runtime&comma; an Android operating system is always hosting an app as a process&period; So each app gets a dedicated process to run itself&period; At any point in time&comma; it can host any no&period; of apps and for each app&comma; you have a separate process&period; If you consider one particular process then there can be any no&period; of threads inside it and one of the thread is Main thread &lpar;default thread comes with every application&rpar; or also referred to as UI thread&period; In this thread&comma; all of your UI tasks are getting executed like rendering a button&comma; click of a button&comma; everything that is related to UI happens in this particular thread&period;<&sol;p>&NewLine;<p>Android supports the usage of the <span style&equals;"color&colon; &num;008000&semi;"><strong>Thread<&sol;strong><&sol;span> class to perform asynchronous processing&period; Thread class provides constructors and methods to create and perform operations on a thread&period; Thread class extends <span style&equals;"color&colon; &num;000000&semi;"><strong>Object<&sol;strong><&sol;span> class and implements <strong>Runnable<&sol;strong> interface&period;<&sol;p>&NewLine;<p>Always remember the two most important rules when working with threads&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Don’t block the Main thread<&sol;li>&NewLine;<li>Don’t try and access the UI directly from the worker thread<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Whats the need of a thread or why we use Threads&quest;<&sol;span><&sol;strong><&sol;h4>&NewLine;<ul>&NewLine;<li>To perform asynchronous or background processing<&sol;li>&NewLine;<li>Increases the responsiveness of GUI applications<&sol;li>&NewLine;<li>Take advantage of multiprocessor systems<&sol;li>&NewLine;<li>Simplify program logic when there are multiple independent entities<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">The Application Main Thread<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>All apps&comma; when started&comma; run in a single Main thread&period; It’s also known as the User Interface &lpar;or UI&rpar; thread&period;<&sol;p>&NewLine;<p>Android modifies the user interface and handles input events from this <em>main thread<&sol;em>&period; Android collects all events in this thread in a queue and processes this queue with an instance of the <code>Looper<&sol;code> class&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1200" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Screenshot-2019-08-26-18&period;59&period;57&period;png" alt&equals;"Screenshot 2019-08-26 18&period;59&period;57" width&equals;"1548" height&equals;"676" &sol;><&sol;p>&NewLine;<p>By default&comma; all components &lpar;Activities&comma; Services&comma; Content provider&comma; Broadcast receivers&&num;8230&semi;&rpar; of the same application run in the same process and thread &lpar;called the &&num;8220&semi;main&&num;8221&semi; thread&rpar;&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-1199" src&equals;"https&colon;&sol;&sol;i0&period;wp&period;com&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Screenshot-2019-08-26-18&period;16&period;58-1-2279880879-1566823954707&period;png&quest;ssl&equals;1&amp&semi;w&equals;612" alt&equals;"Screenshot 2019-08-26 18&period;16&period;58 &lpar;1&rpar;" width&equals;"306" height&equals;"545" &sol;><&sol;p>&NewLine;<p>When we try to execute long-running operations in our application by default they are called in the main thread and are called synchronously&comma; which means that the UI will remain completely unresponsive until the operation completes&period; Running long operations on the app&&num;8217&semi;s main thread will freeze the app&&num;8217&semi;s user interface and after a couple seconds&comma; we get an &&num;8220&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">application not responding<&sol;span><&sol;strong>&&num;8221&semi; dialog message with the option to force quit the app&period;<&sol;p>&NewLine;<p>For example&comma; let us consider the below code&colon;<&sol;p>&NewLine;<pre>public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private static final String TAG &equals; "MainActivity"&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public void startThread&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; 10&semi; i&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; Log&period;d&lpar;TAG&comma; "startThread&colon; " &plus; i&rpar;&semi;&NewLine;&NewLine; try &lbrace;&NewLine; Thread&period;sleep&lpar;1000&rpar;&semi;&NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>In the above code on clicking the <strong>start<&sol;strong> button&comma; the <span style&equals;"color&colon; &num;008000&semi;"><strong>startThread<&sol;strong><&sol;span> method will be called&period; This method will print the log statement after every one second&comma; ten times&period;<&sol;p>&NewLine;<p>By default&comma; this method will be called inside the main thread&comma; synchronously and it will freeze the app until the operation is finished&period; So in between&comma; if we will try to interact with other UI events &lpar;on&sol;off switch&rpar; we get an &&num;8220&semi;<strong>application not responding<&sol;strong>&&num;8221&semi; dialog message with the options <strong><span style&equals;"color&colon; &num;008000&semi;">close app<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">wait<&sol;span><&sol;strong> like this&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1203" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Screenshot&lowbar;1566840402-576x1024&period;png" alt&equals;"Screenshot&lowbar;1566840402" width&equals;"266" height&equals;"473" &sol;>             <img class&equals;"alignnone wp-image-1201" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Screenshot&lowbar;1566840475&period;png" alt&equals;"Screenshot&lowbar;1566840475" width&equals;"267" height&equals;"475" &sol;><&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;"> What triggers the ANR dialog&quest;<&sol;span><&sol;strong><&sol;h4>&NewLine;<ul>&NewLine;<li>If an app can’t respond to user input within 5 seconds<&sol;li>&NewLine;<li>If Broadcast receivers haven’t finished executing within 10 seconds<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>To avoid this we have to move heavy operations like Network operations and database calls&comma; as well as loading of certain components onto a separate <strong><span style&equals;"color&colon; &num;008000&semi;">thread<&sol;span><&sol;strong>&period; A separate thread will avoid blocking the UI while they are being performed &lpar;i&period;e&period;&comma; they are performed asynchronously from the UI&rpar;&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating New Thread<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Thread implementation can be achieved in two ways&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>Extending the Thread class<&sol;li>&NewLine;<li>Implementing the Runnable Interface<&sol;li>&NewLine;<&sol;ol>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">1&rpar; By extending Thread class<&sol;span><&sol;strong><&sol;h4>&NewLine;<ul>&NewLine;<li>The class should extend the Thread class&period;<&sol;li>&NewLine;<li>The class should override the <span style&equals;"color&colon; &num;008000&semi;"><strong>run&lpar;&rpar;<&sol;strong><&sol;span> method&period;<&sol;li>&NewLine;<li>The functionality that is expected by the Thread to be executed is written in the run&lpar;&rpar; method&period;<&sol;li>&NewLine;<li>To start the thread&comma; create a new Thread object and then call <span style&equals;"color&colon; &num;008000&semi;"><strong>start&lpar;&rpar;<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">void start&lpar;&rpar;<&sol;span><&sol;strong>&colon; Creates a new thread and makes it runnable&period;<br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">void run&lpar;&rpar;<&sol;span><&sol;strong>&colon; The new thread begins its life inside this method&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private static final String TAG &equals; "MainActivity"&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void startThread&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; ExampleThread thread &equals; new ExampleThread&lpar;10&rpar;&semi;&NewLine;&NewLine; thread&period;start&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; class ExampleThread extends Thread &lbrace;&NewLine;&NewLine;&NewLine; int seconds&semi;&NewLine;&NewLine;&NewLine; ExampleThread&lpar;int seconds&rpar; &lbrace;&NewLine;&NewLine; this&period;seconds &equals; seconds&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void run&lpar;&rpar; &lbrace;&NewLine;&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; seconds&semi; i&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; Log&period;d&lpar;TAG&comma; "startThread&colon; " &plus; i&rpar;&semi;&NewLine;&NewLine; try &lbrace;&NewLine; Thread&period;sleep&lpar;1000&rpar;&semi;&NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1204" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Screenshot-2019-08-26-23&period;53&period;10&period;png" alt&equals;"Screenshot 2019-08-26 23&period;53&period;10" width&equals;"1392" height&equals;"314" &sol;><&sol;p>&NewLine;<h4><&sol;h4>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">2&rpar; By implementing Runnable interface<&sol;span><&sol;strong><&sol;h4>&NewLine;<ul>&NewLine;<li>The class should implement the Runnable interface<&sol;li>&NewLine;<li>The class should override the <strong><span style&equals;"color&colon; &num;008000&semi;">run&lpar;&rpar;<&sol;span><&sol;strong> method&period;<&sol;li>&NewLine;<li>The functionality that is expected by the Thread to be executed is put in the run&lpar;&rpar; method<&sol;li>&NewLine;<li>To start the thread&comma; pass the runnable to a new Thread object and then call <strong><span style&equals;"color&colon; &num;008000&semi;">start&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private static final String TAG &equals; "MainActivity"&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void startThread&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; ExampleRunnable runnable &equals; new ExampleRunnable&lpar;10&rpar;&semi;&NewLine; new Thread&lpar;runnable&rpar;&period;start&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; class ExampleRunnable implements Runnable &lbrace;&NewLine;&NewLine; int seconds&semi;&NewLine; &NewLine; ExampleRunnable&lpar;int seconds&rpar; &lbrace;&NewLine;&NewLine; this&period;seconds &equals; seconds&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public void run&lpar;&rpar; &lbrace;&NewLine;&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; seconds&semi; i&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; Log&period;d&lpar;TAG&comma; "startThread&colon; " &plus; i&rpar;&semi;&NewLine;&NewLine; try &lbrace;&NewLine; Thread&period;sleep&lpar;1000&rpar;&semi;&NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Extends Thread class vs Implements Runnable Interface<&sol;span><&sol;strong><&sol;h3>&NewLine;<ul>&NewLine;<li>Extending the <span style&equals;"color&colon; &num;008000&semi;"><strong>Thread<&sol;strong><&sol;span> class will make your class unable to extend other classes&comma; because of the single inheritance feature in  JAVA&period; However&comma; this will give you a simpler code structure&period; If you implement <strong><span style&equals;"color&colon; &num;008000&semi;">Runnable<&sol;span><&sol;strong>&comma; you can gain better object-oriented design and consistency and also avoid the single inheritance problems&period;<&sol;li>&NewLine;<li>If you just want to achieve basic functionality of a thread you can simply implement Runnable interface and override <strong><span style&equals;"color&colon; &num;008000&semi;">run&lpar;&rpar;<&sol;span><&sol;strong> method&period; But if you want to do something serious with thread object as it has other methods like <strong>suspend&lpar;&rpar;&comma; resume&lpar;&rpar;&comma; &period;&period;etc<&sol;strong> which are not available in Runnable interface then you may prefer to extend the Thread class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Worker threads<&sol;b><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">According to the <strong><span style&equals;"color&colon; &num;008000&semi;">single-threaded model<&sol;span><&sol;strong> described above&comma; it&&num;8217&semi;s important to the responsiveness of your application&&num;8217&semi;s UI that you do not block the UI thread&period; If you have operations to perform that are not instantaneous&comma; you should make sure to do them in separate threads &lpar;&&num;8220&semi;background&&num;8221&semi; or &&num;8220&semi;worker&&num;8221&semi; threads&rpar;&period;<&sol;p>&NewLine;<p class&equals;"p2">The other rule says&comma; that you cannot update the UI from any thread other than the UI thread or the &&num;8220&semi;main&&num;8221&semi; thread&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">For example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private static final String TAG &equals; "MainActivity"&semi;&NewLine; Button btn&lowbar;start&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine; btn&lowbar;start &equals; findViewById&lpar;R&period;id&period;btn&lowbar;start&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public void startThread&lpar;View view&rpar; &lbrace;&NewLine; ExampleThread thread &equals; new ExampleThread&lpar;10&rpar;&semi;&NewLine;&NewLine; thread&period;start&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; class ExampleThread extends Thread &lbrace;&NewLine;&NewLine;&NewLine; int seconds&semi;&NewLine;&NewLine;&NewLine; ExampleThread&lpar;int seconds&rpar; &lbrace;&NewLine;&NewLine; this&period;seconds &equals; seconds&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void run&lpar;&rpar; &lbrace;&NewLine;&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; seconds&semi; i&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; if &lpar;i &equals;&equals; 5&rpar; &lbrace;&NewLine; btn&lowbar;start&period;setText&lpar;"50&percnt;"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; Log&period;d&lpar;TAG&comma; "startThread&colon; " &plus; i&rpar;&semi;&NewLine;&NewLine; try &lbrace;&NewLine; Thread&period;sleep&lpar;1000&rpar;&semi;&NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &rcub;<&sol;pre>&NewLine;<p>In the above example&comma; the <strong>ExampleThread<&sol;strong> will execute the log statement up to i equals to 4 and when i equals to 5 our app will crash&period;<&sol;p>&NewLine;<p>In logcat&comma; you can see the error message &colon; <span style&equals;"color&colon; &num;ff0000&semi;"><strong>Only the original thread that created a view hierarchy can touch its views&period;<&sol;strong><&sol;span>It clearly says that you cannot update the UI from any thread other than the UI thread&period;<&sol;p>&NewLine;<p class&equals;"p2">To fix this problem&comma; Android offers several ways to access the UI thread from other threads&period; Here is a list of methods that can help&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li3"><strong><span class&equals;"s1" style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s2">Activity&period;runOnUiThread&lpar;Runnable&rpar;<&sol;span><&sol;span><&sol;strong><&sol;li>&NewLine;<li class&equals;"li3"><strong><span class&equals;"s1" style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s2">View&period;post&lpar;Runnable&rpar;<&sol;span><&sol;span><&sol;strong><&sol;li>&NewLine;<li class&equals;"li3"><strong><span class&equals;"s1" style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s2">View&period;postDelayed&lpar;Runnable&comma; long&rpar;<&sol;span><&sol;span><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>For example&comma; by using <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1"><span class&equals;"s2">View&period;post&lpar;Runnable&rpar; <&sol;span><&sol;span><&sol;strong><&sol;span><span class&equals;"s1" style&equals;"color&colon; &num;000000&semi;"><span class&equals;"s2">you can access the UI Thread inside other Thread&lpar;i&period;e  ExampleThread&rpar; as shown below&colon;<&sol;span><&sol;span><&sol;p>&NewLine;<pre>public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private static final String TAG &equals; "MainActivity"&semi;&NewLine; Button btn&lowbar;start&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine; btn&lowbar;start &equals; findViewById&lpar;R&period;id&period;btn&lowbar;start&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public void startThread&lpar;View view&rpar; &lbrace;&NewLine; ExampleThread thread &equals; new ExampleThread&lpar;10&rpar;&semi;&NewLine;&NewLine; thread&period;start&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; class ExampleThread extends Thread &lbrace;&NewLine;&NewLine;&NewLine; int seconds&semi;&NewLine;&NewLine;&NewLine; ExampleThread&lpar;int seconds&rpar; &lbrace;&NewLine;&NewLine; this&period;seconds &equals; seconds&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void run&lpar;&rpar; &lbrace;&NewLine;&NewLine; for &lpar;int i &equals; 1&semi; i &lt&semi;&equals; seconds&semi; i&plus;&plus;&rpar; &lbrace;&NewLine;&NewLine; if &lpar;i &equals;&equals; 5&rpar; &lbrace;&NewLine; btn&lowbar;start&period;post&lpar;new Runnable&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void run&lpar;&rpar; &lbrace;&NewLine; btn&lowbar;start&period;setText&lpar;"50&percnt;"&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; Log&period;d&lpar;TAG&comma; "startThread&colon; " &plus; i&rpar;&semi;&NewLine;&NewLine; try &lbrace;&NewLine; Thread&period;sleep&lpar;1000&rpar;&semi;&NewLine; &rcub; catch &lpar;InterruptedException e&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &rcub;<&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Thread Basic Example<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Let&&num;8217&semi;s create a new android application to understand the concept of asynchronous processing in Android applications&period;<&sol;p>&NewLine;<ol>&NewLine;<li>Create a new android application <span style&equals;"color&colon; &num;008000&semi;"><strong>Threadexample&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<&sol;li>&NewLine;<li>Open layout file <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main<&sol;span><&sol;strong> and write the below code&period; This file consists of one <strong>Imageview<&sol;strong> and one <strong>button<&sol;strong>&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;orientation&equals;"vertical"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine; &NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;image"&NewLine; android&colon;layout&lowbar;width&equals;"350dp"&NewLine; android&colon;layout&lowbar;height&equals;"350dp"&NewLine; android&colon;background&equals;"&commat;drawable&sol;border&lowbar;image"&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;download"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"10dp"&NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;blue&lowbar;bright"&NewLine; android&colon;onClick&equals;"downloadImage"&NewLine; android&colon;padding&equals;"10dp"&NewLine; android&colon;text&equals;"download image"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;pre>&NewLine;<p>3 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and write the below code&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre class&equals;"p1"><span class&equals;"s1"><b>package <&sol;b><&sol;span><span class&equals;"s2">trendlife&period;threadexample&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;app&period;Activity&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;graphics&period;Bitmap&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;graphics&period;BitmapFactory&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;os&period;Bundle&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;support&period;v7&period;app&period;AppCompatActivity&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;util&period;Log&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;view&period;View&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;widget&period;ImageView&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">android&period;widget&period;Toast&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">java&period;io&period;IOException&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">java&period;io&period;InputStream&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">java&period;net&period;HttpURLConnection&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">java&period;net&period;MalformedURLException&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>import <&sol;b><&sol;span><span class&equals;"s2">java&period;net&period;URL&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s1"><b>public class <&sol;b><&sol;span><span class&equals;"s2">MainActivity <&sol;span><span class&equals;"s1"><b>extends <&sol;b><&sol;span><span class&equals;"s2">AppCompatActivity &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">    <&sol;span>ImageView <&sol;span><span class&equals;"s3"><b>imageView<&sol;b><&sol;span><span class&equals;"s2">&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s4"><span class&equals;"Apple-converted-space">    <&sol;span><&sol;span><span class&equals;"s2">&commat;Override<&sol;span>&NewLine;&NewLine;<span class&equals;"s5"><span class&equals;"Apple-converted-space">    <&sol;span><&sol;span><span class&equals;"s1"><b>protected void <&sol;b><&sol;span><span class&equals;"s2">onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span><&sol;span><span class&equals;"s1"><b>super<&sol;b><&sol;span><span class&equals;"s2">&period;onCreate&lpar;savedInstanceState&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>setContentView&lpar;R&period;layout&period;<&sol;span><span class&equals;"s3"><b><i>activity&lowbar;main<&sol;i><&sol;b><&sol;span><span class&equals;"s2">&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span><&sol;span><span class&equals;"s3"><b>imageView <&sol;b><&sol;span><span class&equals;"s2">&equals; findViewById&lpar;R&period;id&period;<&sol;span><span class&equals;"s3"><b><i>image<&sol;i><&sol;b><&sol;span><span class&equals;"s2">&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">    <&sol;span>&rcub;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">    <&sol;span><&sol;span><span class&equals;"s1"><b>public void <&sol;b><&sol;span><span class&equals;"s2">downloadImage&lpar;View view&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>Log&period;<i>i<&sol;i>&lpar;<&sol;span><span class&equals;"s6"><b>"Button"<&sol;b><&sol;span><span class&equals;"s2">&comma; <&sol;span><span class&equals;"s6"><b>"Tapped"<&sol;b><&sol;span><span class&equals;"s2">&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>DownloadImage downloadImage &equals; <&sol;span><span class&equals;"s1"><b>new <&sol;b><&sol;span><span class&equals;"s2">DownloadImage&lpar;<&sol;span><span class&equals;"s1"><b>this<&sol;b><&sol;span><span class&equals;"s2">&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span><&sol;span><span class&equals;"s1"><b>new <&sol;b><&sol;span><span class&equals;"s2">Thread&lpar;downloadImage&rpar;&period;start&lpar;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">    <&sol;span>&rcub;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">    <&sol;span><&sol;span><span class&equals;"s1"><b>class <&sol;b><&sol;span><span class&equals;"s2">DownloadImage <&sol;span><span class&equals;"s1"><b>implements <&sol;b><&sol;span><span class&equals;"s2">Runnable &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>Activity <&sol;span><span class&equals;"s3"><b>activity<&sol;b><&sol;span><span class&equals;"s2">&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>Bitmap <&sol;span><span class&equals;"s3"><b>result <&sol;b><&sol;span><span class&equals;"s2">&equals; <&sol;span><span class&equals;"s1"><b>null<&sol;b><&sol;span><span class&equals;"s2">&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>DownloadImage&lpar;Activity activity&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span><&sol;span><span class&equals;"s1"><b>this<&sol;b><&sol;span><span class&equals;"s2">&period;<&sol;span><span class&equals;"s3"><b>activity <&sol;b><&sol;span><span class&equals;"s2">&equals; activity&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>&rcub;<&sol;span>&NewLine;&NewLine;<span class&equals;"s4"><span class&equals;"Apple-converted-space">        <&sol;span><&sol;span><span class&equals;"s2">&commat;Override<&sol;span>&NewLine;&NewLine;<span class&equals;"s5"><span class&equals;"Apple-converted-space">        <&sol;span><&sol;span><span class&equals;"s2"><b>public void <&sol;b><&sol;span><span class&equals;"s4">run&lpar;&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span>URL url&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span>HttpURLConnection httpURLConnection&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span><&sol;span><span class&equals;"s1"><b>try <&sol;b><&sol;span><span class&equals;"s2">&lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s4"><span class&equals;"Apple-converted-space">                <&sol;span>url &equals; <&sol;span><span class&equals;"s1"><b>new <&sol;b><&sol;span><span class&equals;"s4">URL&lpar;<&sol;span><span class&equals;"s2"><b>"https&colon;&sol;&sol;vignette&period;wikia&period;nocookie&period;net&sol;disney&sol;images&sol;0&sol;0a&sol;ElsaPose&period;png&sol;revision&sol;latest&quest;cb&equals;20170221004839"<&sol;b><&sol;span><span class&equals;"s4">&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span>httpURLConnection &equals; &lpar;HttpURLConnection&rpar; url&period;openConnection&lpar;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span>httpURLConnection&period;connect&lpar;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span>InputStream in &equals; httpURLConnection&period;getInputStream&lpar;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span><&sol;span><span class&equals;"s3"><b>result <&sol;b><&sol;span><span class&equals;"s2">&equals; BitmapFactory&period;<i>decodeStream<&sol;i>&lpar;in&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span>&rcub; <&sol;span><span class&equals;"s1"><b>catch <&sol;b><&sol;span><span class&equals;"s2">&lpar;MalformedURLException e&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span>e&period;printStackTrace&lpar;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span>&rcub; <&sol;span><span class&equals;"s1"><b>catch <&sol;b><&sol;span><span class&equals;"s2">&lpar;IOException e&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span>e&period;printStackTrace&lpar;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span>&rcub;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span><&sol;span><span class&equals;"s3"><b>activity<&sol;b><&sol;span><span class&equals;"s2">&period;runOnUiThread&lpar;<&sol;span><span class&equals;"s1"><b>new <&sol;b><&sol;span><span class&equals;"s2">Runnable&lpar;&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span><&sol;span><span class&equals;"s5">&commat;Override<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span><&sol;span><span class&equals;"s1"><b>public void <&sol;b><&sol;span><span class&equals;"s4">run&lpar;&rpar; &lbrace;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                    <&sol;span><&sol;span><span class&equals;"s3"><b>imageView<&sol;b><&sol;span><span class&equals;"s2">&period;setImageBitmap&lpar;<&sol;span><span class&equals;"s3"><b>result<&sol;b><&sol;span><span class&equals;"s2">&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                    <&sol;span>Toast&period;<i>makeText<&sol;i>&lpar;<&sol;span><span class&equals;"s3"><b>activity<&sol;b><&sol;span><span class&equals;"s2">&comma; <&sol;span><span class&equals;"s6"><b>"Image downloaded"<&sol;b><&sol;span><span class&equals;"s2">&comma; Toast&period;<&sol;span><span class&equals;"s3"><b><i>LENGTH&lowbar;SHORT<&sol;i><&sol;b><&sol;span><span class&equals;"s2">&rpar;&period;show&lpar;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">                <&sol;span>&rcub;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">            <&sol;span>&rcub;&rpar;&semi;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">        <&sol;span>&rcub;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2"><span class&equals;"Apple-converted-space">    <&sol;span>&rcub;<&sol;span>&NewLine;&NewLine;<span class&equals;"s2">&rcub;<&sol;span><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>4 &period; Inside MainActivity&comma; on <strong><span style&equals;"color&colon; &num;008000&semi;">DOWNLOAD IMAGE<&sol;span><&sol;strong> button click <strong><span style&equals;"color&colon; &num;008000&semi;">downloadImage<&sol;span><&sol;strong>&lpar;&rpar; method will call&period; This method starts a new thread <span style&equals;"color&colon; &num;008000&semi;"><strong>DownloadImage<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>5 &period; <strong><span style&equals;"color&colon; &num;008000&semi;">DownloadImage<&sol;span><&sol;strong> Thread downloading an image over the network and displaying it in an ImageView and then display a toast message <span style&equals;"color&colon; &num;008000&semi;"><strong>Image Downloaded<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>6 &period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">AndroidManifest<&sol;span><&sol;strong> file and add <strong><span style&equals;"color&colon; &num;008000&semi;">INTERNET<&sol;span><&sol;strong> permission to download an image over the network&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;0000ff&semi;">AndroidManifest&period;xml<&sol;span><&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET"&sol;&gt&semi;<&sol;pre>&NewLine;<p>7 &period; On running the android application <span style&equals;"color&colon; &num;008000&semi;"><strong>Threadexample<&sol;strong><&sol;span> it will look like this as shown below&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1205" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Screenshot&lowbar;1566898859&period;png" alt&equals;"Screenshot&lowbar;1566898859" width&equals;"360" height&equals;"640" &sol;><&sol;p>&NewLine;<p>I hope this article will help you in understanding what is thread&comma; why we use thread and how to use thread in our android applications&period; As we know it is possible to access UI components inside the worker thread using some of the methods which we have discussed above but there is one more alternative in android i&period;e <strong class&equals;"kp lf"><span style&equals;"color&colon; &num;008000&semi;">Handler<&sol;span> <&sol;strong>which also allows you communicate back with the UI thread from other background thread&period; In the next article&comma; we will talk about <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Handler<&sol;strong><&sol;span> in brief&period;&NewLine;

Exit mobile version