Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrent Programming in Android OS

Similar presentations


Presentation on theme: "Concurrent Programming in Android OS"— Presentation transcript:

1 Concurrent Programming in Android OS
Mobile Programming Concurrent Programming in Android OS

2 Threads Threads are the cornerstone of any multitasking operating system and can be thought of as mini-processes running within a main process, the purpose of which is to enable at least the appearance of parallel execution paths within applications. The speed and efficiency of a long-running, data-intensive operation often improves when you split it into smaller operations running on multiple threads. On a device that has a CPU with multiple processors (cores), the system can run the threads in parallel, rather than making each sub-operation wait for a chance to run. For example, decoding multiple image files in order to display them on a thumbnail screen runs substantially faster when you do each decode on a separate thread.

3 Main Thread in Android Every Android app has a main thread which is in charge of handling UI (including measuring and drawing views), coordinating user interactions, and receiving lifecycle events. All of your UI tasks happen in this thread like rendering a button, click of a button, etc. By default, application code runs in the main thread. Every statement is therefore executed in sequence. Android collects all events in this thread in a queue and processes this queue with an instance of the Looper class.

4 Contd. If you perform a long lasting operation, the application blocks until the corresponding operation has finished. Android enforces a worst case reaction time of applications. If an activity does not react within 5 seconds to user input, the Android system displays an Application not responding (ANR) dialog. From this dialog the user can choose to stop the application.

5 Through Multi-threading
How to prevent ANR? Through Multi-threading Your application must create other threads and put long running work on non- UI threads.  There are options on how to accomplish the creation of alternate threads. You can create and start your own java.lang.Thread.  You can create and start an AsyncTask – Android’s own thread simplification mechanism.  The non-UI thread then handles long running processing – like downloading a file – while the UI thread sticks to displaying the UI and reacting to user events.  Potentially slow operations are for example network, file and database access and complex calculations. These should be done in separate threads.

6 Another Problem Processing Threads Unable To Update the UI
Unfortunately, the user interface (UI) cannot be updated by non-UI threads.  For example, after successfully downloading a file, a separate (non-UI) thread can’t show an AlertDialog, update a TextView widget, otherwise make a UI change to indicate the file has been successfully downloaded.  If you attempt to update the UI from a non-UI thread, the application will compile, but you get a CalledFromWrongThreadException thrown from the point your non-UI thread attempts to make the UI change.  As the exception message will inform you, “Only the original thread that created a view hierarchy can touch its views.”

7 Solution If you need to update the user interface from a new Thread, you need to synchronize with the main thread. Because of this restrictions, Android developer typically use Android specific code constructs. Standard Android provides the android.os.Handler class or the AsyncTasks classes among some other ways.

