1
Concurrency 3 – Concurrent Execution
Alexandre David
adavid@cs.aau.dk Credits for the slides: Claus Brabrand Jeff Magee & Jeff Kramer
Concurrency 3 Concurrent Execution Alexandre David - - PowerPoint PPT Presentation
Concurrency 3 Concurrent Execution Alexandre David adavid@cs.aau.dk Credits for the slides: Claus Brabrand Jeff Magee & Jeff Kramer 1 Repetition Concepts: We adopt a model-based approach for the design and construction of
1
adavid@cs.aau.dk Credits for the slides: Claus Brabrand Jeff Magee & Jeff Kramer
2
➢ Concepts: We adopt a model-based approach
➢ Models: We use finite state models to
➢ Practice: We use Java for constructing
3
➢ Based on Labelled Transition Systems (LTS) ➢ Described textually as Finite State Processes
speed engineOn engineOff
4
➢ Finite State Processes (FSP):
5
➢ Subclassing java.lang.Thread: ➢ Implementing java.lang.Runnable:
class MyThread extends Thread { public void run() { // ... } } class MyRun implements Runnable { public void run() { // ... } } Thread t = new MyThread(); t.start(); // ... Thread t = new Thread(new MyRun()); t.start(); // ...
6
7
➔ Parallelism (aka. “Real” Concurrent Execution)
Time
8
Time
➔ Concurrency (aka. Pseudo-Concurrent Execution)
9
Time
Time
10
➢ How do we model concurrency? ➢ Arbitrary relative order of actions from
x
y
11
➢ How should we model process execution speed? ➢ We abstract away time: arbitrary speed! a b x y
12
13
2 states 3 states (0,0) (0,1) (0,2) (1,2) (1,1) (1,0)
2 x 3 states
14
15
16
3 states 3 states 3 x 3 states?
17
3 states 3 states 3 x 3 states?
18
3 states 3 states
4 states!
19
20
21
22
23
24
r e a d y use
25
r e a d y use
26
27
28
29
30
31
32
33
34
a:USER a.acquire a.use
a.release
b:USER
b.acquire b.use b.release
b.acquire a.acquire b.release a.release
{a,b}::RESOURCE
b.acquire b.use b.release
a.use a.acquire a.release RESOURCE_SHARE
35
36
call wait reply continue
request call service reply
call service reply
continue
37
38
acquire tau release
USER
39
acquire tau release
USER
40
acquire tau release
USER
41
acquire tau release
USER
acquire release
USER
42
T
a b
43
T
a b
P
a b
Q
m
c d c x x x
44
T
a b
P
a b
Q
m
A B
a c d c x x x
S
y x
45
a:BUFF b:BUFF
a.out
TWOBUFF
in in
in
46
CLIENTv2
call accept
SERVERv2
call service continue
CLIENT
call request
SERVER
call reply wait reply service continue
47 a:USER
printer
b:USER
printer
printer: RESOURCE
acquire release
RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use->printer.release->USER). ||PRINTER_SHARE = (a:USER || b:USER || {a,b}::printer:RESOURCE).
48
THREAD = OFF, OFF = (toggle->ON |abort->STOP), ON = (toggle->OFF |output->ON |abort->STOP). ||THREAD_DEMO = (a:THREAD || b:THREAD) /{stop/{a,b}.abort}. Interpret:
as inputs
as output a:T b:T
stop a.toggle a.output b.output b.toggle
THREAD_DEMO
49
class MyThread extends Thread { private boolean on; MyThread() {
} void toggle() { on = !on; } void abort() { interrupt(); } private void output() { System.out.println(“output”); } public void run() { try { while (true) { if (on) output(); sleep(1000); } } catch(InterruptedException _) { System.out.println(“Done!”); } } }
50
class MyThread extends Thread { private boolean on; MyThread() {
} void toggle() { on = !on; } void abort() { interrupt(); } private void output() { System.out.println(“output”); } public void run() { try { while (true) { if (on) output(); sleep(1000); } } catch(InterruptedException _) { System.out.println(“Done!”); } } }
Interpret:
as inputs
as output
51
class ThreadDemo { private stop(MyThread a, MyThread b) { a.abort(); b.abort(); } public static void main(String[] args) { MyThread a = new MyThread(); MyThread b = new MyThread(); a.start(); b.start(); while (true) { switch (readChar()) { case ‘a’: a.toogle(); break; case ‘b’: b.toogle(); break; case ‘i’: stop(a,b); return; } } } }
52
➢ Concepts: concurrent processes and process
➢ Models:
– asynchronous (arbitrary speed) & interleaving (arbitrary
– parallel composition (finite state process with action
– process interaction (shared actions) – process labeling, action relabeling, and hiding – structure diagrams
➢ Practice: multiple threads in Java.