Small talk with the UI thread

Slides:



Advertisements
Similar presentations
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Advertisements

All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
Cosc 5/4730 A little on threads and Messages: Handler class.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Concurrent Programming and Threads Threads Blocking a User Interface.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
Android Threads. Threads Android will show an “ANR” error if a View does not return from handling an event within 5 seconds Or, if some code running in.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
Concurrency in Android
Introduction to Operating Systems
Asynchronous Task (AsyncTask) in Android
Reactive Android Development
Multi Threading.
Instructor: Mazhar Hussain
Data Transport for Online & Offline Processing
CS240: Advanced Programming Concepts
Java Programming Language
Scheduler activations
Notifications and Services
Activities and Intents
Lecture 6: Process, Thread, Task
Reactive Android Development
PHP / MySQL Introduction
CS499 – Mobile Application Development
Counted Loops.
Mobile Computing With Android ACST 4550 Android Animation
Developing Android Services
Android Programming Lecture 8
Organizing Memory in Java
Light-weight Contexts: An OS Abstraction for Safety and Performance
Mobile Software Development Framework: Android
CS371m - Mobile Computing Responsiveness.
Processes and Threads.
Android Topics UI Thread and Limited processing resources
Mobile Computing With Android ACST 4550 Android Database Storage
Android Topics Asynchronous Callsbacks
Android Topics Threads and the MessageQueue
Android Developer Fundamentals V2
Lecture 5: Functions and Parameters
Planning and Storyboarding a Web Site
Lecture 9 Announcements.
Threads, Handlers, and AsyncTasks
Threads in Java James Brucker.
Service Services.
Using threads for long running tasks.
Lecture 6: Process, Thread, Task
Why Threads Are A Bad Idea (for most purposes)
CMSC 202 Threads.
Android Threads Dimitar Ivanov Mobile Applications with Android
Presentation transcript:

Small talk with the UI thread Landon Cox March 9, 2017

Android threads Thread: “a sequence of executing instructions” Every app has a main thread Processes UI events (taps, swipes, etc.) Updates the UI Apps will probably want other threads too Use threads to communicate over network Read/write to file system or database (anything slow)

Play analogy Threads Memory Process is like a play performance Program is like the play’s script What are the threads? Threads What is the memory? Memory

Multiple threads in memory Several actors on a single set Sometimes they interact (speak, dance) Sometimes they are apart (different scenes)

Private vs global thread state What is private to each thread? Current instruction (where actor is in his/her script) Local variables, stack (actor’s mindset) What is shared? Global variables, heap (props on set) Code (like the script)

Single-threaded app User App Server Pull screen Refresh request New messages request Check messages Horrible lag Send messages Recv messages Refresh UI Tap screen

Multi-threaded app User App (UI) App (bg) Server Pull screen Refresh request Msg request Check msgs Tap screen Refresh UI (tap) Send msgs Recv Update msgs Refresh UI (pull)

Threads in Android Main/UI thread Background threads Networking code cannot run on it File/database code probably shouldn’t run on it Only thread on which can View code can run Play analogy? something Harry cannot do … Background threads Place to run slow code (e.g., networking/db) Cannot run View code Play analogy? something only Harry can do … How do we update the UI w/ background data?

Each thread has an event queue. An event is a unit of work. App (UI) App (bg) UI event queue Each thread has an event queue. An event is a unit of work.

Threads process events sequentially (i.e., one at a time) App (UI) App (bg) UI event queue Threads process events sequentially (i.e., one at a time)

UI and bg threads communicate via the event queue. App (UI) App (bg) UI event queue UI and bg threads communicate via the event queue.

App (UI) App (bg) UI event queue BG thread works, generates an event specifying what the UI thread should do.

BG thread then adds the event to the queue for the UI thread. App (UI) App (bg) UI event queue BG thread then adds the event to the queue for the UI thread.

UI thread constantly loops over event queue. App (UI) App (bg) UI event queue UI thread constantly loops over event queue.

UI thread constantly loops over event queue. App (UI) App (bg) UI event queue UI thread constantly loops over event queue.

Updating the UI UI, non-UI threads talk via Handler/Looper class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop();      }  UI thread already setup with a Looper Use a Handler to pass data to the UI thread

Updating the UI To talk to the UI thread, create a Handler handler = new Handler(Looper.getMainLooper()); This ensures that Handler runs on the same thread as the Looper’s thread (UI thread in this case)

This Handler isn’t very useful … Updating the UI To talk to the UI thread, create a Handler handler = new Handler(Looper.getMainLooper()); This Handler isn’t very useful …

