0% found this document useful (0 votes)
59 views62 pages

Concurrency in OOPS

The document discusses the Active Object pattern and how it is applied in Android. The Active Object pattern decouples method invocation on an object from method execution to address issues that arise from invoking methods in different threads. It allows method invocation to occur in the client's thread while execution occurs in a separate thread, making parallelism and synchronized access easier for programmers. In Android, this is implemented using Handlers and Messages to allow asynchronous communication between threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views62 pages

Concurrency in OOPS

The document discusses the Active Object pattern and how it is applied in Android. The Active Object pattern decouples method invocation on an object from method execution to address issues that arise from invoking methods in different threads. It allows method invocation to occur in the client's thread while execution occurs in a separate thread, making parallelism and synchronized access easier for programmers. In Android, this is implemented using Handlers and Messages to allow asynchronous communication between threads.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Android Concurrency:

The Active Object Pattern

Douglas C. Schmidt
d.schmidt@vanderbilt.edu
www.dre.vanderbilt.edu/~schmidt

Institute for Software


Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA

CS 282 Principles of Operating Systems II


Systems Programming for Android
Android Concurrency Douglas C. Schmidt

Learning Objectives in this Part of the Module


• Understand the Active Object pattern

Handler
implementation
Handler
method_2
interface
method_1

2
Android Concurrency Douglas C. Schmidt

Learning Objectives in this Part of the Module


• Understand the Active Object pattern & how it’s applied in Android

UI Thread
sendMessage

Handler
… implementation
Handler
Background thread interface …
handleMessage

3
Android Concurrency Douglas C. Schmidt

Challenge: Invoking Methods in Another Thread


Context Background
Thread
• Android clients that access
objects running in separate
Handler.sendMessage(msg);
threads of control Message

• A “client” is any Android


Queue

code that invokes a object’s Handler


