Android Concurrency & Synchronization: Introduction

Transcription

Android Concurrency &Synchronization: IntroductionDouglas C. edu/ schmidtInstitute for SoftwareIntegrated SystemsVanderbilt UniversityNashville, Tennessee, USACS 282 Principles of Operating Systems IISystems Programming for Android

Android Concurrency & SynchronizationD. C. SchmidtIntroduction Explore the motivations for & challenges of concurrent softwareConcurrent software can simultaneously run multiplecomputations that potentially interact with each other2

Android Concurrency & SynchronizationD. C. SchmidtIntroduction Explore the motivations for & challenges of concurrent software Understand the mechanisms that Androidprovides to manage multiple threads thatrun concurrently within a processProcess AProcess BProcess C3

Android Concurrency & SynchronizationD. C. SchmidtIntroduction Explore the motivations for & challenges of concurrent software Understand the mechanisms that Androidprovides to manage multiple threads thatrun concurrently within a process Some Android mechanisms are based onstandard Java threading & lockingmechanisms4

Android Concurrency & SynchronizationD. C. SchmidtIntroductionLooper Explore the motivations for & challenges of concurrent software Understand the mechanisms that AndroidMessageBackgroundQueueprovides to manage multiple threads thatThread Arun concurrently within a processHandler Some Android mechanisms are based onstandard Java threading & lockingMessagemechanismsMessageMessage Other mechanisms are based onMessageAndroid concurrency idiomsHandlerMessageBackgroundThread BMessageMessageMessageUI Thread(main thread)5AsyncTask

Android Concurrency &Synchronization: Part 1Douglas C. edu/ schmidtInstitute for SoftwareIntegrated SystemsVanderbilt UniversityNashville, Tennessee, USACS 282 Principles of Operating Systems IISystems Programming for Android

Android Concurrency & SynchronizationD. C. SchmidtLearning Objectives in this Part of the Module Understand the motivations for & challenges of concurrent software7

Android Concurrency & SynchronizationD. C. SchmidtMotivations for Concurrent Software Leverage hardware/softwareadvances e.g., multi-core processors& multi-threaded operatingsystems, virtual machines,& phoneshas more info

Android Concurrency & SynchronizationD. C. SchmidtMotivations for Concurrent Software Leverage hardware/softwareadvances Simplify program structure e.g., by allow blocking operationsLooperMessageQueue Classic singlearchitectures can’tperform blockingoperations This complicates appimplementations bydecoupling the flow ofcontrol in time & spaceMessageMessageMessageMessageMessageMessageUI Thread(main thread)9

Android Concurrency & SynchronizationD. C. SchmidtMotivations for Concurrent Software Leverage hardware/softwareadvances Simplify program structure e.g., by allow blocking operationsMessageQueueBackgroundThread AHandlerModern multi-threadedarchitectures supportblocking I/O in andlerMessageBackgroundThread BMessageMessageMessageUI Thread(main thread)10AsyncTask

