CS499 – Mobile Application Development

Slides:



Advertisements
Similar presentations
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Advertisements

Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
1 Mobile Software Development Framework: Android Activity, View/ViewGroup, External Resources, Listener 10/9/2012 Y. Richard Yang.
Cosc 5/4730 A little on threads and Messages: Handler class.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
UI Design Patterns & Best Practices Mike Wolfson July 22, 2010.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Networking Android Club Networking AsyncTask HttpUrlConnection JSON Parser.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
Working in the Background Radan Ganchev Astea Solutions.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
로봇 모니터링 1/2 UNIT 20 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Message Queue Handler 2.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Multithreading in JAVA
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Android Threads. Threads Android will show an “ANR” error if a View does not return from handling an event within 5 seconds Or, if some code running in.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
CHAP 14. 프로세스와 스레드. © 2012 생능출판사 All rights reserved 다중 스레딩 하나의 애플리케이션이 동시에 여러 가지 작업을 하 는 것 이들 작업은 스레드 (thread) 라고 불린다.
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
Concurrency in Android
Small talk with the UI thread
UI Design Patterns & Best Practices
Android Multi-Threading
CS499 – Mobile Application Development
Instructor: Mazhar Hussain
Android – Event Handling
Concurrency in Android
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Java Concurrency.
Lecture 6: Process, Thread, Task
Mobile Computing With Android ACST 4550 Android Animation
Threads II IS
Developing Android Services
Android Programming Lecture 8
CS371m - Mobile Computing Responsiveness.
Multithreading 2 Lec 24.
Multithreaded Programming
Android Topics UI Thread and Limited processing resources
Mobile Computing With Android ACST 4550 Android Database Storage
Android Topics Asynchronous Callsbacks
Android Topics Threads and the MessageQueue
Android Developer Fundamentals V2
Threads, Handlers, and AsyncTasks
Threads in Java James Brucker.
Multithreading in java.
Using threads for long running tasks.
Threads and Multithreading
Lecture 6: Process, Thread, Task
Lecture 19 Threads CSE /6/2019.
Android Threads Dimitar Ivanov Mobile Applications with Android
CIS 470 Mobile App Development
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

CS499 – Mobile Application Development Fall 2013 Threads & Android – Part 2

Concurrent Programming Carnegie Mellon Concurrent Programming Multiple processes – each logical control flow is a process. Some form of IPC (InterProcess Communication) needed Multiple threads – multiple logical control flows within a single process. Shared address space I/O multiplexing – event driven concurrency. Shared address space

Android Threads Activities have a main thread – the UI thread App components in the same process use the same main thread User interaction, system callback & lifecycle methods are handled in the UI thread It is essential that the UI remains responsive UI toolkit not thread-safe. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time.

Implications Two rules: Do not block the UI thread Blocking the UI thread hurts responsiveness Long-running operations should be done in background thread Don’t access/manipulate the UI toolkit from a non UI thread. Results are unpredictable Q: How can the UI & background threads communicate in a thread-safe manner?

Consider the following Game App: public class Demo extends Activity { int mInFLight; @Override public void onCreate(Bundle state) { super.onCreate(state); setContentView(R.layout.demo); final View root – findViewById(R.id.root); final Drawable bg = root.getBackground(); final TextView msg = (TextView) findViewById(R.id.msg); final Game game = Game.newGame(); ((Button)findViewById(R.id.start)).setOnClickListener( new View.onClickListener() { @Override public void onClick(View v) { // initialize the game! } } });

Want to display animated background while waiting to initialize… // Broken – do not use!!! void initGame(View root, Drawable bg, Game game, TextView resp, String level) { // if animation hasn’t started, make it start if (0 >= mInFlight++) { root.setBackgroundResource(R.anim.dots); ((AnimationDrawable)root.getBackground()).start(); } // initialize the game String msg = game.initialize(level); // if last initialization remove & clean up animation if (0 >= --mInFlight) { ((AnimationDrawable)root.getBackground()).stop(); root.setBackgroundDrawable(bg); resp.setText(msg);

Problem: UI thread is executing game Problem: UI thread is executing game.initialize and hence can’t show the animation background animation won’t work if the initialize takes too long, the android framework will suspend it

Might be tempted to do the following: public void onClick(View v) { new Thread(new Runnable() { public void run() { Bitmap b = loadImageFromNetwork("http://example.com/image.png"); mImageView.setImageBitmap(b); } }).start(); } Seems to meet Rule 1 but violates Rule 2

Solution: AsyncTask Structured, safe, powerful, and easy-to-use way to manage UI and background threads correctly. Generic class class AsyncTask<Params,Progress,Result> { … } Generic type parameters Params – Types used in background work Progress – Types used when indicating progress Result – Types of result

AsyncTask methods Runs before doInBackground() Performs work void onPreExecute() Runs before doInBackground() Result doInBackground(Params… params) Performs work can call void publishProgress(Progress…values) void onProgressUpdate(Progress…values) Invoked in response to publishProgress() void onPostExecute(Result result) Runs after doInBackground()

public void onClick(View v) { new DownloadImageTask() public void onClick(View v) {     new DownloadImageTask().execute("http://example.com/image.png"); } private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {     /** The system calls this to perform work in a worker thread and       * delivers it the parameters given to AsyncTask.execute() */     protected Bitmap doInBackground(String... urls) {         return loadImageFromNetwork(urls[0]);     }         /** The system calls this to perform work in the UI thread and delivers       * the result from doInBackground() */     protected void onPostExecute(Bitmap result) {         mImageView.setImageBitmap(result);     } }

AsyncTask Created on UI thread. When ‘execute’d UI thread invokes onPreExecute() to do any setup UI thread creates new background thread that runs a doInBackground() method concurrently Once background thread terminates, onPostExecute() is run in the UI thread.

public void onCreate(Bundle state) { super public void onCreate(Bundle state) { super.onCreate(state); setContentView(R.layout.main); final View dots = findViewById(R.id.dots); final Drawable bg = dots.getBackground(); final TextView msg = ((TextView) findViewById(R.id.msg)); final Game game = Game.newGame(); ((Button) findViewById(R.id.start)).setOnClickListener( new View.OnClickListener() { public void onClick(View v) { new AsyncInitGame(dots, bg, game,msg).execute("basic"); } }); }

private final class AsyncInitGame extends AsyncTask<String, Void, String> { private final View root; private final Game game; private final TextView message; private final Drawable bg; public AsyncInitGame(View root, Drawable bg, Game game, TextView msg) { this.root = root; this.bg = bg; this.game = game; this. message = msg; } // runs on the UI thread @Override protected void onPreExecute() { if (0 >= mInFlight++) { root.setBackgroundResource(R.anim.dots); ((AnimationDrawable)root.getBackground()).start();

// runs on the UI thread @Override protected void onPostExecute(String msg) { if (0 >= --mInFlight) { ((AnimationDrawable)root.getBackground()).stop(); root.setBackgroundDrawable(bg); } message.setText(msg); // runs on a background thread @Override protected String doInBackground(String … args) { return ((1 != args.length) || (null == args[0])) ? null : game.initialize(args[0]);

AsyncTask dataflow Creation Thread (UI) New Thread execute(args…) doInBackground(args…) : result doPostExecute(result)

private final class LoadIconTask extends Integer,Integer,Bitmap> { protected Bitmap doInBackground(Integer…resid) { Bitmap tmp = BitmapFactory.decodeResource( getResources(),resid[0]); for (int I = 1; i< 11;i++) { // some long running op… publishProgress(i*10); } return tmp; protected void onProgressUpdate(Integer... values) { progress.setProgress(values[0]); protected void onPostExecute(Bitmap result) { iview.setImageBitmap(result); }

Be careful! UI thread should not modify data sent as parameters to AsyncTasks doInBackground() must only make thread-safe references to variables inherited into its scope. int mCount; public void initButton(Button button) { mCount = 0; new View.OnClickListener() { @Override public void onClick(View v) { new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void…args) { mCount++; // NOT THREAD SAFE return null; } }.execute(); } });

However… Beginning with Honeycomb (3.0), Android changed how it's running the AsyncTasks: AsyncTask task = new AsyncTask1(); if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { task.execute(); } http://www.techrepublic.com/blog/android-app-builder/android-asynctask-behavior-changes-you-should-know/

Handler Threads can also communicate by posting Messages & Runnables to a Handler Message Can contain a code, data object & args Recipient (Handler) implements response Runnable Contains an instance of the Runnable interface Sender implements response

Handler sendMessage() post() Looper Puts Message on MessageQueue Puts runnable on Message Queue Looper One per Thread Dispatches MessageQueue entries Calls handleMessage() for Messages Calls run() for Runnables

Simple use of UI handler public void onClick(View v) { new Thread(new Runnable() { public void run() { final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png"); mImageView.post(new Runnable() { public void run() { mImageView.setImageBitmap(bitmap); } }); } }).start(); }

Handler Two main uses for a Handler Schedule Message/Runnable for future execution Enqueue action to be performed on a different thread

Messages & Handlers Create Message and set Message content Handler.obtainMessage() Message.obtain() Many variants. See documentation Message parameters include int arg1, arg2 int what Object obj Bundle data

Messages & Handlers sendMessage() sendMessageAtFrontOfQueue() Queue message immediately sendMessageAtFrontOfQueue() Insert message immediately at front ofqueue SendMessageAtTime() Queue message at the stated time sendMessageDelayed() Queue message after the stated delay

public class SimpleThreadingExample extends Activity { private final static int SET_PROGRESS_BAR_VISIBILITY = 0; private final static int PROGRESS_UPDATE = 1; private final static int SET_BITMAP = 2; private ImageView iview; private ProgressBar progress; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SET_PROGRESS_BAR_VISIBILITY: { progress.setVisibility((Integer) msg.obj); break; } case PROGRESS_UPDATE: { progress.setProgress((Integer) msg.obj); case SET_BITMAP: { iview.setImageBitmap((Bitmap) msg.obj); }; …

@Override public void onCreate(Bundle savedInstanceState) { super @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iview = (ImageView) findViewById(R.id.imageView1); progress = (ProgressBar) findViewById(R.id.progressBar1); final Button button = (Button) findViewById(R.id.loadButton); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Thread(new LoadIconTask(R.drawable.icon, handler)).start(); } });

private class LoadIconTask implements Runnable { private final int resId; private final Handler handler; LoadIconTask(int resId, Handler handler) { this.resId = resId; this.handler = handler; } public void run() { final Bitmap tmp = BitmapFactory.decodeResource(getResources(), resId); Message msg = null; msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY,ProgressBar.VISIBLE); handler.sendMessage(msg); for (int i = 1; i < 11; i++) { sleep(); msg = handler.obtainMessage(PROGRESS_UPDATE, i * 10); } msg = handler.obtainMessage(SET_BITMAP, tmp); msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY, ProgressBar.INVISIBLE); private void sleep() { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace();}

Runnables & Handlers boolean post(Runnable r) Add Runnable to the MessageQueue boolean postAtTime(Runnable r, long uptimeMillis) Add Runnable to the MessageQueue. Run at a specific time (based on SystemClock.upTimeMillis()) boolean postDelayed(Runnable, long delayMillis) Add Runnable to the message queue. Run after the specified amount of time elapses.

public class SimpleThreadingExample extends Activity { private ImageView iview; private final Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iview = (ImageView) findViewById(R.id.imageView1); final Button button = (Button) findViewById(R.id.loadButton); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Thread(new LoadIconTask(R.drawable.icon)).start();} }); } private class LoadIconTask implements Runnable { int resId; LoadIconTask(int resId) { this.resId = resId; } public void run() { final Bitmap tmp = BitmapFactory.decodeResource(getResources(),resId); handler.post(new Runnable() { public void run() { iview.setImageBitmap(tmp);} }}

public class SimpleThreadingExample extends Activity { private Bitmap bitmap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView iview = (ImageView) findViewById(R.id.imageView1); final Button button = (Button) findViewById(R.id.loadButton); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Thread(new Runnable() { public void run() { bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon); iview.post(new Runnable() { public void run() { iview.setImageBitmap(bitmap); } });} }).start(); });

public class SimpleThreadingExample extends Activity { private Bitmap bitmap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView iview = (ImageView) findViewById(R.id.imageView1); final Button button = (Button) findViewById(R.id.loadButton); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Thread(new Runnable() { public void run() { bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); SimpleThreadingExample.this.runOnUiThread(new Runnable() { iview.setImageBitmap(bitmap); } }); }).start();