SlideShare a Scribd company logo
Boutique product development company
It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.
Threading
Muhammad Mateen| Android Mentor
Threading
Topics covered in the presentation




     •   UI Thread
     •   Loppers & Handlers
     •   Async Tasks
     •   Intent Services
     •   Executor
     •   Executor Service
     •   Callables & Futures
                                     Muhammad Mateen| Android Mentor
Threading


UI Thread

  • When an application is launched, the System creates a Thread
  of execution called the Main Thread or the UI Thread.


  • Called the UI Thread because it is this thread which interacts
  with the UI widgets.


  • Don’t execute tasks that take more then 5 seconds? Why ?




                                                  Muhammad Mateen| Android Mentor
Threading

UI Thread



   •Things to remember
        •   No long operations
        •   Don’t interact other threads with UI thread



   •The UI thread runs as a NORMAL PRIORITY thread.




                                                  Muhammad Mateen| Android Mentor
Threading


UI Thread

  •UI Thread acts as a Pipeline Thread, similar to those from Swing or
  any other UI Framework
  •Each action is feed into a Pipeline, and processed
  •Any long operation can starve other tasks in the queue
  •We can use HANDLERS to interact with this queue




                                                 Muhammad Mateen| Android Mentor
Threading

Loopers and Handlers


   •Ordinary thread to Pipeline thread
   •Loopers implements the loop
   •Handler feeds the task into Queue associated with the Thread
   •Together, multi-threaded processes can be handled
   •Handlers detect the Looper, implicitly
   •Each Handler is always tied to one Looper




                                                Muhammad Mateen| Android Mentor
Threading

Async Tasks


  •Easiest way to offload onto a separate thread.
  • Executes task off the UI thread and post result on UI
  • Not used for long processes (why ? )
  • Works parallel (for less 4.x)
  • Tied with Activity
  • On memory shortage, it likely to be killed




                                                    Muhammad Mateen| Android Mentor
Threading

Async Tasks


 • Why should be take care of point 3 ?

 • Well for one the API documentation says so

 • Secondly the Resource Queue is created by the System

 • Avoid straining resources that you did not allocate




                                                Muhammad Mateen| Android Mentor
Threading

Async Tasks

 Methods
      • onPreExecute()
      • doInBackground()
      • onProgressUpdate()
      • onPostExecute()




                             Muhammad Mateen| Android Mentor
Threading

Intent Service


  • Subclass of Service Class
  • Work Queue Process
  • Runs off the UI Thread
  • Can place long process in onHandleIntent()
  • Manages Worker Thread itself.
  • Can get a out-of-the-box-queue
  • Broadcast intents




                                                 Muhammad Mateen| Android Mentor
Threading

Executor


   • Pool of runnable tasks
   • Decouples the mechanics of a Task Submission from the process
   of how they will be executed.

   •We use this instead of creating Threads separately.




                                                 Muhammad Mateen| Android Mentor
Threading

Executor Service


 • Add lifecycle methods to Executor
 • Create N number of thread pool
 • Or Executors.newSingleThreadExecutor() for a Single runnable
 pool
 • shut down a executor will reject new Tasks




                                                Muhammad Mateen| Android Mentor
Threading

Executor Service

•Methods
     shutDown() – don’t kill the running tasks
     shutDownNow() – kill the running tasks
• Unused ExecutorService can be shutdown, to claim for its
resources
•Upon termination, an executor has no tasks actively executing,
no tasks awaiting execution, and no new tasks can be submitted




                                                 Muhammad Mateen| Android Mentor
Threading

Callables And Futures


•Biggest Issue with Runnable ?
•Callable return values after completion
• Submitting callable to executor returns a Future
• Futures can be used to check Callable’s status
•Use the get method to retrieve the result of a Future




                                                     Muhammad Mateen| Android Mentor

More Related Content

Similar to Ts threading

mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptx
NgLQun
 
Background threads, async communication and vaadin
Background threads, async communication and vaadinBackground threads, async communication and vaadin
Background threads, async communication and vaadin
Petter Holmström
 
Microservices: The Best Practices
Microservices: The Best PracticesMicroservices: The Best Practices
Microservices: The Best Practices
Pavel Mička
 
Internals of AsyncTask
Internals of AsyncTask Internals of AsyncTask
Internals of AsyncTask
BlrDroid
 
Android Service
Android ServiceAndroid Service
Android Service
Charile Tsai
 
MVP Clean Architecture
MVP Clean  Architecture MVP Clean  Architecture
MVP Clean Architecture
Himanshu Dudhat
 
Vagrant
VagrantVagrant
Vagrant
Obed N Muñoz
 
Building an OpenMRS Distribution - Lessons from KenyaEMR
Building an OpenMRS Distribution - Lessons from KenyaEMRBuilding an OpenMRS Distribution - Lessons from KenyaEMR
Building an OpenMRS Distribution - Lessons from KenyaEMR
rowanseymour
 