On what thread will handleMessage run? Updating the UI To talk to the UI thread, create a Handler handler = new Handler(Looper.getMainLooper()) { public void handleMessage(Message msg){ // do something with the msg } }; On what thread will handleMessage run? On the UI thread

Updating the UI To talk to the UI thread, create a Handler handler = new Handler(Looper.getMainLooper()) { public void handleMessage(Message msg){ // do something with the msg } }; What data does the method need to update the UI? Reference to UI element to update, update content (e.g., image data)

Updating the UI To talk to the UI thread, create a Handler handler = new Handler(Looper.getMainLooper()) { public void handleMessage(Message msg){ // do something with the msg } }; Where will the method get the data it needs? From the passed-in Message!

Use this to create create a new Message Updating the UI Let’s take a look at Message class Message { public int arg1; public int arg2; public Object obj; public Messenger replyTo; public int what; void sendToTarget(); static Message obtain (Handler h, …); } Use this to create create a new Message

Updating the UI Let’s take a look at Message class Message { public int arg1; public int arg2; public Object obj; public Messenger replyTo; public int what; void sendToTarget(); static Message obtain (Handler h, …); } Why this instead of calling the constructor? Makes it easier to re-use Messages

Use this to send the Message to another thread Updating the UI Let’s take a look at Message class Message { public int arg1; public int arg2; public Object obj; public Messenger replyTo; public int what; void sendToTarget(); static Message obtain (Handler h, …); } Use this to send the Message to another thread

Updating the UI Let’s take a look at Message class Message { public int arg1; public int arg2; public Object obj; public Messenger replyTo; public int what; void sendToTarget(); static Message obtain (Handler h, …); } How does the Message know where to send itself? Handler passed in to obtain()

Updating the UI Let’s take a look at Message class Message { public int arg1; public int arg2; public Object obj; public Messenger replyTo; public int what; void sendToTarget(); static Message obtain (Handler h, …); } What are these fields for? This is how a Message’s data is stored/retrieved

Updating the UI To talk to the UI thread, create a Handler MyMsg is a class that we defined handler = new Handler(Looper.getMainLooper()) { public void handleMessage(Message msg){ MyMsg mymsg = (MyMsg) msg.obj; View v = mymsg.getView (); // UI View to update String s = mymsg.getString (); // String data } }; We can define any functions we want, like getView and getString

Putting it all together mView = activity.findViewById(R.id.list); fillList (mList); handler = new Handler(Looper.getMainLooper()) { public void handleMessage(Message msg){ MyMsg mymsg = (MyMsg) msg.obj; TextView v = mymsg.getView (); List list = mymsg.getList (); // List of Strings for (String s : list) { v.append(s); } }; MyMsg mymsg = new MyMsg(mList, mView); Message m = Message.obtain(handler, mymsg); m.sendToTarget();

AsyncTasks Android will do all of this for you via … AsyncTask Creates a background thread Syncs it with the main thread To use AsyncTask, subclass it // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { … }; MyTask t = new MyTask; t.execute (new String[] {“www.cs.duke.edu”});

AsyncTask AsyncTask: simplest way to do background work // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls); void publishProgress(Integer … values); void onProgressUpdate(Integer … progress); void onPostExecute(Long result); } // to use an AsyncTask new MyTask.execute(url1, url2, url3);

AsyncTask AsyncTask: simplest way to do background work // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls); void publishProgress(Integer … values); void onProgressUpdate(Integer … progress); void onPostExecute(Long result); } // to use an AsyncTask new MyTask.execute(url1, url2, url3);

AsyncTask AsyncTask: simplest way to do background work // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls); void publishProgress(Integer … values); void onProgressUpdate(Integer … progress); void onPostExecute(Long result); } // to use an AsyncTask new MyTask.execute(url1, url2, url3);

AsyncTask AsyncTask: simplest way to do background work // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls); void publishProgress(Integer … values); void onProgressUpdate(Integer … progress); void onPostExecute(Long result); } // to use an AsyncTask new MyTask.execute(url1, url2, url3);

This is where most of the work is done AsyncTask AsyncTask: simplest way to do background work // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls); void publishProgress(Integer … values); void onProgressUpdate(Integer … progress); void onPostExecute(Long result); } // to use an AsyncTask new MyTask.execute(url1, url2, url3); This is where most of the work is done

AsyncTask AsyncTask: simplest way to do background work What thread does this run on? class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls) { int count=0; for (URL url: urls) { Downloader.downloadFile(url); publishProgress((int)(( count++*(float)urls.length)*100); } void publishProgress(Integer … values); void onProgressUpdate(Integer … progress) { // display our progress on the UI thread What thread does this run on? doInBackground runs on background thread publishProgress runs on background thread onProgressUpdate runs on UI thread What thread does this run on?

What thread does this run on? AsyncTask AsyncTask: simplest way to do background work // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls); void publishProgress(Integer … values); void onProgressUpdate(Integer … progress); void onPostExecute(Long result); } // to use an AsyncTask new MyTask.execute(url1, url2, url3); onPostExecute runs on UI thread What thread does this run on?

AsyncTask hides almost all of the Handler stuff AsyncTask: simplest way to do background work // subclass an AsyncTask class MyTask extends AsyncTask<URL, Integer, Long> { Long doInBackground(URL … urls); void publishProgress(Integer … values); void onProgressUpdate(Integer … progress); void onPostExecute(Long result); } // to use an AsyncTask new MyTask.execute(url1, url2, url3); AsyncTask hides almost all of the Handler stuff

Tools at your disposal Loopers/Handlers AsyncTasks Volley Gives you the most control Can run arbitrary code on a thread AsyncTasks Simple, easy way to run code on a thread May need more control than it offers Volley Simpler, easier way to make HTTP requests in the background

Volley request lifecycle

Tools at your disposal Loopers/Handlers AsyncTasks Volley Gives you the most control Can run arbitrary code on a thread AsyncTasks Simple, easy way to run code on a thread May need more control than it offers Volley Even simpler, easier way to make HTTP requests in the background Let’s use AsyncTasks to move SQLite off UI thread