Android Topics UI Thread and Limited processing resources

Slides:



Advertisements
Similar presentations
Android Application Development A Tutorial Driven Course.
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.
Multithreading The objectives of this chapter are:
Cosc 4755 Phone programming: GUI Concepts & Threads.
Java Overview February 4, /4/2004 Assignments Due – Homework 1 Due – Reading and Warmup questions Project 1 – Basic Networking.
Android Tutorial Android Written in Java Utilizes Dalvik VM – Just in time (JIT) compilation since Android 2.2.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
About me Yichuan Wang Android Basics Credit goes to Google and UMBC.
Austin Java Users Group developerWorks article – µActor Library BARRY FEIGENBAUM, PH. D. 02/26/13.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Mobile Application Development using Android Lecture 2.
CS378 - Mobile Computing Intents.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
1 Web Based Programming Section 8 James King 12 August 2003.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Concurrent Programming and Threads Threads Blocking a User Interface.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
3 Important Performance tracking tools in an Android Application Development Workflow Here are 3 tools every Android application developer should familiarize.
Java Thread Programming
Multithreading The objectives of this chapter are:
Introduction to threads
Concurrency in Android
Chapter 13: Multithreading
Reactive Android Development
Event Loops and GUI Intro2CS – weeks
Threads and Multithreading
Android Runtime – Dalvik VM
Mobile Operating System
MAD.
Operating System (013022) Dr. H. Iwidat
Android Mobile Application Development
Lecture 21 Concurrency Introduction
Lecture 28 Concurrent, Responsive GUIs
Chapter 19 Java Never Ends
Android Programming Lecture 8
Android App Developing with communication included
Event loops.
Application Development A Tutorial Driven Course
CS371m - Mobile Computing Responsiveness.
Threads and Multithreading
CS323 Android Topics Network Basics for an Android App
Threads and Multithreading
Android Topics Asynchronous Callsbacks
Android Topics What are Intents? Implicit Intents vs. Explicit Intents
Add to the Coffee Ordering App
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Multithreaded Programming
Android Topics Threads and the MessageQueue
Android Topics Limited Resources and why we need to consider them.
Threads and Multithreading
Direct Manipulation.
Chapter 15 Multithreading
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Threads and Multithreading
Mobile Computing Dr. Mohsin Ali Memon.
Chapter 4: Threads.
Multithreading The objectives of this chapter are:
Event loops.
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Android Topics UI Thread and Limited processing resources Construct an exploratory app Three Experiments to test Threads

What are Intents? Most apps require us to send intents   Example: the Coffee Ordering App will require us to send off the order summary in an email. The app must launch an email app that exists on the device.  An intent is a message that requests some type of action to be performed by another app component. This could be a camera, email, Google Maps, or even an activity in another app.

Limited Processing Resources Mobile devices have a lot of resource constraints: memory and CPU (processing power). Low-processing devices must consider background threads for heavy processing. 

UI Thread UI Thread is the main thread of execution. All your code runs on the UI thread by default. The UI Thread is important thread for interacting with your user. Being able to design code that doesn’t blocking the UI thread is a very important concept.

Exploratory App Experiment with code that runs on UI thread. What happens when time-consuming work is done on the main thread. How can we use a background thread for time-consuming work? TASK: Build the layout.

Experiment 1 Start work Button and End work Button.  Test the app using a non intensive workload, displaying a message in the Log.i. public void workItOut(View view){ if (view.getId() == R.id.start_button) Log.i("THREAD DEMO", "Working on it"); else Log.i("THREAD DEMO", "Not Working"); }

Experiment 2 Send an unreasonable amount of output to the log window when the user clicks “Start Work”. The logcat should fill up with the string “Working on it”. Implement this by throw the Log.i statement in a loop and only stop when the user clicks the button labelled “Stop Work”. What happens?

Experiment 2 Produces a ANR Why?

Why did the ANR occur? The loop work is completely taking over the the UI Thread. Too much work on this thread often leads to a poor performing app or an application that doesn’t respond (ANR – Application Not Responding). The UI Thread MUST be able to respond to the user. Resource intensive work should never be done on the UI thread. Once it is blocked, it cannot handle anything else.

Android Operating System At runtime, Android Operating System hosts app as a process. Each app gets a dedicated process for execution.

Consider an Individual Process/App There can be any number of threads in a process. In most apps, there is a need to be smart in how you allocate work for the main thread, shown in red. If you have a task that takes a significant amount of time, such as 5 to 3 seconds, then it will block the UI Thread and eventually seriously impact the app.

Background Thread A best approach is to move time- consuming work off the main UI thread. Create a separate thread to can handle all tasks interfering with the main UI thread. An App can have multiple threads of execution running concurrently.

Creating a Background Thread in Java The class Thread implements Runnable. Runnable is an interface specifically for executing background threads. The Thread must define a method called run().

Solution to Experiment 3: public void workItOut(View view) { if (view.getId() == R.id.start_button) { if (!isDoingWork) { isDoingWork = true; new Thread(new Runnable() { @Override public void run() { while (isDoingWork) Log.i("THREAD DEMO", "Working on it"); } }).start(); } } else { isDoingWork = false; } }

Operations which should be carried out on separate threads.   Heavy calculations Long initialization Networking Database operations