Professional Documents
Culture Documents
ch3 PDF
ch3 PDF
Concurrent Execution
1
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
Concurrent execution
2
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
Definitions
u Concurrency
l Logically simultaneous processing. A
Does not imply multiple processing
B
elements (PEs). Requires
interleaved execution on a single PE. C
u Parallelism
Time
l Physically simultaneous processing.
Involves multiple PEs and/or
independent device operations.
3
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
3.1 Modeling Concurrency
ITCH = (scratch->STOP).
CONVERSE = (think->talk->STOP).
Disjoint
alphabets
||CONVERSE_ITCH = (ITCH || CONVERSE).
scratch
Multi-party synchronization:
MAKE_A = (makeA->ready->used->MAKE_A).
MAKE_B = (makeB->ready->used->MAKE_B).
ASSEMBLE = (ready->assemble->used->ASSEMBLE).
||FACTORY = (MAKE_A || MAKE_B || ASSEMBLE).
makeA
0 1 2 3 4 5
makeB 10
2015 Concurrency: concurrent execution used ©Magee/Kramer 2nd Edition
composite processes
12
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
Example: an abstract roller coaster model
const M = 3
//turnstile simulates passenger arrival
TURNSTILE = ( … -> TURNSTILE).
//control counts passengers and signals when full
CONTROL = CONTROL[0],
CONTROL[i:0..M]=( … -> CONTROL[i+1]
| … -> CONTROL[0]
).
//car departs when signalled
CAR = ( … -> CAR).
||ROLLERCOASTER = ( … ).
13
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
process instances and labelling
CLIENT = (call->wait->continue->CLIENT).
SERVER = (request->service->reply->SERVER).
continue reply
continue
16
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
process labelling by a set of prefix labels
17
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
process prefix labels for shared resources
a.acquire a.use b.acquire b.use
a:USER b:USER
0 1 2 0 1 2
a.release b.release
b.acquire
How does the model ensure a.acquire
SERVERv2 = (accept.request
->service->accept.reply->SERVERv2).
CLIENTv2 = (call.request
->call.reply->continue->CLIENTv2).
19
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
action hiding - abstraction to reduce complexity
USER = (acquire->use->release->USER)
@{acquire,release}.
acquire tau
Minimization removes hidden
tau actions to produce an LTS
0 1 2 with equivalent observable
behaviour.
acquire
release
0 1
release
21
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
structure diagrams – systems as interacting processes
P a
Process P with
b
alphabet {a,b}.
m
P a b Q Parallel Composition
c d
c
x x x
(P||Q) / {m/a,m/b,c/d}
S Composite process
x P a Q y ||S = (P||Q) @ {x,y}
22
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
structure diagrams
range T = 0..3
BUFF = (in[i:T]->out[i]->BUFF).
||TWOBUF = ?
TWOBUFF
a:BUFF b:BUFF
in a.out out
in out in out
23
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
structure diagrams
24
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
structure diagrams - resource sharing
PRINTER_SHARE
a:USER
printer
printer:
RESOURCE
acquire
b:USER release
printer
RESOURCE = (acquire->release->RESOURCE).
USER = (printer.acquire->use
->printer.release->USER)\{use}.
||PRINTER_SHARE
= (a:USER||b:USER||{a,b}::printer:RESOURCE).
25
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
3.2 Multi-threaded Programs in Java
26
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
ThreadDemo model
THREAD_DEMO
a.run b.run
a.pause b.pause
a:ROTATOR b:ROTATOR
a.rotate stop b.rotate
Applet Panel
GraphicCanvas
A,B display
ThreadDemo ThreadPanel
run() 28
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
Rotator class
29
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
ThreadPanel class ThreadPanel
manages the display
public class ThreadPanel extends Panel { and control buttons for
a thread.
// construct display with title and segment color c
public ThreadPanel(String title, Color c) {…}
// rotate display of currently running thread 6 degrees
// return value not used in this example Calls to rotate()
public static boolean rotate() are delegated to
throws InterruptedException {…} DisplayThread.
// create a new thread with target r and start it running
public void start(Runnable r) {
thread = new DisplayThread(canvas,r,…);
thread.start();
} Threads are created
and started by the
// stop the thread using Thread.interrupt() start() method, and
public void stop() {thread.interrupt();} terminated by the
} stop() method. 30
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
ThreadDemo class
public class ThreadDemo extends Applet {
ThreadPanel A; ThreadPanel B;
public void init() {
A = new ThreadPanel("Thread A",Color.blue);
B = new ThreadPanel("Thread B",Color.blue);
add(A); add(B);
} ThreadDemo creates two
ThreadPanel displays
public void start() { when initialized and two
A.start(new Rotator()); threads when started.
B.start(new Rotator());
}
public void stop() { ThreadPanel is used
A.stop(); extensively in later
B.stop(); demonstration programs.
}
31
}
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition
3.3 Java Concurrency Utilities Package
u Models
l Asynchronous (arbitrary speed) & interleaving (arbitrary order).
l Parallel composition as a finite state process with action
interleaving.
l Process interaction by shared actions.
l Process labeling and action relabeling and hiding.
l Structure diagrams
u Practice
l Multiple threads in Java.
33
2015 Concurrency: concurrent execution ©Magee/Kramer 2nd Edition