8 This does not update the UI
public class MainActivity extends AppCompatActivity { private ProgressBar progress; private TextView text; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void startProgress(View view) { // do something long Runnable runnable = new Runnable() { @Override public void run() { for (int i = 0; i <= 10; i++) { final int value = i; doFakeWork(); } } }; new Thread(runnable).start(); } // Simulating something timeconsuming private void doFakeWork() { try { SystemClock.sleep(5000); } catch (Exception e) { e.printStackTrace(); } } } This does not update the UI

9 Handler Framework Android threads, in particular the UI thread, have a message queue.  Messages in the queue are processed by the thread in order of arrival.  In the case of the UI thread, user events (like a button push) cause event messages to be placed in the queue. Using the Handler Framework, you can create a message directly and put the message on the UI thread’s queue from the non-UI thread.  The framework also lets you build a message handler to listen for the message on the UI thread.  Thus, providing a means for the non-UI thread to communicate with the UI via the framework pieces.

10

11 Implementing a thread Handler
Thread Handlers are implemented in the main thread of an application and are primarily used for updating UI in response to messages sent by another thread running within the application’s process. Handlers are sub-classed from Android Handler class and the steps to implement it are as follows: Create a subclass of Handler Class sendMessage() or post(runnable) from the worker thread If sendMessage() is used then override handleMessage() callback method

12 <. xml version="1. 0" encoding="utf-8"
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:indeterminate="false" android:max="10" android:padding="4dip" > </ProgressBar> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" > </TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startProgress" android:text="Start Progress" > </Button> </LinearLayout>

13 public class MainActivity extends AppCompatActivity { private ProgressBar progress; private TextView text; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = (ProgressBar) findViewById(R.id.progressBar1); text = (TextView) findViewById(R.id.textView1); handler = new Handler(){ @Override public void handleMessage(Message msg) { progress.setProgress(msg.getData().getInt("value")); text.setText(msg.getData().getString("info")); } };} public void startProgress(View view) { // do something long Runnable runnable = new Runnable() { @Override public void run() { for (int i = 0; i <= 10; i++) { doFakeWork(); Message msg = new Message(); Bundle b = new Bundle(); b.putInt("value",i); if(i!=10) b.putString("info","updating"); else b.putString("info","updated"); msg.setData(b); handler.sendMessage(msg);}}}; new Thread(runnable).start();} private void doFakeWork() { try { SystemClock.sleep(5000);} catch (Exception e) { e.printStackTrace();}}} Using sendMessage()

14 public class MainActivity extends AppCompatActivity { private ProgressBar progress; private TextView text; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = findViewById(R.id.progressBar1); text = findViewById(R.id.textView1); handler = new Handler(); } public void startProgress(View view) { // do something long Runnable runnable = new Runnable() { @Override public void run() { for (int i = 0; i <= 10; i++) { final int value = i; doFakeWork(); handler.post(new Runnable(){ @Override public void run() { progress.setProgress(value); if(value!=10) text.setText("updated"); else text.setText("updating"); } }); } } }; new Thread(runnable).start(); } // Simulating something timeconsuming private void doFakeWork() { try { SystemClock.sleep(5000);} catch (Exception e) {e.printStackTrace();}}} Using post(Runnable)

15 Output – GIF IMAGE (View in Slideshow)

16 AsyncTask The AsyncTask is a special Android convenience class for creating a new Thread that can “publish results on the UI thread without having to manipulate threads and/or handlers.”   Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched. Due to this single thread model tasks that take longer time to fetch the response can make the application non-responsive. To avoid this we use android AsyncTask to perform the heavy tasks in background on a dedicated thread and passing the results back to the UI thread. Hence use of AsyncTask in android application keeps the UI thread responsive at all times.

17 AsyncTask Methods The basic methods used in an android AsyncTask class are defined below : doInBackground() – code here is executed on a new, non-UI thread that Android creates when the AsynTask is executed.  This is the only required method of an AsyncTask subclass. onPreExecute() – code executed on the UI thread by Android before non-UI thread work is executed in doInBackground() code. onPostExecute() – code executed on the UI thread by Android after non-UI thread work is executed in doInBackground. onProgressUpdate() – called on the UI thread by Android whenever publishProgress(Progress…) is called (typically in the doInBackground method) to provide the user interface (and user) with updates while the separate thread is still running.

18 Generic Types in AsyncTask Class
The three generic types used in an android AsyncTask class are given below : Params : The type of the parameters sent to the task upon execution Progress : The type of the progress units published during the background computation Result : The type of the result of the background computation

19 <RelativeLayout xmlns:android="http://schemas. android
<RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt" android:textColor="#444444" android:layout_alignParentLeft="true" android:layout_marginRight="9dip" android:layout_marginTop="20dip" android:layout_marginLeft="10dip" android:text="Sleep time in Seconds:"/> <EditText android:layout_width="150dip" android:layout_height="wrap_content" android:inputType="number" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Run Async task" android:layout_centerHorizontal="true" android:layout_marginTop="64dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="7pt" android:layout_centerHorizontal="true" /> </RelativeLayout> Example

20 public class MainActivity extends AppCompatActivity { private Button button; private EditText time; private TextView finalResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); time = findViewById(R.id.in_time); button = findViewById(R.id.btn_run); finalResult = findViewById(R.id.tv_result); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AsyncTaskRunner runner = new AsyncTaskRunner(); String sleepTime = time.getText().toString(); runner.execute(sleepTime); } }); }

21 private class AsyncTaskRunner extends AsyncTask<String, String, String> { private String resp; ProgressDialog progressDialog; @Override protected String doInBackground(String... params) { publishProgress("Sleeping..."); // Calls onProgressUpdate() try { int time = Integer.parseInt(params[0])*1000; Thread.sleep(time); resp = "Slept for " + params[0] + " seconds"; } catch (InterruptedException e) { e.printStackTrace(); resp = e.getMessage(); } catch (Exception e) { e.printStackTrace(); resp = e.getMessage(); } return resp; } @Override protected void onPostExecute(String result) { // execution of result of Long time consuming operation progressDialog.dismiss(); finalResult.setText(result);} @Override protected void onPreExecute() { progressDialog = ProgressDialog.show(MainActivity.this, "ProgressDialog", "Wait for "+time.getText().toString()+ " seconds");} @Override protected void onProgressUpdate(String... text) { finalResult.setText(text[0]);} }}

22 Output – GIF IMAGE (View in Slideshow)

23 Thank You!


Download ppt "Concurrent Programming in Android OS"

Similar presentations


Ads by Google