40
Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA Types of Frameworks in AsyncTask (Part 1)

Types of Frameworks in AsyncTask (Part 1)schmidt/cs891s/2018-PDFs/L2... · 2018. 3. 14. · 3 • Understand what black-box & white-box framework are… & how AsyncTask implements

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

  • Douglas C. [email protected]

    www.dre.vanderbilt.edu/~schmidt

    Institute for Software Integrated Systems

    Vanderbilt University Nashville, Tennessee, USA

    Types of Frameworks in AsyncTask (Part 1)

    mailto:[email protected]

  • 2

    Learning Objectives in this Part of the Lesson• Understand what black-box & white-box framework are…

    See www.dre.vanderbilt.edu/~schmidt/PDF/DRC.pdf

    White-boxBlack-box

    http://www.dre.vanderbilt.edu/%7Eschmidt/PDF/DRC.pdf

  • 3

    • Understand what black-box & white-box framework are… & how AsyncTaskimplements both types of frameworks

    Learning Objectives in this Part of the Lesson

    White-boxBlack-box

  • 4

    Common Types of Frameworks

  • 5

    Common Types of Frameworks• Black-box frameworks only

    require understanding external interfaces of objects

  • 6

    Common Types of Frameworks• Black-box frameworks only

    require understanding external interfaces of objects• Framework elements typically

    reused by parameterizing & assembling objects

  • 7

    Common Types of Frameworks• Black-box frameworks only

    require understanding external interfaces of objects• Framework elements typically

    reused by parameterizing & assembling objects

    • White-box frameworks require understanding some parts of the framework implementation

  • 8

    Common Types of Frameworks• Black-box frameworks only

    require understanding external interfaces of objects• Framework elements typically

    reused by parameterizing & assembling objects

    • White-box frameworks require understanding some parts of the framework implementation• Framework elements typically

    reused by subclassing & overridding

  • 9

    Common Types of Frameworks

    • Each category of OO framework uses different sets of patterns

    • Black-box frameworks only require understanding external interfaces of objects

    • White-box frameworks require understanding some parts of the framework implementation

  • 10

    Common Types of Frameworks

    • Each category of OO framework uses different sets of patterns

    See en.wikipedia.org/wiki/Strategy_pattern& en.wikipedia.org/wiki/Decorator_pattern

    • Black-box frameworks only require understanding external interfaces of objects

    • White-box frameworks require understanding some parts of the framework implementation

    Black-box frameworks apply patterns that emphasize extensibility via object composition

    http://en.wikipedia.org/wiki/Strategy_patternhttp://en.wikipedia.org/wiki/Decorator_pattern

  • 11

    Common Types of Frameworks

    See en.wikipedia.org/wiki/Template_method& en.wikipedia.org/wiki/State_pattern

    • Each category of OO framework uses different sets of patterns

    • Black-box frameworks only require understanding external interfaces of objects

    • White-box frameworks require understanding some parts of the framework implementation

    White-box frameworks apply patterns that emphasize

    extensibility via subclassing

    http://en.wikipedia.org/wiki/Template_methodhttp://en.wikipedia.org/wiki/State_pattern

  • 12

    Common Types of Frameworks• Black-box frameworks only

    require understanding external interfaces of objects

    • White-box frameworks require understanding some parts of the framework implementation

    • Each category of OO framework uses different sets of patterns

    Android’s AsyncTask combines elements of black-box & white-box frameworks

  • 13

    White-box Elements of the AsyncTask Framework

  • 14

    : DownloadTask

    : WorkerRunnable

    : DefaultExecutor

    : ThreadedDownload

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    White-box Elements of the AsyncTask Framework • White-box framework

    elements enable long duration operations to interact with UI thread

    See en.wikipedia.org/wiki/Template_method

    BackgroundThread

    http://en.wikipedia.org/wiki/Template_method

  • 15

    : WorkerRunnable

    : DefaultExecutor

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    Template method

    • White-box framework elements enable long duration operations to interact with UI thread

    BackgroundThread

    : DownloadTask

    : ThreadedDownload

    See xp123.com/wwake/fw/ch12-bb.htm for more on framework design

    White-box Elements of the AsyncTask Framework

    http://xp123.com/wwake/fw/ch12-bb.htm

  • 16

    : WorkerRunnable

    : DefaultExecutor

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    • Framework dictatescontrol flow via hook method callbacks

    BackgroundThread

    : DownloadTask

    : ThreadedDownload

    White-box Elements of the AsyncTask Framework

  • 17

    : WorkerRunnable

    : DefaultExecutor

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    BackgroundThread

    Initialization actions

    • Framework dictatescontrol flow via hook method callbacks

    : DownloadTask

    : ThreadedDownload

    White-box Elements of the AsyncTask Framework

  • 18

    : WorkerRunnable

    : DefaultExecutor

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    Perform long-running task

    • Framework dictatescontrol flow via hook method callbacks

    BackgroundThread

    : DownloadTask

    : ThreadedDownload

    White-box Elements of the AsyncTask Framework

  • 19

    : WorkerRunnable

    : DefaultExecutor

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    Display results in UI

    • Framework dictatescontrol flow via hook method callbacks

    BackgroundThread

    : DownloadTask

    : ThreadedDownload

    White-box Elements of the AsyncTask Framework

  • 20

    : WorkerRunnable

    : DefaultExecutor

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    • Framework dictatescontrol flow via hook method callbacks

    BackgroundThread

    : DownloadTask

    : ThreadedDownload

    White-box Elements of the AsyncTask Framework

  • 21

    : WorkerRunnable

    : DefaultExecutor

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    • Framework dictatescontrol flow via hook method callbacks

    BackgroundThread

    ThisTemplate Method variant allows hook methods to run in different threads

    : DownloadTask

    : ThreadedDownload

    White-box Elements of the AsyncTask Framework

  • 22

    Black-box Elements of the AsyncTask Framework

  • 23

    • Black-box framework elements control the background thread(s)

    Black-box Elements of the AsyncTask Framework: Download

    Task: WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute()

    execute()onPreExecute() call()

    postResult()

    UI Thread

    BackgroundThreads

    See en.wikipedia.org/wiki/Strategy_pattern

    onPostExecute()

    http://en.wikipedia.org/wiki/Strategy_pattern

  • 24

    • Black-box framework elements control the background thread(s)• Default concurrency

    model has changed

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    See developer.android.com/reference/android/os/AsyncTask.html

    onPreExecute()

    onPostExecute()

    Black-box Elements of the AsyncTask Framework

    http://developer.android.com/reference/android/os/AsyncTask.html

  • 25

    • Black-box framework elements control the background thread(s)• Default concurrency

    model has changed

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute()

    execute() call()

    postResult()

    UI Thread

    BackgroundThread

    onPreExecute()

    onPostExecute()

    Black-box Elements of the AsyncTask Framework

  • 26

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute()

    execute() call()

    postResult()

    UI Thread

    BackgroundThread

    sDefaultExecutor = SERIAL_EXECUTOR

    onPreExecute()

    onPostExecute()

    See developer.android.com/reference/java/util/concurrent/Executor.html

    Black-box Elements of the AsyncTask Framework

    http://developer.android.com/reference/java/util/concurrent/Executor.html

  • 27

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute()

    execute() call()

    postResult()

    UI Thread

    BackgroundThread

    onPreExecute()

    onPostExecute()

    Black-box Elements of the AsyncTask Framework

    sDefaultExecutor = SERIAL_EXECUTOR

    SERIAL_EXECUTOR executes tasks one at a time in serial order

  • 28

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    Black-box Elements of the AsyncTask Framework

    sDefaultExecutor = SERIAL_EXECUTOR

    SERIAL_EXECUTOR executes tasks one at a time in serial order

    class SerialExecutor implements Executor {final ArrayDeque mTasks = new ArrayDeque();Runnable mActive;

    public synchronized void execute(final Runnable r) {mTasks.offer(() -> { try { r.run(); }

    finally { scheduleNext(); }}});if (mActive == null) scheduleNext();

    }

    protected synchronized void scheduleNext() {if ((mActive = mTasks.poll()) != null) THREAD_POOL_EXECUTOR.execute(mActive);

    }}

  • 29

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute()

    execute()onPreExecute() call()

    onPostExecute()postResult()

    UI Thread

    BackgroundThread

    Some apps need to run AsyncTask objects in parallel instead of serially

    sDefaultExecutor = SERIAL_EXECUTOR

    Black-box Elements of the AsyncTask Framework

  • 30

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    Allows multiple long duration tasks to run in parallel within a process

    onPreExecute()

    onPostExecute()

    executeOnExecutor(THREAD_POOL_EXECUTOR,params)

    Black-box Elements of the AsyncTask Framework

  • 31

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    onPreExecute()

    onPostExecute()

    executeOnExecutor(THREAD_POOL_EXECUTOR,params)

    Black-box Elements of the AsyncTask Framework

    THREAD_POOL_EXECUTOR can be used to execute tasks in parallel

    static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();

    static final int CORE_POOL_SIZE = CPU_COUNT + 1;static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;static final int KEEP_ALIVE = 1;

    static final BlockingQueue sPoolWorkQueue =new LinkedBlockingQueue(128);

    static Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,

    KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

  • 32

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    onPreExecute()

    onPostExecute()

    executeOnExecutor(MY_CUSTOM_EXECUTOR,params)

    See developer.android.com/reference/java/util/concurrent/ExecutorService.html

    Black-box Elements of the AsyncTask Framework

    http://developer.android.com/reference/java/util/concurrent/Executor.html

  • 33

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    onPreExecute()

    onPostExecute()

    executeOnExecutor(executorStrategy,params)

    Black-box Elements of the AsyncTask Framework

  • 34

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    SERIAL_EXECUTOR, THREAD_POOL_EXECUTOR,

    or custom Executor strategy

    onPreExecute()

    onPostExecute()

    executeOnExecutor(executorStrategy,params)

    Black-box Elements of the AsyncTask Framework

  • 35

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    SERIAL_EXECUTOR, THREAD_POOL_EXECUTOR,

    or custom Executor strategy

    onPreExecute()

    onPostExecute()

    executeOnExecutor(executorStrategy,params)

    Black-box Elements of the AsyncTask Framework

  • 36

    • AsyncTask can be configured via a # of Executor strategies

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    onPreExecute()

    onPostExecute()

    executeOnExecutor(executorStrategy,params)

    Executor treated as a black-box

    Black-box Elements of the AsyncTask Framework

  • 37

    • AsyncTask ensures certain operations are safe without explicit synchronizations

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    onPreExecute()

    onPostExecute()

    executeOnExecutor(executorStrategy,params)

    Black-box Elements of the AsyncTask Framework

  • 38

    • AsyncTask ensures certain operations are safe without explicit synchronizations, e.g.• Set fields in the

    AsyncTask ctoror onPreExecute(), & refer to them in doInBackground()

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    onPostExecute()

    onPreExecute()

    executeOnExecutor(executorStrategy,params)

    Black-box Elements of the AsyncTask Framework

  • 39

    • AsyncTask ensures certain operations are safe without explicit synchronizations, e.g.• Set fields in the

    AsyncTask ctoror onPreExecute(), & refer to them in doInBackground()

    • Set member fields in doInBackground()& refer to them inonProgressUpdate()& onPostExecute()

    : DownloadTask

    : WorkerRunnable

    : Executor: ThreadedDownload

    doInBackground()

    execute() call()

    postResult()

    UI Thread

    BackgroundThreads

    onPreExecute()

    onPostExecute()

    executeOnExecutor(executorStrategy,params)

    Black-box Elements of the AsyncTask Framework

  • 40

    End of Types of Frameworks in AsyncTask (Part 2)

    Slide Number 1Learning Objectives in this Part of the LessonLearning Objectives in this Part of the LessonSlide Number 4Common Types of FrameworksCommon Types of FrameworksCommon Types of FrameworksCommon Types of FrameworksCommon Types of FrameworksCommon Types of FrameworksCommon Types of FrameworksCommon Types of FrameworksSlide Number 13Slide Number 14Slide Number 15Slide Number 16Slide Number 17Slide Number 18Slide Number 19Slide Number 20Slide Number 21Slide Number 22Slide Number 23Slide Number 24Slide Number 25Slide Number 26Slide Number 27Slide Number 28Slide Number 29Slide Number 30Slide Number 31Slide Number 32Slide Number 33Slide Number 34Slide Number 35Slide Number 36Slide Number 37Slide Number 38Slide Number 39End of Types of Frameworks in AsyncTask (Part 2)