SlideShare a Scribd company logo
1 of 12
Download to read offline
Design for Responsiveness : Multi-Threading
                         Daniel, Dao Quang Minh




                
Design for Responsiveness
   * Evil * Application not 
    Responding (ANR) Dialog




                      
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




                       
How to avoid it ?




         
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.  




                       
How to avoid it
   Long running operations and computationally 
     expensive calculations should be done in child 
     threads.




                       
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




                       
   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();




                                     
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




                          
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




                       
Manage > 1 Threads
   ExecutorService : a more extensive interface for 
     Executor.
   Allows you to manage termination and tracking 
     progress of one or more async tasks.




                       
Demo program
   http://www.engineyard.com/blog/2009/programming­co
   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




                        

More Related Content

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Design for Responsiveness : Multi-Threading

  • 1. Design for Responsiveness : Multi-Threading Daniel, Dao Quang Minh    
  • 2. Design for Responsiveness  * Evil * Application not  Responding (ANR) Dialog    
  • 3. 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    
  • 5. 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.      
  • 6. How to avoid it  Long running operations and computationally  expensive calculations should be done in child  threads.    
  • 7. 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    
  • 8. 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();    
  • 9. 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    
  • 10. 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    
  • 11. Manage > 1 Threads  ExecutorService : a more extensive interface for  Executor.  Allows you to manage termination and tracking  progress of one or more async tasks.    
  • 12. Demo program  http://www.engineyard.com/blog/2009/programming­co  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