Lecture #2 threading, networking & permissions final version #2
Lecture #2  threading, networking & permissions final version #2Lecture #2  threading, networking & permissions final version #2
Lecture #2 threading, networking & permissions final version #2
Vitali Pekelis
 
Kku2011
Kku2011Kku2011
Creating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 SeattleCreating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 Seattle
Jim McKeeth
 
Introduction to Python Celery
Introduction to Python CeleryIntroduction to Python Celery
Introduction to Python Celery
Mahendra M
 
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskAndroid - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTask
Hoang Ngo
 
Android Service Patterns
Android Service PatternsAndroid Service Patterns
Android Service Patterns
Shree Kumar
 
Making a Mock by Kelsey Shannahan
Making a Mock by Kelsey ShannahanMaking a Mock by Kelsey Shannahan
Making a Mock by Kelsey Shannahan
QA or the Highway
 
Reactive Micro Services with Java seminar
Reactive Micro Services with Java seminarReactive Micro Services with Java seminar
Reactive Micro Services with Java seminar
Gal Marder
 
Kku2011
Kku2011Kku2011
Test Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh DahalTest Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh Dahal
QA or the Highway
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
MuhammadBilal187526
 
QAorHighway2016
QAorHighway2016QAorHighway2016
QAorHighway2016
Bhupesh Dahal
 

Similar to Ts threading (20)

mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptx
 
Background threads, async communication and vaadin
Background threads, async communication and vaadinBackground threads, async communication and vaadin
Background threads, async communication and vaadin
 
Microservices: The Best Practices
Microservices: The Best PracticesMicroservices: The Best Practices
Microservices: The Best Practices
 
Internals of AsyncTask
Internals of AsyncTask Internals of AsyncTask
Internals of AsyncTask
 
Android Service
Android ServiceAndroid Service
Android Service
 
MVP Clean Architecture
MVP Clean  Architecture MVP Clean  Architecture
MVP Clean Architecture
 
Vagrant
VagrantVagrant
Vagrant
 
Building an OpenMRS Distribution - Lessons from KenyaEMR
Building an OpenMRS Distribution - Lessons from KenyaEMRBuilding an OpenMRS Distribution - Lessons from KenyaEMR
Building an OpenMRS Distribution - Lessons from KenyaEMR
 
Lecture #2 threading, networking & permissions final version #2
Lecture #2  threading, networking & permissions final version #2Lecture #2  threading, networking & permissions final version #2
Lecture #2 threading, networking & permissions final version #2
 
Kku2011
Kku2011Kku2011
Kku2011
 
Creating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 SeattleCreating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 Seattle
 
Introduction to Python Celery
Introduction to Python CeleryIntroduction to Python Celery
Introduction to Python Celery
 
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskAndroid - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTask
 
Android Service Patterns
Android Service PatternsAndroid Service Patterns
Android Service Patterns
 
Making a Mock by Kelsey Shannahan
Making a Mock by Kelsey ShannahanMaking a Mock by Kelsey Shannahan
Making a Mock by Kelsey Shannahan
 
Reactive Micro Services with Java seminar
Reactive Micro Services with Java seminarReactive Micro Services with Java seminar
Reactive Micro Services with Java seminar
 
Kku2011
Kku2011Kku2011
Kku2011
 
Test Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh DahalTest Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh Dahal
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
QAorHighway2016
QAorHighway2016QAorHighway2016
QAorHighway2016
 

More from Confiz

Agile training workshop
Agile training workshopAgile training workshop
Agile training workshop
Confiz
 
Web services with laravel
Web services with laravelWeb services with laravel
Web services with laravel
Confiz
 
DMAIC-Six sigma process Improvement Approach
DMAIC-Six sigma process Improvement ApproachDMAIC-Six sigma process Improvement Approach
DMAIC-Six sigma process Improvement Approach
Confiz
 
What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.
Confiz
 
Software testing methods, levels and types
Software testing methods, levels and typesSoftware testing methods, levels and types
Software testing methods, levels and types
Confiz
 
Sqa, test scenarios and test cases
Sqa, test scenarios and test casesSqa, test scenarios and test cases
Sqa, test scenarios and test cases
Confiz
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
Confiz
 
Entity framework code first
Entity framework code firstEntity framework code first
Entity framework code first
Confiz
 
Security testing presentation
Security testing presentationSecurity testing presentation
Security testing presentation
Confiz
 
Advance text rendering in i os
Advance text rendering in i osAdvance text rendering in i os
Advance text rendering in i os
Confiz
 
Photoshop manners
Photoshop mannersPhotoshop manners
Photoshop manners
Confiz
 
Monkey talk
Monkey talkMonkey talk
Monkey talk
Confiz
 
An insight to microsoft platform
An insight to microsoft platformAn insight to microsoft platform
An insight to microsoft platform
Confiz
 
