74
Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision COMP30112: Concurrency Topics 2.2: Concurrency in FSP & 3.1: Java Threads Howard Barringer Room KB2.20: email: [email protected] February 2008

COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Embed Size (px)

Citation preview

Page 1: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

COMP30112: ConcurrencyTopics 2.2: Concurrency in FSP & 3.1: Java Threads

Howard Barringer

Room KB2.20: email: [email protected]

February 2008

Page 2: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 3: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 4: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Parallel Composition

F Consider ‘DAY’ processes? F

(P||Q) represents the concurrent execution of P and Q.The operator || is the parallel composition operator.

LAUGH = (laugh->STOP).CONVERSE = (think->talk->STOP).

|| CONVERSE LAUGH = (LAUGH || CONVERSE).

F Possible Traces? F

Page 5: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Parallel Composition

F Consider ‘DAY’ processes? F

(P||Q) represents the concurrent execution of P and Q.The operator || is the parallel composition operator.

LAUGH = (laugh->STOP).CONVERSE = (think->talk->STOP).

|| CONVERSE LAUGH = (LAUGH || CONVERSE).

F Possible Traces? F

Page 6: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Parallel Composition

F Consider ‘DAY’ processes? F

(P||Q) represents the concurrent execution of P and Q.The operator || is the parallel composition operator.

LAUGH = (laugh->STOP).CONVERSE = (think->talk->STOP).

|| CONVERSE LAUGH = (LAUGH || CONVERSE).

F Possible Traces? F

Page 7: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Interleaving Actions

LAUGH

2 states

��������0 1

laughI CONVERSE

3 states

������������0 1 2

thinkI

talkI

F Composite LTS? F

Page 8: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Interleaving Actions

LAUGH

2 states

��������0 1

laughI CONVERSE

3 states

������������0 1 2

thinkI

talkI

F Composite LTS? F

Page 9: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Example: Clock Radio

CLOCK = (tick->CLOCK).RADIO = (on->off->RADIO).

||CLOCK RADIO = (CLOCK || RADIO).

F LTS? F F Traces? F F Number of states? F

Page 10: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Example: Clock Radio

CLOCK = (tick->CLOCK).RADIO = (on->off->RADIO).

||CLOCK RADIO = (CLOCK || RADIO).

F LTS? F

F Traces? F F Number of states? F

Page 11: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Example: Clock Radio

CLOCK = (tick->CLOCK).RADIO = (on->off->RADIO).

||CLOCK RADIO = (CLOCK || RADIO).

F LTS? F F Traces? F

F Number of states? F

Page 12: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Example: Clock Radio

CLOCK = (tick->CLOCK).RADIO = (on->off->RADIO).

||CLOCK RADIO = (CLOCK || RADIO).

F LTS? F F Traces? F F Number of states? F

Page 13: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 14: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Shared Actions

• processes in a composition may have common actions: sharedactions(i.e. process alphabets intersect)

• shared actions model process interaction

• unshared actions: arbitrary interleaving

• shared actions: must be executed at the same time by allprocesses

MAKER = (make->ready->MAKER).USER = (ready->use->USER).

|| MAKER USER = (MAKER || USER).

F LTS? F F Traces? F F Number of states? F

Page 15: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Shared Actions

• processes in a composition may have common actions: sharedactions(i.e. process alphabets intersect)

• shared actions model process interaction

• unshared actions: arbitrary interleaving

• shared actions: must be executed at the same time by allprocesses

MAKER = (make->ready->MAKER).USER = (ready->use->USER).

|| MAKER USER = (MAKER || USER).

F LTS? F F Traces? F F Number of states? F

Page 16: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Shared Actions

• processes in a composition may have common actions: sharedactions(i.e. process alphabets intersect)

• shared actions model process interaction

• unshared actions: arbitrary interleaving

• shared actions: must be executed at the same time by allprocesses

MAKER = (make->ready->MAKER).USER = (ready->use->USER).

|| MAKER USER = (MAKER || USER).

F LTS? F F Traces? F

F Number of states? F

Page 17: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Shared Actions

• processes in a composition may have common actions: sharedactions(i.e. process alphabets intersect)

• shared actions model process interaction

• unshared actions: arbitrary interleaving

• shared actions: must be executed at the same time by allprocesses

MAKER = (make->ready->MAKER).USER = (ready->use->USER).

|| MAKER USER = (MAKER || USER).

F LTS? F F Traces? F F Number of states? F

