Documente Academic
Documente Profesional
Documente Cultură
a light switch
0 1 LTS
off
a sequence of
onÆoffÆonÆoffÆonÆoffÆ ………. actions or trace
SWITCH = OFF,
OFF = (on -> ON), 0 1
ON = (off-> OFF).
off
0 1
off
Concurrency: processes & threads 8
©Magee/Kramer 2nd Edition
FSP - action prefix
0 1 2 3
Trace: orange
redÆorangeÆgreenÆorangeÆredÆorangeÆgreen …
Concurrency: processes & threads 9
©Magee/Kramer 2nd Edition
FSP - choice
0 1 2
tea
Concurrency: processes & threads 11
©Magee/Kramer 2nd Edition
Non-deterministic choice
COIN = (toss->HEADS|toss->TAILS),
HEADS= (heads->COIN), toss
TAILS= (tails->COIN).
toss
Tossing a
coin. 0 1 2
Possible traces?
heads
Use non-determinism... in
0 1
in
CHAN = (in->CHAN
|in->out->CHAN out
).
index expressions to 0 1 2 3
model calculation:
out.0
const N = 1
range T = 0..N out.1
range R = 0..2*N
out.2
SUM = (in[a:T][b:T]->TOTAL[a+b]),
TOTAL[s:R] = (out[s]->SUM).
Concurrency: processes & threads 15
©Magee/Kramer 2nd Edition
FSP - guarded actions
0 1 2 3
dec dec dec
Concurrency: processes & threads 16
©Magee/Kramer 2nd Edition
FSP - guarded actions
A countdown timer which beeps after N ticks, or can be
stopped.
COUNTDOWN (N=3) = (start->COUNTDOWN[N]),
COUNTDOWN[i:0..N] =
(when(i>0) tick->COUNTDOWN[i-1]
|when(i==0)beep->STOP
stop
|stop->STOP
). stop
stop
stop
start tick tick tick beep
0 1 2 3 4 5
Concurrency: processes & threads 17
©Magee/Kramer 2nd Edition
FSP - guarded actions
const False = 0
P = (when (False) doanything->P).
Answer:
STOP
WRITER = (write[1]->write[3]->WRITER)
+{write[0..3]}.
Alphabet of WRITER is the set {write[0..3]}
(we make use of alphabet extensions in later chapters)
Modeling processes as
finite state machines
using FSP/LTS.
Implementing threads
in Java.
O S Proc ess
stop(), or
st run() returns
op
()
Terminated
The predicate isAlive() can be
used to test if a thread has been started but
not terminated. Once terminated, it cannot
be restarted (cf. mortals).
Concurrency: processes & threads 26
©Magee/Kramer 2nd Edition
thread alive states in Java
Alive
Running sl
su ee
sp p(
en )
d(
)
yield() dispatch
suspend()
start()
Runnable Non-Runnable
resume()
stop(), or
Also, wait() makes a Thread Non-Runnable, run() returns
and notify() makes it Runnable
Concurrency: processes
(used in later & threads
chapters). 27
©Magee/Kramer 2nd Edition
Java thread lifecycle - an FSP specification
THREAD = CREATED,
CREATED = (start ->RUNNABLE
|stop ->TERMINATED),
RUNNING = ({suspend,sleep}->NON_RUNNABLE
|yield ->RUNNABLE
|{stop,end} ->TERMINATED
|run ->RUNNING),
RUNNABLE = (suspend ->NON_RUNNABLE
|dispatch ->RUNNING
|stop ->TERMINATED),
NON_RUNNABLE = (resume ->RUNNABLE
|stop ->TERMINATED),
TERMINATED = STOP.
Concurrency: processes & threads 28
©Magee/Kramer 2nd Edition
Java thread lifecycle - an FSP specification
start suspend
sleep
stop dispatch suspend
0 1 2 3 4
run
stop
States 0 to 4 correspond to CREATED, TERMINATED, RUNNABLE,
RUNNING,
Concurrency: processes & and
threads NON-RUNNABLE respectively. 29
©Magee/Kramer 2nd Edition
CountDown timer example
Implementation in Java?
CountDown
init() counter target Thread
start()
stop()
display
run() NumberCanvas
tick()
beep() setvalue()
counter.start() created
target.run()
tick() alive
beep()
terminated
Concurrency: processes & threads 34
©Magee/Kramer 2nd Edition
CountDown
CountDown execution
init()
start()
counter.start() created
target.run()
stop()
tick() alive
tick()
counter=null
terminated
Concurrency: processes & threads 35
©Magee/Kramer 2nd Edition
Summary
Concepts
z process - unit of concurrency, execution of a program
Models
z LTS to model processes as state machines - sequences of
atomic actions
z FSP to specify processes using prefix “->”, choice ” | ”
and recursion.
Practice
z Java threads to implement processes.
z Thread lifecycle - created, running, runnable, non-
runnable, terminated.
Concurrency: processes & threads 36
©Magee/Kramer 2nd Edition