Ts threading
Click here to load reader
Transcript of Ts threading
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
Muhammad Mateen| Android Mentor
• 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 ?
Threading
UI Thread
Muhammad Mateen| Android Mentor
•Things to remember
• No long operations
• Don’t interact other threads with UI thread
•The UI thread runs as a NORMAL PRIORITY thread.
Threading
UI Thread
Muhammad Mateen| Android Mentor
•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
Threading
Loopers and Handlers
Muhammad Mateen| Android Mentor
•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
Threading
Async Tasks
Muhammad Mateen| Android Mentor
•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
Threading
Async Tasks
Muhammad Mateen| Android Mentor
• 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
Threading
Async Tasks
Muhammad Mateen| Android Mentor
Methods
• onPreExecute()
• doInBackground()
• onProgressUpdate()
• onPostExecute()
Threading
Intent Service
Muhammad Mateen| Android Mentor
• 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
Threading
Executor
Muhammad Mateen| Android Mentor
• 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.
Threading
Executor Service
Muhammad Mateen| Android Mentor
• 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
Threading
Executor Service
Muhammad Mateen| Android Mentor
•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
Threading
Callables And Futures
Muhammad Mateen| Android Mentor
•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