Page 18: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Handshake

Handshake is an action acknowledged by another:

MAKERv2 = (make->ready->used->MAKERv2).USERv2 = (ready->use->used->USERv2).

|| MAKER USERv2 = (MAKERv2 || USERv2).

F LTS? F

Page 19: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Handshake

Handshake is an action acknowledged by another:

MAKERv2 = (make->ready->used->MAKERv2).USERv2 = (ready->use->used->USERv2).

|| MAKER USERv2 = (MAKERv2 || USERv2).

F LTS? F

Page 20: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Handshake

Handshake is an action acknowledged by another:

MAKERv2 = (make->ready->used->MAKERv2).USERv2 = (ready->use->used->USERv2).

|| MAKER USERv2 = (MAKERv2 || USERv2).

F LTS? F

Page 21: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Handshake

Handshake is an action acknowledged by another:

MAKERv2 = (make->ready->used->MAKERv2).USERv2 = (ready->use->used->USERv2).

|| MAKER USERv2 = (MAKERv2 || USERv2).

F LTS? F

Page 22: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Handshake

Handshake is an action acknowledged by another:

MAKERv2 = (make->ready->used->MAKERv2).USERv2 = (ready->use->used->USERv2).

|| MAKER USERv2 = (MAKERv2 || USERv2).

F LTS? F

Page 23: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Multiple Processes

Multi-party synchronisation:

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).

F LTS? F

Page 24: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Multiple Processes

Multi-party synchronisation:

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).

F LTS? F

Page 25: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Multiple Processes

Multi-party synchronisation:

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).

F LTS? F

Page 26: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Multiple Processes

Multi-party synchronisation:

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).

F LTS? F

Page 27: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Multiple Processes

Multi-party synchronisation:

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).

F LTS? F

Page 28: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 29: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Labelling - I

a : P prefixes each action label in the alphabet of P with a.Two instances of a switch process:SWITCH = (on->off->SWITCH).|| TWO SWITCH = (a : SWITCH || b : SWITCH).

An array of instances of the switch process:||SWITCHES(N = 3) = (forall[i : 1..N] s[i] : SWITCH).||SWITCHES(N = 3) = (s[i : 1..N] : SWITCH).

F Write SWITCHES(3) in basic FSP? F

Page 30: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Labelling - I

a : P prefixes each action label in the alphabet of P with a.Two instances of a switch process:SWITCH = (on->off->SWITCH).|| TWO SWITCH = (a : SWITCH || b : SWITCH).

An array of instances of the switch process:||SWITCHES(N = 3) = (forall[i : 1..N] s[i] : SWITCH).||SWITCHES(N = 3) = (s[i : 1..N] : SWITCH).

F Write SWITCHES(3) in basic FSP? F

Page 31: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Labelling - I

a : P prefixes each action label in the alphabet of P with a.Two instances of a switch process:SWITCH = (on->off->SWITCH).|| TWO SWITCH = (a : SWITCH || b : SWITCH).

An array of instances of the switch process:||SWITCHES(N = 3) = (forall[i : 1..N] s[i] : SWITCH).||SWITCHES(N = 3) = (s[i : 1..N] : SWITCH).

F Write SWITCHES(3) in basic FSP? F

Page 32: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Labelling - II

{a1, . . . , ax} :: P replaces every action label n in the alphabet of Pwith the labels a1.n, . . . , ax.n. Further, every transition n->X inthe definition of P is replaced with the transitions{a1.n, . . . , ax.n}->X.

Process prefixing is useful for modeling shared resources:RESOURCE = (acquire->release->RESOURCE).USER = (acquire->use->release->USER).

||RESOURCE SHARE = (a : USER || b : USER|| {a,b} :: RESOURCE).

F Write RESOURCE SHARE in basic FSP? F

Page 33: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Labelling - II

{a1, . . . , ax} :: P replaces every action label n in the alphabet of Pwith the labels a1.n, . . . , ax.n. Further, every transition n->X inthe definition of P is replaced with the transitions{a1.n, . . . , ax.n}->X.

Process prefixing is useful for modeling shared resources:RESOURCE = (acquire->release->RESOURCE).USER = (acquire->use->release->USER).

||RESOURCE SHARE = (a : USER || b : USER|| {a,b} :: RESOURCE).

F Write RESOURCE SHARE in basic FSP? F

Page 34: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Labelling - II

{a1, . . . , ax} :: P replaces every action label n in the alphabet of Pwith the labels a1.n, . . . , ax.n. Further, every transition n->X inthe definition of P is replaced with the transitions{a1.n, . . . , ax.n}->X.

Process prefixing is useful for modeling shared resources:RESOURCE = (acquire->release->RESOURCE).USER = (acquire->use->release->USER).

||RESOURCE SHARE = (a : USER || b : USER|| {a,b} :: RESOURCE).

F Write RESOURCE SHARE in basic FSP? F

Page 35: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Prefix Labels For Shared Resources

F LTS for RESOURCE SHARE? F

Page 36: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Process Prefix Labels For Shared Resources

F LTS for RESOURCE SHARE? F

Page 37: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 38: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Action Relabelling - I

Relabelling functions are applied to processes to change the namesof action labels. The general form of the relabelling function is:

/{newlabel1/oldlabel1 , . . . , newlabeln/oldlabeln}.

Relabelling to ensure that composed processes synchronize onparticular actions:CLIENT = (call->wait->continue->CLIENT).SERVER = (request->service->reply->SERVER).

|| CLIENT SERVER = (CLIENT || SERVER)/{call/request, reply/wait}.

Page 39: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Action Relabelling - I

Relabelling functions are applied to processes to change the namesof action labels. The general form of the relabelling function is:

/{newlabel1/oldlabel1 , . . . , newlabeln/oldlabeln}.

Relabelling to ensure that composed processes synchronize onparticular actions:CLIENT = (call->wait->continue->CLIENT).SERVER = (request->service->reply->SERVER).

|| CLIENT SERVER = (CLIENT || SERVER)/{call/request, reply/wait}.

Page 40: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Client Server LTSs

Page 41: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Action Relabelling - II

Alternative formulation of the client server system is describedbelow using qualified or prefixed labels:SERVERv2 = (accept.request

->service->accept.reply->SERVERv2).CLIENTv2 = (call.request

->call.reply->continue->CLIENTv2).

|| CLIENT SERVERv2 = (CLIENTv2 || SERVERv2)/{call/accept}.

Page 42: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Action Hiding

When applied to a process P, the hiding operator\{a1..ax} removes the action names a1..ax from thealphabet of P and makes these concealed actions“silent”. These silent actions are labelled tau. Silentactions in different processes are not shared.

Sometimes it is more convenient to specify the set of labels to beexposed...

When applied to a process P, the interface operator@{a1..ax} hides all the actions in the alphabet of P notlabelled in the set a1..ax.

Page 43: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Action Hiding

When applied to a process P, the hiding operator\{a1..ax} removes the action names a1..ax from thealphabet of P and makes these concealed actions“silent”. These silent actions are labelled tau. Silentactions in different processes are not shared.

Sometimes it is more convenient to specify the set of labels to beexposed...

When applied to a process P, the interface operator@{a1..ax} hides all the actions in the alphabet of P notlabelled in the set a1..ax.

Page 44: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

The following definitions are equivalent:USER = (acquire->use->release->USER)

\{use}.USER = (acquire->use->release->USER)

@{acquire, release}.

Page 45: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 46: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Summary

• process composition e.g. (P || Q || R)

• shared actions synchronised• unshared actions interleaved

• process labeling e.g. a : P and {a,b} :: P

• action relabelling e.g. P/{a/b, c/d}• action hiding e.g. P\{a,b} and P@{c,d}

Page 47: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Summary

• process composition e.g. (P || Q || R)• shared actions synchronised

• unshared actions interleaved

• process labeling e.g. a : P and {a,b} :: P

• action relabelling e.g. P/{a/b, c/d}• action hiding e.g. P\{a,b} and P@{c,d}

Page 48: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Summary

• process composition e.g. (P || Q || R)• shared actions synchronised• unshared actions interleaved

• process labeling e.g. a : P and {a,b} :: P

• action relabelling e.g. P/{a/b, c/d}• action hiding e.g. P\{a,b} and P@{c,d}

Page 49: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Summary

• process composition e.g. (P || Q || R)• shared actions synchronised• unshared actions interleaved

• process labeling e.g. a : P and {a,b} :: P

• action relabelling e.g. P/{a/b, c/d}• action hiding e.g. P\{a,b} and P@{c,d}

Page 50: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Summary

• process composition e.g. (P || Q || R)• shared actions synchronised• unshared actions interleaved

• process labeling e.g. a : P and {a,b} :: P