method, e.g.,
• A background Thread
void handleMessage(Message msg) {
switch (msg.what) {
invoking sendMessage() Looper
Message case SET_PROGRESS_BAR_VISIBILITY:
on a Handler associated Message
{
progress.setVisibility
with the UI Thread Message ((Integer) msg.obj);
break;
Message }
...
Message Message

UI Thread
(main thread)

4
Android Concurrency Douglas C. Schmidt

Challenge: Invoking Methods in Another Thread


Context Thread1
• Android clients that access
objects running in separate
Handler.sendMessage(msg);
threads of control Message

• A “client” is any Android


Queue

code that invokes a object’s Handler


method, e.g.,
• A background Thread
invoking sendMessage() Looper
Message void handleMessage(Message msg)
on a Handler associated Message
{
...
with the UI Thread Message }

• More generally, any Message

Threads that interact Message Message


via Handlers/Messages
Thread2

5
Android Concurrency Douglas C. Schmidt

Challenge: Invoking Methods in Another Thread


Problems Thread1
• Leveraging the parallelism
available on a
Handler.sendMessage(msg);
hardware/software Message
platform (relatively) Queue

transparently Handler

Message
Looper
void handleMessage(Message msg)
Message
{
...
Message }

Message

Message Message

Thread2

6
Android Concurrency Douglas C. Schmidt

Challenge: Invoking Methods in Another Thread


Problems Thread1
• Leveraging the parallelism
available on a
Handler.sendMessage(msg);
hardware/software Message
platform (relatively) Queue

transparently Handler
• Ensuring that processing-
intensive methods
invoked on an object Message
Looper
void handleMessage(Message msg)
concurrently do not block Message
{
the entire process Message }
...

Message

Message Message

Thread2

7
Android Concurrency Douglas C. Schmidt

Challenge: Invoking Methods in Another Thread


Problems Thread1
• Leveraging the parallelism
available on a
Handler.sendMessage(msg);
hardware/software Message
platform (relatively) Queue

transparently Handler
• Ensuring that processing-
intensive methods
invoked on an object Message
Looper
void handleMessage(Message msg)
concurrently do not block Message
{
the entire process Message }
...

• Making synchronized
Message
access to shared objects
Message
easy & intuitive to
Message

program
Thread2

8
Android Concurrency Douglas C. Schmidt

Challenge: Invoking Methods in Another Thread


Solution
• Apply the Active Object pattern to decouple method invocation on the object
from method execution
• Method invocation should occur in the client’s thread of control, whereas
method execution should occur in a separate thread

UI Thread
sendMessage

Handler
… implementation
Handler
Background thread interface …
handleMessage

9
Android Concurrency Douglas C. Schmidt

Challenge: Invoking Methods in Another Thread


Solution
• Apply the Active Object pattern to decouple method invocation on the object
from method execution
• Method invocation should occur in the client’s thread of control, whereas
method execution should occur in a separate thread
• The client should appear to invoke an ordinary method
• i.e., the client shouldn’t manipulate synchronization mechanisms explicitly

UI Thread
sendMessage

Handler
… implementation
Handler
Background thread interface …
handleMessage

10
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Intent
• Define service requests on components as the units of concurrency & run
service requests on a component in different thread(s) from the requesting
client thread
• Enable the client & component to interact asynchronously to produce &
consume service results

Handler
implementation
Handler
method_2
interface
method_1

www.dre.vanderbilt.edu/~schmidt/PDF/Act-Obj.pdf
11 has more info
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applicability
• When an object’s interface methods should define its concurrency boundaries

Handler
implementation
Handler
method_2
interface
method_1

12
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applicability
• When an object’s interface methods should define its concurrency boundaries
• When objects should be responsible for method synchronization & scheduling
transparently, without requiring explicit client intervention

Handler
implementation
Handler
method_2
interface
method_1

13
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applicability
• When an object’s interface methods should define its concurrency boundaries
• When objects should be responsible for method synchronization & scheduling
transparently, without requiring explicit client intervention
• When an object’s methods may block during their execution

Handler
implementation
Handler
method_2
interface
method_1

14
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applicability
• When an object’s interface methods should define its concurrency boundaries
• When objects should be responsible for method synchronization & scheduling
transparently, without requiring explicit client intervention
• When an object’s methods may block during their execution
• When multiple client method requests can run concurrently on an object

Handler
implementation
Handler
method_2
interface
method_1

15
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applicability
• When an object’s interface methods should define its concurrency boundaries
• When objects should be responsible for method synchronization & scheduling
transparently, without requiring explicit client intervention
• When an object’s methods may block during their execution
• When multiple client method requests can run concurrently on an object
• When method invocation order might not match method execution order

Handler
implementation
Handler
method_2
interface
method_1

16
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applicability
• When an object’s interface methods should define its concurrency boundaries
• When objects should be responsible for method synchronization & scheduling
transparently, without requiring explicit client intervention
• When an object’s methods may block during their execution
• When multiple client method requests can run concurrently on an object
• When method invocation order might not match method execution order

Handler
implementation
Handler
method_2
interface
method_1

Note the similarities between Active Object


17 & Monitor Object wrt applicability
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Structure & Participants
Handler.sendMessage()

18
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Structure & Participants

Handler

19
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Structure & Participants

Message

20
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Structure & Participants MessageQueue

21
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Structure & Participants Looper

22
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Structure & Participants

MyHandler

23
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Structure & Participants

MyActivity

24
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Dynamics Client invokes a method call on a proxy

25
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Dynamics Proxy converts method call into method request,
passes to scheduler, & returns future to client

26
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Dynamics Scheduler enqueues method request

27
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Dynamics Scheduler dequeues method request at some point
& runs it on the servant in a separate thread

28
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Dynamics Clients can obtain result from futures via polling, or callbacks

29
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation class Handler {
• Implement the invocation boolean sendMessage (Message msg) {
return sendMessageDelayed(msg, 0);
infrastructure
}
• Implement the proxy
• Creates a concrete method boolean sendMessageDelayed
request for each method (Message msg, long delayMillis){
return sendMessageAtTime(msg,
invocation by a client
SystemClock.uptimeMillis() +
delayMillis);
}

boolean sendMessageAtTime
(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
queue.enqueueMessage
(msg, uptimeMillis);
...

30 has more on the Proxy pattern


en.wikipedia.org/wiki/Proxy_pattern
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation class Handler {
• Implement the invocation boolean sendMessage (Message msg) {
return sendMessageDelayed(msg, 0);
infrastructure
}
• Implement the proxy
• Creates a concrete method boolean sendMessageDelayed
request for each method (Message msg, long delayMillis){
return sendMessageAtTime(msg,
invocation by a client
SystemClock.uptimeMillis() +
delayMillis);
}

boolean sendMessageAtTime
(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
queue.enqueueMessage
(msg, uptimeMillis);
...

frameworks/base/core/java/android/os/Handler.java
31 has the source code
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation class Handler {
• Implement the invocation boolean sendMessage (Message msg) {
return sendMessageDelayed(msg, 0);
infrastructure
}
• Implement the proxy
• Implement the method boolean sendMessageDelayed
requests (Message msg, long delayMillis){
return sendMessageAtTime(msg,
• Method requests can be SystemClock.uptimeMillis() +
considered as command delayMillis);
objects }

boolean sendMessageAtTime
(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
queue.enqueueMessage
(msg, uptimeMillis);
...

Android Handler proxy & method requests


32 are simpler than POSA active objects
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation public class MessageQueue {
• Implement the invocation ...
final boolean enqueueMessage
infrastructure
(Message msg, long when) {
• Implement the activation list ...
• Used to insert & remove a }
method request
final Message next() {
• This list can be implemented ...
as a synchronized bounded }
buffer shared between the
client threads & the thread
in which the active object’s
scheduler & servant run

frameworks/base/core/java/android/os/MessageQueue.java
33 has source code
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation public class Looper {
• Implement the invocation ...
final MessageQueue mQueue;
infrastructure
• Implement the activation list public static void loop() {
• Implement the scheduler ...

• A scheduler is a command for (;;) {


processor that manages the Message msg =
activation list & executes queue.next();
pending method requests ...
whose synchronization
constraints have been met msg.target.
dispatchMessage(msg);
...
}
...

wiki.hsr.ch/APF/files/CommandProcessor.pdf
34 has more on Command Processor
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation public class Looper {
• Implement the invocation ...
final MessageQueue mQueue;
infrastructure
• Implement the activation list public static void loop() {
• Implement the scheduler ...

• A scheduler is a command for (;;) {


processor that manages the Message msg =
activation list & executes queue.next();
pending method requests ...
whose synchronization
constraints have been met msg.target.
dispatchMessage(msg);
...
}
...

frameworks/base/core/java/android/os/Looper.java
35 has source code
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation class WorkerHandler extends Handler {
• Implement the invocation public void handleMessage
infrastructure (Message msg) {
switch (msg.what) {
• Implement the activation list case SET_PROGRESS_BAR_VISIBILITY:
• Implement the scheduler {
mAct.progress.setVisibility
• Implement the servant
((Integer) msg.obj);
• A servant defines the behavior break;
& state being modeled as an }
active object case PROGRESS_UPDATE:
{
mAct.progress.setProgress
((Integer) msg.obj);
break;
}
...

36
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Implementation class WorkerHandler extends Handler {
• Implement the invocation public void handleMessage
infrastructure (Message msg) {
...
• Implement the activation list WorkerArgs args =
• Implement the scheduler (WorkerArgs) msg.obj;
• Implement the servant
Message reply =
• Determine rendezvous & return args.handler.obtainMessage();
value policy reply.obj = args;
• The rendezvous policy reply.arg1 = msg.arg1;
determines how clients obtain
reply.sendToTarget();
return values from methods ...
invoked on active objects

A common idiom is to pass the original Handler via a Message to a Worker


Thread, which can then pass a response back to the original Handler

developer.android.com/reference/android/os/Message.html#sendToTarget()
37
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android
• AsyncQueryHandler is a helper class
that invokes ContentResolver
calls asynchronously to avoid
blocking the UI Thread
Synchronous Query
• ContentResolver provides
apps access to an underlying
ContentProvider
: Content
: Activity Resolver

query()

See developer.android.com/reference/android/content/ContentResolver.html
38
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android
• AsyncQueryHandler is a helper class
that invokes ContentResolver
calls asynchronously to avoid
blocking the UI Thread
Synchronous Query
• ContentResolver provides
apps access to an underlying
ContentProvider
: Content
: Activity Resolver

query() Run query on


Block Activity thread ContentProvider
until the query is done & return result

See developer.android.com/reference/android/content/ContentResolver.html
39
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android
• AsyncQueryHandler is a helper class
that invokes ContentResolver
calls asynchronously to avoid
Asynchronous Query
blocking the UI Thread

: MyAsync
: Activity QueryHandler

startQuery()

Caller returns & the call


runs asynchronously

See developer.android.com/reference/android/content/AsyncQueryHandler.html
40
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android
• AsyncQueryHandler is a helper class
that invokes ContentResolver
calls asynchronously to avoid
Asynchronous Query
blocking the UI Thread

: MyAsync : Worker
: Activity QueryHandler Handler

startQuery() handleMessage()
sendMessage()

Caller returns & the call


runs asynchronously

See developer.android.com/reference/android/content/AsyncQueryHandler.html
41
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android
• AsyncQueryHandler is a helper class
that invokes ContentResolver
calls asynchronously to avoid
Asynchronous Query
blocking the UI Thread

: MyAsync : Worker : Content


: Activity QueryHandler Handler Resolver

startQuery() handleMessage()
sendMessage() Run query on
query() ContentProvider
& return result

Block worker thread


until the query is done

See developer.android.com/reference/android/content/AsyncQueryHandler.html
42
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android
• AsyncQueryHandler is a helper class
that invokes ContentResolver
calls asynchronously to avoid
Asynchronous Query
blocking the UI Thread

: MyAsync : Worker : Content


: Activity QueryHandler Handler Resolver

startQuery() handleMessage()
sendMessage() Run query on
query() ContentProvider
& return result
sendToTarget()
Caller returns & the call
runs asynchronously

See developer.android.com/reference/android/content/AsyncQueryHandler.html
43
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android
• AsyncQueryHandler is a helper class
that invokes ContentResolver
calls asynchronously to avoid
Asynchronous Query
blocking the UI Thread

: MyAsync : Worker : Content


: Activity QueryHandler Handler Resolver

startQuery() handleMessage()
sendMessage() Run query on
query() ContentProvider
& return result
handleMessage() sendToTarget()
onQueryComplete() Handle completion of the
asynchronously invoked query

See developer.android.com/reference/android/content/AsyncQueryHandler.html
44
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Applying Active Object in Android Activity
Thread
• AsyncQueryHandler is a helper class
that invokes ContentResolver 1. startQuery()
2. wWorkerHandlerThread.
calls asynchronously to avoid Message sendMessage(msg);
blocking the UI Thread Queue

• Internally, AsyncQueryHandler Worker


Handler
uses a (subset of the) Active
Object pattern 4. void handleMessage
Message (Message msg) {
Looper
3. void loop() { switch (event) {
Message
... case EVENT_ARG_QUERY:
for (;;) { Message Cursor cursor =
Message msg = resolver.query(....);
queue.next(); Message ...
...
Message Message
msg.target.
dispatchMessage(msg); Worker
... Thread

frameworks/base/core/java/android/content/AsyncQueryHandler.java
45 has code
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Thread1
+ Enhances concurrency & simplifies
synchronized complexity
Handler.sendMessage(msg);
• Client threads & asynchronous Message
method executions can run Queue

concurrently Handler

Message

Looper
Message void handleMessage(Message msg)
{
Message ...
}
Message

Message Message

Thread2

46
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Thread1
+ Enhances concurrency & simplifies
synchronized complexity
Handler.sendMessage(msg);
• Client threads & asynchronous Message
method executions can run Queue

concurrently Handler

• A scheduler can evaluate


synchronization constraints to
serialize access to servants Message

Looper
Message void handleMessage(Message msg)
{
Message ...
}
Message

Message Message

Thread2

47
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Thread1
+ Enhances concurrency & simplifies
synchronized complexity
Handler.sendMessage(msg);
+ Transparent leveraging of available Message
parallelism Queue

• Multiple active object methods Handler

can execute in parallel if the


scheduler is configured using
a thread pool & supported by Message

Looper
void handleMessage(Message msg)
the OS/hardware Message
{
...
Message }
Message
Message
Message
Message Message
Thread2
Thread2
Thread2

48
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Thread1
+ Enhances concurrency & simplifies
synchronized complexity
Handler.sendMessageDelayed
+ Transparent leveraging of available Message (msg, delayTime);
parallelism Queue

+ Method execution order can differ Handler

from method invocation order


• Methods that are invoked
asynchronously can execute Message

Looper
according to synchronization Message void handleMessage(Message msg)
{
constraints defined by guards Message ...
& scheduling policies Message
}

Message Message

Thread2

49
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Background
Thread
− Runtime overhead 1. Message msg =
Handler.obtainMessage
• Stemming from (SET_PROGRESS_BAR_VISIBILITY,
Message ProgressBar.VISIBLE);
Queue
2. Handler.sendMessage
Handler (msg);

4. void handleMessage(Message msg) {


Message switch (msg.what) {
Looper

case SET_PROGRESS_BAR_VISIBILITY:
Message {
3. void loop() { progress.setVisibility
Message
... ((Integer) msg.obj);
for (;;) { Message break;
Message msg = queue.next(); }
Message ... Message
...
msg.target.
dispatchMessage(msg); UI Thread
... (main thread)

50
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Background
Thread
− Runtime overhead 1. Message msg =
Handler.obtainMessage
• Stemming from (SET_PROGRESS_BAR_VISIBILITY,
• Dynamic memory Message ProgressBar.VISIBLE);
Queue
(de)allocation 2. Handler.sendMessage
(msg);
Handler

4. void handleMessage(Message msg) {


Message switch (msg.what) {
Looper

case SET_PROGRESS_BAR_VISIBILITY:
Message {
3. void loop() { progress.setVisibility
Message
... ((Integer) msg.obj);
for (;;) { Message break;
Message msg = queue.next(); }
Message ... Message
...
msg.target.
dispatchMessage(msg); UI Thread
... (main thread)

51
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Background
Thread
− Runtime overhead 1. Message msg =
Handler.obtainMessage
• Stemming from (SET_PROGRESS_BAR_VISIBILITY,
• Dynamic memory Message ProgressBar.VISIBLE);
Queue
(de)allocation 2. Handler.sendMessage
(msg);
Handler
• Synchronization
operations
4. void handleMessage(Message msg) {
Message switch (msg.what) {
Looper

case SET_PROGRESS_BAR_VISIBILITY:
Message {
3. void loop() { progress.setVisibility
Message
... ((Integer) msg.obj);
for (;;) { Message break;
Message msg = queue.next(); }
Message ... Message
...
msg.target.
dispatchMessage(msg); UI Thread
... (main thread)

52
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Background
Thread
− Runtime overhead 1. Message msg =
Handler.obtainMessage
• Stemming from (SET_PROGRESS_BAR_VISIBILITY,
• Dynamic memory Message ProgressBar.VISIBLE);
Queue
(de)allocation 2. Handler.sendMessage
(msg);
Handler
• Synchronization
operations
4. void handleMessage(Message msg) {
• Context switches Message switch (msg.what) {
Looper

case SET_PROGRESS_BAR_VISIBILITY:
Message {
3. void loop() { progress.setVisibility
Message
... ((Integer) msg.obj);
for (;;) { Message break;
Message msg = queue.next(); }
Message ... Message
...
msg.target.
dispatchMessage(msg); UI Thread
... (main thread)

53
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences Background
Thread
− Runtime overhead 1. Message msg =
Handler.obtainMessage
• Stemming from (SET_PROGRESS_BAR_VISIBILITY,
• Dynamic memory Message ProgressBar.VISIBLE);
Queue
(de)allocation 2. Handler.sendMessage
(msg);
Handler
• Synchronization
operations
4. void handleMessage(Message msg) {
• Context switches Message switch (msg.what) {
Looper

• CPU cache updates


case SET_PROGRESS_BAR_VISIBILITY:
Message {
3. void loop() { progress.setVisibility
Message
... ((Integer) msg.obj);
for (;;) { Message break;
Message msg = queue.next(); }
Message ... Message
...
msg.target.
dispatchMessage(msg); UI Thread
... (main thread)

54
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Consequences
− Higher overhead
− Complicated debugging
• It is harder to debug programs
that use concurrency due to
non-determinism of the various
schedulers

Subsets of Active Object are often 55


used to workaround these limitations
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Known Uses
• Programming languages based on the Actor model
• A mathematical model of concurrent
computation that treats "actors" as
the universal primitives of concurrent
digital computation

56
en.wikipedia.org/wiki/Actor_model has more info
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Known Uses
• Programming languages based on the Actor model
• A mathematical model of concurrent
computation that treats "actors" as
the universal primitives of concurrent
digital computation
• In response to a message that it
receives, an actor can make local
decisions, create more actors, send
more messages, & determine how
to respond to the next message
received

57
en.wikipedia.org/wiki/Actor_model has more info
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Known Uses
• Programming languages based on the Actor model
• Active Object in C++11
class Active {
public:
typedef function<void()> Message;

Active(): done(false)
{ th = unique_ptr<thread>(new thread([=]{ this->run(); })); }
~Active() { send([&]{done = true;}); th->join(); }

void send(Message m) { mq.send(m); }

private:
message_queue<Message> mq; bool done; unique_ptr<thread> th;
void run(){ while(!done){ Message msg = mq.receive(); msg();}}
};

www.drdobbs.com/parallel/prefer-using-active-objects-instead-of-n/225700095
58
Android Concurrency Douglas C. Schmidt

Active Object POSA2 Concurrency


Known Uses
• Programming languages based on the Actor model
• Active Object in C++11
• The ACE Task framework

See www.dre.vanderbilt.edu/~schmidt/PDF/ACE-concurrency.pdf
59 for more info
Android Concurrency Douglas C. Schmidt

Summary

Handler
implementation
Handler
method_2
interface
method_1

• Clients may need to issue requests on components without blocking until


the requests execute
• It should also be possible to schedule the execution of client requests
according to certain criteria
• e.g., request priorities or deadlines

60
Android Concurrency Douglas C. Schmidt

Summary

Handler
implementation
Handler
method_2
interface
method_1

• Clients may need to issue requests on components without blocking until


the requests execute
• The Active Object pattern helps keep service requests independent so they
can be serialized & scheduled transparently to the component & its clients

See www.dre.vanderbilt.edu/~schmidt/PDF/Act-Obj.pdf
61 for Active Object
Android Concurrency Douglas C. Schmidt

Summary

Handler
implementation
Handler
method_2
interface
method_1

• Clients may need to issue requests on components without blocking until


the requests execute
• The Active Object pattern helps keep service requests independent so they
can be serialized & scheduled transparently to the component & its clients
• It’s instructive to compare Active Object with Monitor Object
• Active Object is more powerful, but also more complicated (& potentially
more overhead)

See www.dre.vanderbilt.edu/~schmidt/PDF/monitor.pdf
62 for comparisons

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy