design for responsiveness : multi-threading

12

Click here to load reader

Upload: codeandroid

Post on 07-Jul-2015

5.986 views

Category:

Education


1 download

DESCRIPTION

by Daniel, Dao Quang Minh

TRANSCRIPT

Page 1: Design for Responsiveness : Multi-Threading

  

Design for Responsiveness : Multi-Threading

Daniel, Dao Quang Minh

Page 2: Design for Responsiveness : Multi-Threading

  

Design for Responsiveness

* Evil * Application not 

Responding (ANR) Dialog

Page 3: Design for Responsiveness : Multi-Threading

  

What triggers ANR ?

No response to an input event (e.g. Key press, screen touch within 5 seconds)

A BroadcastReceiver hasn't finished executing within 10 seconds

Page 4: Design for Responsiveness : Multi-Threading

  

How to avoid it ?

Page 5: Design for Responsiveness : Multi-Threading

  

How to avoid it ?

Android applications normally run entirely on a single main thread.

Hence anything that takes a long time to complete might trigger the ANR dialog.  

Page 6: Design for Responsiveness : Multi-Threading

  

How to avoid it

Long running operations and computationally expensive calculations should be done in child threads.

Page 7: Design for Responsiveness : Multi-Threading

  

Create a child thread

Handler class allows you to queue tasks to be run on different threads and allows you schedule tasks using Message and Runnable objects

Page 8: Design for Responsiveness : Multi-Threading

  

Handler mHandler = new Handler(){

public void handleMessage(Message m){ //a hook that let you pass messages

//do something here when receive messages

}

}

new Thread(){

public void run(){

doStuff();

Message m = mHandler.obtainMessage();

Bundle b = new Bundle();

b.putString("key","value");

m.setData(b);

mHandler.sendMessage(m); // add Message to the end of the MessageQueue

}

}.start();

Page 9: Design for Responsiveness : Multi-Threading

  

Create a child thread 

Besides sending messages, we can also send Runnable objects directly and schedule things to be run at different times in the future.

post(Runnable r)

postAtFrontOfQueue(Runnable r)

postAtTime(Runnable r, long uptimeMillis)

Etc.

docs/reference/android/os/Handler.html

Page 10: Design for Responsiveness : Multi-Threading

  

Manage > 1 Threads

Executor: an object that executes submitted Runnable tasks. This interface provides a way to decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.

An Executor is normally used instead of explicitly creating threads

docs/reference/java/util/concurrent/Executor.html

Page 11: Design for Responsiveness : Multi-Threading

  

Manage > 1 Threads

ExecutorService : a more extensive interface for Executor.

Allows you to manage termination and tracking progress of one or more async tasks.

Page 12: Design for Responsiveness : Multi-Threading

  

Demo program

http://www.engineyard.com/blog/2009/programming­contest­win­iphone­3gs­2k­cloud­credit/ sequence of twelve words that when hashed is bit­

wise closest to a hash of a challenge phrase All words must be from a 1,000 word dictionary Basically a Hamming distance problem. Let's dive into the code