• action relabelling e.g. P/{a/b, c/d}

• action hiding e.g. P\{a,b} and P@{c,d}

Page 51: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Summary

• process composition e.g. (P || Q || R)• shared actions synchronised• unshared actions interleaved

• process labeling e.g. a : P and {a,b} :: P

• action relabelling e.g. P/{a/b, c/d}• action hiding e.g. P\{a,b} and P@{c,d}

Page 52: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 53: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

The Thread Class

class MyThread extends Thread {int i;

public void run() {i = 0;while (i <= 5) {System.out.println("" + i);i++;

}}}

...MyThread mt = new MyThread();mt.start();...

Page 54: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

The Runnable Interface

public interface Runnable{

public abstract void run();

}

class MyRun implements Runnable {

int i;

public void run() {

i = 0;

while (i <= 5) {

System.out.println("" + i);

i++;

}}}

...

MyRun mr = new MyRun();

Thread mt = new Thread(mr);

mt.start();

...

Page 55: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 56: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Terminal Counter

class Counter implements Runnable {

Thread counter; int i;final static int N = 3;

Counter () {counter = new Thread(this);i = N;counter.start();

}

Page 57: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Terminal Counter - cont.

public void run () {while (true) {if (counter == null) return;if (i>0) {System.out.println("Count is " + i);try { Thread.sleep(500); }catch (InterruptedException e) {}i--;

}if (i==0) {System.out.println("Count is done");return;

}}//while

}//run }//class

Page 58: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

class CounterText {

public static void main (String [] args) {

System.out.println("Main starting counter...");Counter count = new Counter();System.out.println("Main Done");

}//main}//class CounterText

Page 59: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 60: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Thread Synchronisation

synchronized (obj) statements

synchronized typename methodname () statement

This provides mutually exclusive access to methods/objects.

Page 61: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

• each object has a single lock associated with it

• any synchronized code applied to an object must firstobtain the lock for the object

• if the lock is already taken, then thread will block until lock isavailable

• exception releases lock

• scheduling policy for waiting threads

Page 62: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

wait() and notify()

• wait(): suspend and block thread; release lock

• notify(): unblock a suspended thread

• notifyAll(): unblock all suspended threads

• wait sets

Page 63: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Condition Synchronization

synchronized type1 Method1 (...)throws InterruptedException {

while (!firstCondition){ wait(); }...

}

synchronized type2 Method2 (...) {...firstCondition = true;notify(); // or notifyAll()...

}

Note: signal-and-continue semantics for notify()

Page 64: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 65: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Car Park

Page 66: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Car Park Control Class

class CarParkControl {

protected int spaces;

protected int capacity;

CarParkControl(int n) {

capacity = spaces = n;

}

synchronized void arrive() throws InterruptedException {

while (spaces==0) wait();

--spaces;

notifyAll();

}

synchronized void depart() throws InterruptedException{

while (spaces==capacity) wait();

++spaces;

notifyAll();

}

}

Page 67: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Car Park Arrivals class

class Arrivals implements Runnable {

CarParkControl carpark;

Arrivals(CarParkControl c) {

carpark = c;

}

public void run() {

try {

while(true) {

ThreadPanel.rotate(330);

carpark.arrive();

ThreadPanel.rotate(30);

}

} catch (InterruptedException e){}

}

}

Page 68: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Car Park Departures class

class Departures implements Runnable {

CarParkControl carpark;

Departures(CarParkControl c) {

carpark = c;

}

public void run() {

try {

while(true) {

ThreadPanel.rotate(180);

carpark.depart();

ThreadPanel.rotate(180);

}

} catch (InterruptedException e){}

}

}

Page 69: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Outline

Topic 2.2: Modelling Processes with FSP - IIComposing Processes in ParallelInteractionLabelling ProcessesRelabelling & HidingSummary

Topic 3.1: Java Threads: RevisionThreadsExampleSynchronisationExampleThread Lifecycle

Page 70: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Thread Lifecycle

Page 71: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Thread Lifecycle

Page 72: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Thread Lifecycle

Page 73: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

FSP for Thread Lifecycle

THREAD = CREATED,

CREATED = (start ->RUNNING

|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.

Page 74: COMP30112: Concurrency - Topics 2.2: Concurrency in …howard/Teaching/COMP30112/Slides-08/lectures... · Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

Topic 2.2: Modelling Processes with FSP - II Topic 3.1: Java Threads: Revision

LTS for Thread Lifecycle