Download presentation
Presentation is loading. Please wait.
1
Mobile Computing Dr. Mohsin Ali Memon
2
Threads • Basic approach
- Make a task list with Executors.newFixedThreadPool - Add tasks to list with taskList.execute(someRunnable) • Three variations on the theme - Separate classes that implement Runnable - Main Activity implements Runnable - Inner classes that implement Runnable - Race conditions and synchronization • Related topics - Helpful Thread-related methods
3
Motivation for Concurrent Programming
• Pros - Advantages even on single-processor systems • Efficiency - Downloading image files • Convenience - A clock icon • Responsiveness - If you block the main thread, event handlers will not respond - Some devices have multiple cpus or cores • Find out via Runtime.getRuntime().availableProcessors() • Cons - Significantly harder to debug and maintain than single- threaded apps
4
Steps for Concurrent Programming
5
Steps for Concurrent Programming cont’d
6
Approach One: Separate Classes that Implement Runnable
7
Separate Runnable Class: Template Code
This code should not update the UI. If you need to update UI, use AsyncTask class.
8
Thread Mechanism One: Example
9
Thread Mechanism One: Example con’d
10
Helper Class
11
Thread Mechanism One: Example Result
12
Pros and Cons of Approach
13
Approach Two: Main App Implements Runnable
14
Main App Implements Runnable: Template Code
15
Thread Mechanism Two: Example
16
Thread Mechanism Two: Results
17
Pros and Cons of Approach
18
Approach Three: Inner Class that Implements Runnable
19
Inner Class implements Runnable: Template code
20
Thread Mechanism Three: Example
21
Thread Mechanism Three: Example cont’d
22
Thread Mechanism Three: Example Result
23
Pros and cons of approach
24
Summary
25
Race Condition
26
Race Conditions: Example
27
Race Conditions: Example cont’d
28
Race Conditions: Result
29
Race Conditions: Solution
30
Race Conditions: Solution
31
Helpful –Thread related Methods
32
Methods in ExecutorService Class
33
Threads Cannot Update UI
34
Updating UI All at Once
35
Displaying a network image
36
BitmapUtils: Getting a Bitmap
37
Making a View for an Image
38
BitmapUtils:Getting a view for image
39
Example: Unthreaded Image Viewer
40
Activity
41
Results
42
Updating UI with View.post
43
Example: Multithreaded Image Viewer
44
Main Activity
45
Main Activity Button Handler
46
Main Activity: Runnable Inner Class (For Background)
47
Main Activity: Runnable Inner Class (For UI Thread)
48
Results
49
Downloading Images via AsyncTask
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.