Hi .... Dear All .... Today I am uploading a superb example of Multi-threading.
In this example i have one Main Thread / UI Thread and worker thread.
Worker thread updates the status on UI thread.
• Threads are lightweight processes, which allow us to perform
jobs in parallel and concurrent manner.
• Android starts a process with a single thread for execution
called the “Main thread” or “UI Thread”.
• Capable of handling multiple threads
• Different threads need different
priorities
Thread Life Cycle
In Android we can classify threads
based on their Access to UI toolkit. Thus we have two types of thread
1. UI thread
2. Worker thread
UI thread
•We have already seen that the
application starts its execution with a single thread called the UI thread.
•Since the UI thread is responsible
for the user’s interaction with the UI components.
•Android UI toolkit is not thread
safe. This means that other thread cannot access the UI toolkit.
•In order to manipulate the widgets,
we must follow these two golden rules.
• Always access the
UI toolkit from the UI thread only.
• Never block the UI
thread
Worker Thread
•Worker threads are created to remove long time-consuming
jobs off from the UI thread.
•These threads are vital for the responsiveness of the
application, given that UI thread can’t be blocked.
•Basically, all network related operations like fetching data
from web services, downloading files, etc., database queries, IO operations
like reading or writing to a file, time consuming calculations etc. are
performed on these threads.
•To overcome this problem, Android has a work around solution
allows us to post task to the UI thread from any thread. Following are the
method which helps us achieve this.
• View.post(Runnable)
• Activity.runOnUiThread(Runnable)
•
View.postDelayed(Runnable,timeInMillis)
See an Example
A. Output will be like
a.
b.
B. XML files are
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ThreadActivity"> <TextView android:id="@+id/counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:textAlignment="center"/>
</RelativeLayout>
C. Java code is as
package com.exam.ravi.threadex1; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class ThreadActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_thread); textView = (TextView) findViewById(R.id.counter); new Thread(new Runnable() { @Override public void run() { int i=0; while(i<100) { SystemClock.sleep(200); i++; final int curCount =i; if(curCount%10==0) { textView.post(new Runnable() { @Override public void run() { textView.setText(curCount+"% Complete"); } }); } } textView.post(new Runnable() { @Override public void run() { textView.setText("Count Completed"); } }); } }).start(); } }
No comments:
Post a Comment