Android Programming Lecture 8

Slides:



Advertisements
Similar presentations
FatMax Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 LicenseCreative Commons Attribution-NonCommercial-ShareAlike 2.5.
Advertisements

Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
IT Systems Multiprocessor System EN230-1 Justin Champion C208 –
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
4.7.1 Thread Signal Delivery Two types of signals –Synchronous: Occur as a direct result of program execution Should be delivered to currently executing.
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
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.
Concurrent Programming and Threads Threads Blocking a User Interface.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Lecture 1: Network Operating Systems (NOS) An Introduction.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Conway’s Game Of Live 1 Fall 2014 CS7020: Game Design and Development.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
Java Thread Programming
Android Application -Architecture.
Concurrency in Android
Small talk with the UI thread
Chapter 4: Threads.
Chapter 4: Threads.
Processes and threads.
Asynchronous Task (AsyncTask) in Android
Reactive Android Development
Advanced OS Concepts (For OCR)
CS240: Advanced Programming Concepts
OPERATING SYSTEMS CS3502 Fall 2017
Scheduler activations
Lecture Topics: 11/1 Processes Process Management
Notifications and Services
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Multithreaded Programming
Lecture 21 Concurrency Introduction
Lecture 6: Process, Thread, Task
Chapter 2.2 : Process Scheduling
CS499 – Mobile Application Development
Operating System Concepts
Mobile Computing With Android ACST 4550 Android Animation
Java Byte IPC: Part 6-Summary
Threads II IS
Distributed Systems - Comp 655
Chapter 4 Multithreading programming
Chapter 4: Threads.
CS703 - Advanced Operating Systems
CS371m - Mobile Computing Responsiveness.
Android Topics UI Thread and Limited processing resources
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads Chapter 4.
Android Topics Asynchronous Callsbacks
Multithreaded Programming
Android Topics Threads and the MessageQueue
Introduction to AppInventor
Chapter 4: Threads.
Threads, Handlers, and AsyncTasks
Threads in Java James Brucker.
Using threads for long running tasks.
Mobile Computing Dr. Mohsin Ali Memon.
Chapter 4: Threads.
Chapter 4: Threads.
Android Threads Dimitar Ivanov Mobile Applications with Android
Mobile Computing With Android ACST 4550 Android Animation
Threads CSE 2431: Introduction to Operating Systems
Asynchronous Programming CS Programming Languages for Web Applications
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Android Programming Lecture 8 Testing notes Multi-threading

Timesharing Operating Systems All modern operating systems are capable of sharing processor(s) between multiple users and processes Process scheduling algorithms There are often lots (hundreds) of processes running Many are managed by the system Some are user executed

Process A process is typically defined as execution of a single program on a computing device Processes are scheduled by the OS Processes are owned by a user or the OS (root) Processes may have a long lifetime (a web server) or a short lifetime (one run of a short program) Processes do not share memory between each other Most OS support inter-process communication

Threads A thread is a component of a process Usually considered the smallest unit that can be scheduled by an OS Can share memory and instructions with other threads in the same process Most OS support inter-thread communication

Process V.S. Thread https://www.youtube.com/watch?v=O3EyzlZxx3g

Timesharing Operating Systems On a single processor system, “Round robin” scheduling system switches between threads and processes fast enough that it appears that multiple programs are running Only one is every truly executing at a time On multi-core or multi-processor systems, Threads or processes can be allocated across the processors True parallel processing “Round-robin” scheduling still occurs

Android Android starts a new Linux process for the application with a single thread of execution All components of the same application run in the same process and thread This thread is called the “main” thread or “UI” thread Any updates to the UI/display must be accomplished on the main thread

Slow Android App If a component of the work takes a long time, the rest of the work will be “blocked” For example, a long time to access data across the network prevents responding to any GUI events In the Android OS, if a GUI doesn’t respond to an input event in < five seconds, then it is considered unresponsive and the OS will try to kill it!

Multi-threading Put non-UI work on a separate thread For example, download an image, connect to a remote server, open a web browser… Android App UI Main Thread Java Threads BG Background Thread

What technique shall I use for my background thread execution? UI Android App Java Threads BG What technique shall I use for my background thread execution? Linux Process UI BG Application Start Application End Time

Asynchronous Techniques Thread Executor HandlerThread AsyncTask Service IntentService AsyncQueryHandler Loader

Handler – Loop - Message Message Queue UI Thread Message 1 Thread 1 sendMessage() Message 2 Looper Handler Message 3 Message 4 Thread 2

Handler UI Thread Message Queue Message 1 @Override protected void onCreate(Bundle savedInstanceState) { … final Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x123) { // Do the things when the message } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // Open a new thread } }, 0, 100); Thread 1 Looper UI Thread Thread 1 @Override public void run() { // To be executed in the thread … } handler.sendEmptyMessage(0x123);

Timer Thread A thread manages a Runnable object Timer class The java.util.Timer class provides facility for threads to schedule tasks for future execution in a background thread. This class schedules tasks for one-time execution, or for repeated execution at regular intervals. The Timer class in the java.util package schedules instances of a class called TimerTask final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // To be executed in the thread } }, 0, 100); Timer Class Reference: http://developer.android.com/reference/java/util/Timer.html Runnable Class Reference: http://developer.android.com/reference/java/lang/Runnable.html

Handler Allow you to send and process Message and Runnable objects associated with a thread’s Message Queue Handler instance is associated with a single thread and that thread’s message queue When you create a new Handler, it is bound to the thread and message queue of the thread that is creating it It will deliver Message and Runnable to that message queue and execute them as they come out of the message queue Need only one Handler object per activity Handler Message Queue Message 1 Looper

Handler Loop Create and Start 1 Message Queue Add Process 3 2 1 2 3 onCreate(){ … } 1 Handler h = new Handler() { @Override public void handleMessage(Message msg) { //Process message } }; 2 h.sendEmptyMessage(msg); 3

Message Class Defines a message containing a description and arbitrary data object that can be sent to a Handler. A message contains two extra int fields and an extra object field what: User-defined message code so that the recipient can identify what this message is about. obj: An arbitrary object to send to the recipient. While the constructor of Message is public, the best way to get one of these is to call Message.obtain() or one of the Handler.obtainMessage() methods, which will pull them from a pool of recycled objects.

Handler UI Thread Message Queue Message 1 @Override protected void onCreate(Bundle savedInstanceState) { … final Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x123) { // Do the things when the message } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // Open a new thread } Thread 1 Looper UI Thread Thread 1 @Override public void run() { // To be executed in the thread … handler.sendEmptyMessage(0x123); }

Handler Methods to Send Message sendEmptyMessage(int what) Sends a Message containing only the what value. sendEmptyMessageDelayed(int what, long delayMillis) Sends a Message containing only the what value, to be delivered at a specific time. sendEmptyMessageAtTime(int what, long uptimeMillis) Sends a Message containing only the what value, to be delivered after the specified amount of time elapses. sendMessage(Message msg) Pushes a message onto the end of the message queue after all pending messages before the current time. sendMessageAtFrontOfQueue(Message msg) Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop. sendMessageAtTime(Message msg, long uptimeMillis) Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds) uptimeMillis.

References: Android Programming: The Big Nerd Ranch Guide Chapter 24 (Safari Books Online)