Ts threading


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ts threading

  1. 1. Boutique product development companyIt is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.
  2. 2. ThreadingMuhammad Mateen| Android Mentor
  3. 3. ThreadingTopics covered in the presentation • UI Thread • Loppers & Handlers • Async Tasks • Intent Services • Executor • Executor Service • Callables & Futures Muhammad Mateen| Android Mentor
  4. 4. ThreadingUI 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. 5. ThreadingUI 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. 6. ThreadingUI 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. 7. ThreadingLoopers 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. 8. ThreadingAsync 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. 9. ThreadingAsync 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. 10. ThreadingAsync Tasks Methods • onPreExecute() • doInBackground() • onProgressUpdate() • onPostExecute() Muhammad Mateen| Android Mentor
  11. 11. ThreadingIntent 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. 12. ThreadingExecutor • 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. 13. ThreadingExecutor 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. 14. ThreadingExecutor Service•Methods shutDown() – don’t kill the running tasks shutDownNow() – kill the running tasks• Unused ExecutorService can be shutdown, to claim for itsresources•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. 15. ThreadingCallables 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