Thread Example with its description
While launching an application firstly main thread is executed. This Main thread handles all the UI concept of application. If we want to run long the task in which we don't need the UI then we use thread for running that task in background.
Here is the example of Thread which describes blow:
We can create thread by creating the object of Thread which have
Thread.run() method for running the thread.Here,
run() method is called by the
We can also run the the multiple threads independently, which is known as MultiThreading. This thread also have the functionality of sleep by which the currently executing thread to sleep (temporarily cease execution) for the specified number of time. But sleep throws the InterruptedException So, we have to handle it by using try/catch like this.
Updating the UI from a Background Thread
It is common to use a background Thread for doing network operations or long running tasks, and then update the UI with the results when needed.
This poses a problem, as only the main thread can update the UI.
The solution is to use the
runOnUiThread() method, as it allows you to initiate code execution on the UI thread from a background Thread.
In this simple example, a Thread is started when the Activity is created, runs until the magic number of
42 is randomly generated, and then uses the
runOnUiThread() method to update the UI once this condition is met.