Lecture 6: Process, Thread, Task

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Chapter 4 Threads Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
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.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
1 Chapter 4 Threads Threads: Resource ownership and execution.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Chapter 8 Windows Outline Programming Windows 2000 System structure Processes and threads in Windows 2000 Memory management The Windows 2000 file.
UI Design Patterns & Best Practices Mike Wolfson July 22, 2010.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
ANDROID L. Grewe Components  Java Standard Development Kit (JDK) (download) (latest version)  AndroidStudio.
Working in the Background Radan Ganchev Astea Solutions.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
CS333 Intro to Operating Systems Jonathan Walpole.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
Lecture 2: Android Concepts
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
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.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Embedded Real-Time Systems
Concurrency in Android
UI Design Patterns & Best Practices
Lecture 2: Android Concepts
Operating Systems Case Study
OPERATING SYSTEM CONCEPT AND PRACTISE
Processes and threads.
Process Management Process Concept Why only the global variables?
PROCESS MANAGEMENT IN MACH
Lecture 7: Service Topics: Services, Playing Media.
Instructor: Mazhar Hussain
Operating Systems (CS 340 D)
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Android Mobile Application Development
Chapter 4 Threads.
Intro to Processes CSSE 332 Operating Systems
CS499 – Mobile Application Development
Operating Systems (CS 340 D)
Android Programming Lecture 9
Android Programming Lecture 8
Application Development A Tutorial Driven Course
CS371m - Mobile Computing Responsiveness.
Operating Systems Lecture 12.
Process Description and Control
Android Topics UI Thread and Limited processing resources
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads and Concurrency
Threads Chapter 4.
Android Topics Asynchronous Callsbacks
Dr. Mustafa Cem Kasapbaşı
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Android Topics Threads and the MessageQueue
Android Developer Fundamentals V2
Chapter 3: Processes.
CS510 Operating System Foundations
Lecture 7: Service Topics: Services, Broadcast Receiver, Playing Media.
Using threads for long running tasks.
Lecture 6: Process, Thread, Task
Lecture 2: Android Concepts
Chapter 4: Threads.
Presentation transcript:

Lecture 6: Process, Thread, Task Topics: Process, Thread, Worker Thread, Async Task

Today’s class: Concepts What is a process? Thread? (Lecture) In Android? Problem (code) What is the limit of the main thread? Creating the problem scenario. Back-to-Concepts (lecture) Running on UI thread. Async Task Solution (code: all) Create the problem scenario Use Async Task

Concepts

Program vs. Process A program is a set of instructions + data stored in an executable image. (passive) A process is a program “in action” (dynamic) Program counter CPU registers Stacks States Process: Runs in own virtual address space. Uses secure, kernel managed mechanism to interact with other processes. For more details: http://www.tldp.org/LDP/tlk/kernel/processes.html

Program vs. Process Chai Tea Recipe: 1. Add water in pan. 2. Add sugar, tea leaves, spices. 3. Bring to boil and simmer. 4. Add milk. 5. Bring to boil and simmer. 6. Strain tea in teapot.

Thread A Thread is a flow of execution inside a process. Threads in a process share the same virtual memory address space. Context switching between threads are faster than context switching between processes. 1:1 mapping between kernel space and user space threads.

Android Processes and Threads Default Behavior: All components of an App run in the same thread (the main/UI thread) and the same process. C1 C2 P T C3 Default However, you can: Arrange for components to run in different processes. Create multiple threads per process. C1 P1 T1 C2 P2 T2 C3 It’s possible

android:process Specify android:process in AndroidManifest.xml: <activity>, <service>, <receiver>, <provider> <application> (all components; same UID and certificate) Potential Course Project

Process creation and removal Creation: When the first component of an App is run and there is currently no process for that App. Removal: Remove old processes to reclaim memory for new or more important processes. Which process should be killed?

Process Hierarchy From High to Low Priority Priority Type Description 1 Foreground On-going interactions; activity, service, broadcast 2 Visible Not in the foreground, but visible 3 Service Playing music, downloading stuffs 4 Background onStop() called, not visible, LRU kill 5 Empty Lowest priority Kill them first Foreground (highest priority, interactions on-going, can be Activity, Service, Broadcast receiver) Visible (not foreground but visible) Service (startService, playing music/downloads) Background (onStop called, no visible, LRU kill) Empty (lowest priority) Visible Activity Classified as Visible Process Service P

Main Thread Created when an App is launched Often called the UI thread Dispatched events to widgets android.widget and android.view package

Keeping Apps Responsive Problem Keeping Apps Responsive

Problem: keeping an App responsive Testing the limit of the UI thread: How big a task we should do in UI thread? How big a task we can do in UI thread? What is the alternative? What is the limitation of this alternative? void clickEventHandler(View button) { //what is my limit? //Let’s experiment! } 10 seconds => warning in logcat (# frames skipped) 10 seconds, multiple clicks => dialog box

Worker Thread Used to make UI thread light-weight, responsive. Limitation: Cannot access UI toolkit elements (e.g. views) from another thread. Runs in the new worker Thread Runs in UI Thread public void onClick(View v) { new Thread(new Runnable() { public void run() { Bitmap b = loadImageFromNetwork("http://example.com/i.png"); mImageView.setImageBitmap(b); } }).start(); } Do not try this (i.e. accessing an ImageView which was created in UI thread and now being accessed in a worker thread)

So … two lessons to remember Do not block the UI thread Do not access the Android UI toolkit from outside the UI thread. Caution: Don’t run long running (5 sec+) task in the UI thread

Solutions

1. Access UI Thread from Other Threads Use one of these three: Activity.runOnUiThread (Runnable) View.post (Runnable) View.postDelayed (Runnable, long) Runs in UI Thread Runs in worker Thread public void onClick(View v) { new Thread(new Runnable() { public void run() { final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png"); mImageView.post(new Runnable() { public void run() { mImageView.setImageBitmap(bitmap); } }); } }).start(); }

2. Async Task Performs the blocking operations in a worker thread, and publishes the results on the UI thread. public void onClick(View v) { new DownloadImageTask().execute("http://example.com/image.png"); } private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protected void onPostExecute(Bitmap result) { mImageView.setImageBitmap(result); } } Runs in worker Thread Runs in UI Thread http://developer.android.com/reference/android/os/AsyncTask.html

References (study these) http://developer.android.com/guide/components/processes-and-threads.html

Thread-safe methods and IPC Make sure, when multiple threads can invoke a method, the method is thread-safe. Android offers inter-process communication using remote procedure calls (RPCs) Content Provider Query(). Insert(), delete(), update() Another Activity