Android Concurrency & SynchronizationD. C. SchmidtMotivations for Concurrent Software Leverage hardware/softwareadvances Simplify program structure e.g., by allow blocking operationsMulti-threadedAndroid exampleprivate Bitmap bitmap;final ImageView iview .final Button button .button.setOnClickListener(new OnClickListener() {public void onClick(View v) {new Thread(new Runnable() {public void run() {bitmap downloadImage(URI);iview.post(new Runnable() {public void run() { threads.html#WorkerThreads

Android Concurrency & SynchronizationD. C. SchmidtMotivations for Concurrent Software Leverage hardware/softwareadvances Simplify program structure e.g., by allow blocking operationsprivate Bitmap bitmap;Handles button clicksfinal ImageView iview .final Button button .button.setOnClickListener(new OnClickListener() {public void onClick(View v) {Download an imagenew Thread(new Runnable() {public void run() {bitmap downloadImage(URI);iview.post(new Runnable() {public void run() { iview.setImageBitmap(bitmap);}});Display bitmap in the UI thread}}).start();Start a new cesses-and-threads.html#WorkerThreads

Android Concurrency & SynchronizationD. C. SchmidtMotivations for Concurrent Software Leverage hardware/softwareadvances Simplify program structure Increase performance Parallelize computations& communications13

Android Concurrency & SynchronizationD. C. SchmidtMotivations for Concurrent Software Leverage hardware/softwareadvances Simplify program structure Increase performance Improve response-time e.g., don’t starve theUI thread14

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental ComplexitiesStem from limitations with development tools & techniques15

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Low-level APIs Tedious, error-prone, & non-portable16See www.dre.vanderbilt.edu/ schmidt/PDF/BC-schmidt.pdffor more info

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Low-level APIstypedef struct{ char message [20]; int thread id ; } PARAMS;void *print hello world (void *ptr) {CastPARAMS *params (PARAMS *) ptr;fromprintf ("%s from thread %d\n",void *params- message , params- thread id );}int main (void) {pthread t thread; PARAMS params;params.thread id 1; strcpy (params.message , "Hello World");Not portable to non-POSIX platformspthread create (&thread, 0, &print hello world,(void *) ¶ms);/* . */pthread join(thread, 0);Cast to void *return 0;}“Quasi-typed”17thread handlePointer-tofunction

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Low-level APIstypedef struct{ char message [20]; int thread id ; } PARAMS;void *print hello world (void *ptr) {PARAMS *params (PARAMS *) ptr;printf ("%s from thread %d\n",params- message , params- thread id );}int main (void) {pthread t thread; PARAMS params;params.thread id 1; strcpy (params.message , "Hello World");pthread create (&thread, 0, &print hello world,(void *) ¶ms);/* . */pthread join(thread, 0);return 0;}Other C threading APIs have 18similar accidental complexities

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Low-level APIs Limited debugging tools19

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Low-level APIs Limited debugging tools20See www.dre.vanderbilt.edu/ schmidt/PDF/DSIS.pdf& www.fluid.cs.cmu.edu

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Inherent ComplexitiesStem fromfundamental domainchallenges21

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Inherent Complexities SynchronizationSynchronization is the application ofmechanisms to ensure that two concurrentlyexecuting threads do not execute specificportions of a program at the same time22en.wikipedia.org/wiki/Synchronization (computer science)has more info

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Inherent Complexities SynchronizationScheduling is the method by which Schedulingthreads, processes, or data flows aregiven access to system resources23en.wikipedia.org/wiki/Scheduling (computing)has more info

Android Concurrency & SynchronizationD. C. SchmidtChallenges for Concurrent Software Accidental Complexities Inherent Complexities Synchronization Scheduling Deadlock needs T1 owns A deadlock is a situation in which two or morecompeting actions are each waiting for theother to finish, and thus neither ever does Accidental Complexities Low-level APIs Limited debugging tools owns L2 Inherent Complexities SynchronizationT2 Scheduling DeadlocksL1 needs 24See en.wikipedia.org/wiki/Deadlockfor more info

Android Concurrency & SynchronizationD. C. SchmidtSummary Concurrent software helps Leverage advances in hardware technology Meet the quality & performance needs of apps & services25

Android Concurrency & SynchronizationD. C. SchmidtSummary Concurrent software helps Leverage advances in hardware technology Meet the quality & performance needs of apps & services Successful concurrent software solutions must address key accidental& inherent complexities arising from Limitations with developmenttools/techniques26

Android Concurrency & SynchronizationD. C. SchmidtSummary Concurrent software helps Leverage advances in hardware technology Meet the quality & performance needs of apps & services Successful concurrent software solutions must address key accidental& inherent complexities arising from Limitations with developmenttools/techniques Fundamental domainchallenges27

Android Concurrency &Synchronization: Part 2Douglas C. edu/ schmidtInstitute for SoftwareIntegrated SystemsVanderbilt UniversityNashville, Tennessee, USACS 282 Principles of Operating Systems IISystems Programming for Android

Android Concurrency & SynchronizationD. C. SchmidtLearning Objectives in this Part of the Module Understand how to program Java mechanisms available in Android toimplement concurrent apps that process requests simultaneously viamultithreadingProcess AProcess B29Process C

Android Concurrency & SynchronizationD. C. SchmidtOverview of Java Threads in Android Android implements manystandard Java concurrency& synchronization classes30See ncy

Android Concurrency & SynchronizationD. C. SchmidtOverview of Java Threads in Android Android implements manyProcess Astandard Java concurrency& synchronization classes Conceptual view Concurrent computationsrunning in a (Linux)process that cancommunicate with eachother via shared memoryor message passing31Process BProcess C

Android Concurrency & SynchronizationD. C. SchmidtOverview of Java Threads in Android Android implements manyProcess AProcess BProcess Cstandard Java concurrency& synchronization classes Conceptual view Implementation view Each Java thread has aprogram counter & a stack(unique) The heap & static areasare shared across threads(common)32See nd-threads.html

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run by either Extending the Thread classThreadpublic class MyThreadextends Thread {public void run() {//code to run goes here}}MyThreadMyThread myt new MyThread();myt.start();Starting a thread using anamed class (or inner class)33

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run by either Extending the Thread class Implementing the Runnable interfaceRunnablepublic interface Runnable {public void run();}public class MyRunnableimplements Runnable {public void run() {//code to run goes here}}MyRunnable myr new MyRunnable();new Thread(myr).start();34MyRunnableStarting a thread using anamed implementation ofRunnable

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run by either Extending the Thread class Implementing the Runnable interfaceRunnablepublic interface Runnable {public void run();}new Thread(new Runnable() {public void run(){//code to run goes here}}).start();MyRunnableStarting a thread using ananonymous class (or innerclass) as the Runnable35

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run Android calls the Thread/Runnablerun() method after a new threadstarts up: MyComponentonCreate(): MyThreadnew()start()run()Runconcurrently36

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run Android calls the Thread/Runnable: My: MyThreadrun() method after a new threadComponentstarts up You can run any code inonCreate()new()a thread, but it must beinside of a run() methodor called from a run()start()methodRunconcurrently37run()

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run Android calls the Thread/Runnable: My: MyThreadrun() method after a new threadComponentstarts up The thread can be activeonCreate()new()as long as the run() methodhasn’t returned Naturally, the Android schedulerstart()can suspend/resume threadsjoin()38run()

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run Android calls the Thread/Runnable: My: MyThreadrun() method after a new threadComponentstarts up The thread can be activeonCreate()new()as long as the run() methodhasn’t returned Naturally, the Android schedulerstart()can suspend/resume threadsrun() If you want thread to run “forever,”you need to have a while(true)statement in that run() methodjoin()39

Android Concurrency & SynchronizationD. C. SchmidtUsing Java Threads in Android All threads must be given some codeto run Android calls the Thread/Runnable: Myrun() method after a new threadComponentstarts up The thread can be activeonCreate()as long as the run() methodhasn’t returned When run() returns the threadis no longer active40

Android Concurrency & SynchronizationD. C. SchmidtSummary Some concurrency mechanisms providedby Android are based on standard Javathreading classes41

Android Concurrency &Synchronization: Part 3Douglas C. edu/ schmidtInstitute for SoftwareIntegrated SystemsVanderbilt UniversityNashville, Tennessee, USACS 282 Principles of Operating Systems IISystems Programming for Android

Android Concurrency & SynchronizationD. C. SchmidtLearning Objectives in this Part of the Module Understand how the Java concurrency mechanisms available in Android areimplemented43

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()44Terminated

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()45Terminated

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()46Terminated

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()47Terminated

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()48Terminated

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()49Terminated

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()50Terminated

Android Concurrency & SynchronizationD. C. SchmidtState Machine for Java Threads in Androidnew MyThread()resourceobtainedBlockingattempt to accessguarded ingsleeptimeelapsedrun() methodreturnsSleepingmyThread.sleep()51Terminated

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()52

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method53See libcore/luni/src/main/java/java/lang/Thread.java

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method3. VMThread.create() // Native method54See libcore/luni/src/main/java/java/lang/VMThread.java

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method3. VMThread.create() // Native method4. Dalvik java lang VMThread create(const u4* args,JValue* pResult) // JNI method55See dalvik/vm/native/java lang VMThread.cpp

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method3. VMThread.create() // Native method4. Dalvik java lang VMThread create(const u4* args,JValue* pResult) // JNI method5. dvmCreateInterpThread(Object* threadObj,int reqStackSize) // Dalvik method56See dalvik/vm/Thread.cpp

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method3. VMThread.create() // Native method4. Dalvik java lang VMThread create(const u4* args,JValue* pResult) // JNI method5. dvmCreateInterpThread(Object* threadObj,int reqStackSize) // Dalvik method6. pthread create(&threadHandle, &threadAttr,interpThreadStart, newThread) // Pthreads methodRuntimethreadstack57See bionic/libc/bionic/pthread.c

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method3. VMThread.create() // Native method4. Dalvik java lang VMThread create(const u4* args,JValue* pResult) // JNI method5. dvmCreateInterpThread(Object* threadObj,int reqStackSize) // Dalvik method6. pthread create(&threadHandle, &threadAttr,interpThreadStart, newThread) // Pthreads method7. interpThreadStart(void* arg) // AdapterRuntimethreadstack58See dalvik/vm/Thread.cpp

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method3. VMThread.create() // Native method4. Dalvik java lang VMThread create(const u4* args,JValue* pResult) // JNI method5. dvmCreateInterpThread(Object* threadObj,int reqStackSize) // Dalvik method6. pthread create(&threadHandle, &threadAttr,interpThreadStart, newThread) // Pthreads methodRuntimethreadstack7. interpThreadStart(void* arg) // Adapter8. dvmCallMethod(self, run,self- threadObj,&unused) // Dalvik method59See dalvik/vm/interp/Stack.cpp

Android Concurrency & SynchronizationD. C. SchmidtStarting Java Threads When start() is called on a Java Thread object a whole series of steps occur1. MyThread.start()2. Thread.start() // Java method3. VMThread.create() // Native method4. Dalvik java lang VMThread create(const u4* args,JValue* pResult) // JNI method5. dvmCreateInterpThread(Object* threadObj,int reqStackSize) // Dalvik method6. pthread create(&threadHandle, &threadAttr,interpThreadStart, newThread) // Pthreads methodRuntimethreadstack7. interpThreadStart(void* arg) // Adapter8. dvmCallMethod(self, run,self- threadObj,&unused) // Dalvik method9. MyThread.run() // User-defined hook60

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread If you are going to create a long running operation inside of your run()method, you must ensure your code can stop voluntarily!Process61

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread One way to stop a thread is to use the interrupt() method This method sends anThread t1 new Thread(new Runnable() {interrupt request topublic void run(){the designated threadfor (int i 0;i input.length;i ) {process(input[i]);if (Thread.interrupted())throw );62

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread One way to stop a thread is to use the interrupt() method This method sends anThread t1 new Thread(new Runnable() {interrupt request topublic void run(){the designated threadfor (int i 0; Check Thread.interrupted()i input.length;periodically to see if thei ) {thread’s been stopped &process(input[i]);throw InterruptedExceptionif (Thread.interrupted())throw );63

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread One way to stop a thread is to use the interrupt() method This method sends anThread t1 new Thread(new Runnable() {interrupt request topublic void run(){the designated threadfor (int i 0; Check Thread.interrupted()i input.length;periodically to see if thei ) {thread’s been stopped &process(input[i]);throw InterruptedExceptionif (Thread.interrupted())throw InterruptedException(); Certain blocking operations}will be automatically be}interrupted e.g., wait(), join(), sleep() t1.start();& blocking I/O calls.t1.interrupt();64See tml#interrupt()

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread One way to stop a thread is to use the interrupt() method Another way is to use a “stop”public class MyRunnableflagimplements Runnable {private volatile booleanrunning true;public void stop() {running false;}public void run() {while(running ) {// do stuff}}}65

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread One way to stop a thread is to use the interrupt() method Another way is to use a “stop”public class MyRunnableflagimplements Runnable {private volatile boolean Add a volatile booleanrunning true;flag “running ” to yourpublic void stop() {class that implementsrunning false;Runnable} Initially, set “running ”public void run() {to truewhile(running ) {// do stuff}}}66en.wikipedia.org/wiki/Volatile variable#In Javahas more on volatile

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread One way to stop a thread is to use the interrupt() method Another way is to use a “stop”public class MyRunnableflagimplements Runnable {private volatile boolean Add a volatile booleanrunning true;flag “running ” to yourpublic void stop() {class that implementsrunning false;Runnable} Have a stop() method thatpublic void run() {sets “running ” to falsewhile(running ) {// do stuff}}}67

Android Concurrency & SynchronizationD. C. SchmidtStopping Java Threads Other than returning from run(), there’s no “stop” method for a Java Thread One way to stop a thread is to use the interrupt() method Another way is to use a “stop”public class MyRunnableflagimplements Runnable {private volatile boolean Add a volatile booleanrunning true;flag “running ” to yourpublic void stop() {class that implementsrunning false;Runnable} Have a stop() method thatpublic void run() {sets “running ” to falsewhile(running ) {// do stuff Check “running ” periodically}to see if the thread’s been}stopped}68This solution requires developers to periodicallycheck if thread was stopped

Android Concurrency & SynchronizationD. C. SchmidtSummary Java Threads are implemented using various methods & functions defined bylower layers of the Android software stack69

Android Concurrency & Synchronization D. C. Schmidt 2 Explore the motivations for & challenges of concurrent sof