Ts branching over the top
Ts   branching over the topTs   branching over the top
Ts branching over the top
Confiz
 

More from Confiz (14)

Agile training workshop
Agile training workshopAgile training workshop
Agile training workshop
 
Web services with laravel
Web services with laravelWeb services with laravel
Web services with laravel
 
DMAIC-Six sigma process Improvement Approach
DMAIC-Six sigma process Improvement ApproachDMAIC-Six sigma process Improvement Approach
DMAIC-Six sigma process Improvement Approach
 
What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.
 
Software testing methods, levels and types
Software testing methods, levels and typesSoftware testing methods, levels and types
Software testing methods, levels and types
 
Sqa, test scenarios and test cases
Sqa, test scenarios and test casesSqa, test scenarios and test cases
Sqa, test scenarios and test cases
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
Entity framework code first
Entity framework code firstEntity framework code first
Entity framework code first
 
Security testing presentation
Security testing presentationSecurity testing presentation
Security testing presentation
 
Advance text rendering in i os
Advance text rendering in i osAdvance text rendering in i os
Advance text rendering in i os
 
Photoshop manners
Photoshop mannersPhotoshop manners
Photoshop manners
 
Monkey talk
Monkey talkMonkey talk
Monkey talk
 
An insight to microsoft platform
An insight to microsoft platformAn insight to microsoft platform
An insight to microsoft platform
 
Ts branching over the top
Ts   branching over the topTs   branching over the top
Ts branching over the top
 

Ts threading

  • 1. Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.
  • 3. Threading Topics covered in the presentation • UI Thread • Loppers & Handlers • Async Tasks • Intent Services • Executor • Executor Service • Callables & Futures Muhammad Mateen| Android Mentor
  • 4. Threading UI Thread • When an application is launched, the System creates a Thread of execution called the Main Thread or the UI Thread. • Called the UI Thread because it is this thread which interacts with the UI widgets. • Don’t execute tasks that take more then 5 seconds? Why ? Muhammad Mateen| Android Mentor
  • 5. Threading UI Thread •Things to remember • No long operations • Don’t interact other threads with UI thread •The UI thread runs as a NORMAL PRIORITY thread. Muhammad Mateen| Android Mentor
  • 6. Threading UI Thread •UI Thread acts as a Pipeline Thread, similar to those from Swing or any other UI Framework •Each action is feed into a Pipeline, and processed •Any long operation can starve other tasks in the queue •We can use HANDLERS to interact with this queue Muhammad Mateen| Android Mentor
  • 7. Threading Loopers and Handlers •Ordinary thread to Pipeline thread •Loopers implements the loop •Handler feeds the task into Queue associated with the Thread •Together, multi-threaded processes can be handled •Handlers detect the Looper, implicitly •Each Handler is always tied to one Looper Muhammad Mateen| Android Mentor
  • 8. Threading Async Tasks •Easiest way to offload onto a separate thread. • Executes task off the UI thread and post result on UI • Not used for long processes (why ? ) • Works parallel (for less 4.x) • Tied with Activity • On memory shortage, it likely to be killed Muhammad Mateen| Android Mentor
  • 9. Threading Async Tasks • Why should be take care of point 3 ? • Well for one the API documentation says so • Secondly the Resource Queue is created by the System • Avoid straining resources that you did not allocate Muhammad Mateen| Android Mentor
  • 10. Threading Async Tasks Methods • onPreExecute() • doInBackground() • onProgressUpdate() • onPostExecute() Muhammad Mateen| Android Mentor
  • 11. Threading Intent Service • Subclass of Service Class • Work Queue Process • Runs off the UI Thread • Can place long process in onHandleIntent() • Manages Worker Thread itself. • Can get a out-of-the-box-queue • Broadcast intents Muhammad Mateen| Android Mentor
  • 12. Threading Executor • Pool of runnable tasks • Decouples the mechanics of a Task Submission from the process of how they will be executed. •We use this instead of creating Threads separately. Muhammad Mateen| Android Mentor
  • 13. Threading Executor Service • Add lifecycle methods to Executor • Create N number of thread pool • Or Executors.newSingleThreadExecutor() for a Single runnable pool • shut down a executor will reject new Tasks Muhammad Mateen| Android Mentor
  • 14. Threading Executor Service •Methods shutDown() – don’t kill the running tasks shutDownNow() – kill the running tasks • Unused ExecutorService can be shutdown, to claim for its resources •Upon termination, an executor has no tasks actively executing, no tasks awaiting execution, and no new tasks can be submitted Muhammad Mateen| Android Mentor
  • 15. Threading Callables And Futures •Biggest Issue with Runnable ? •Callable return values after completion • Submitting callable to executor returns a Future • Futures can be used to check Callable’s status •Use the get method to retrieve the result of a Future Muhammad Mateen| Android Mentor