COMP30112: Concurrency Topics 2.2: Concurrency in FSP & 3.1: - - PowerPoint PPT Presentation

comp30112 concurrency
SMART_READER_LITE
LIVE PREVIEW

COMP30112: Concurrency Topics 2.2: Concurrency in FSP & 3.1: - - PowerPoint PPT Presentation

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: Howard.Barringer@manchester.ac.uk February 2009


slide-1
SLIDE 1

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: Howard.Barringer@manchester.ac.uk

February 2009

slide-2
SLIDE 2

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

Outline

Topic 2.2: Modelling Processes with FSP - II Composing Processes in Parallel Interaction Labelling Processes Relabelling & Hiding Summary Topic 3.1: Java Threads: Revision Threads Example Synchronisation Example Thread Lifecycle

slide-3
SLIDE 3

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

Parallel Composition

⋆ Consider ‘DAY’ processes? ⋆ (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). ⋆ Possible Traces? ⋆

slide-4
SLIDE 4

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

Interleaving Actions

LAUGH

2 states ✒✑ ✓✏ ✒✑ ✓✏

1

laugh ◮ CONVERSE

3 states ✒✑ ✓✏ ✒✑ ✓✏ ✒✑ ✓✏

1 2

think ◮ talk ◮

⋆ Composite LTS? ⋆

slide-5
SLIDE 5

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). ⋆ LTS? ⋆ ⋆ Traces? ⋆ ⋆ Number of states? ⋆

slide-6
SLIDE 6

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

Shared Actions

  • processes in a composition may have common actions: shared

actions (i.e. process alphabets intersect)

  • shared actions model process interaction
  • unshared actions: arbitrary interleaving
  • shared actions: must be executed at the same time by all

processes MAKER = (make->ready->MAKER). USER = (ready->use->USER). || MAKER USER = (MAKER || USER). ⋆ LTS? ⋆ ⋆ Traces? ⋆ ⋆ Number of states? ⋆

slide-7
SLIDE 7

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). ⋆ LTS? ⋆

slide-8
SLIDE 8

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

⋆ LTS? ⋆

slide-9
SLIDE 9

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). ⋆ Write SWITCHES(3) in basic FSP? ⋆

slide-10
SLIDE 10

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 P with the labels a1.n, . . . , ax.n. Further, every transition n->X in the 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). ⋆ Write RESOURCE SHARE in basic FSP? ⋆

slide-11
SLIDE 11

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

Process Prefix Labels For Shared Resources

⋆ LTS for RESOURCE SHARE? ⋆

slide-12
SLIDE 12

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 names

  • f action labels. The general form of the relabelling function is:

/{newlabel1/oldlabel1, . . . , newlabeln/oldlabeln}. Relabelling to ensure that composed processes synchronize on particular actions: CLIENT = (call->wait->continue->CLIENT). SERVER = (request->service->reply->SERVER). || CLIENT SERVER = (CLIENT || SERVER) /{call/request, reply/wait}.

slide-13
SLIDE 13

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

Client Server LTSs

slide-14
SLIDE 14

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 described below 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}.

slide-15
SLIDE 15

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 the alphabet of P and makes these concealed actions “silent”. These silent actions are labelled tau. Silent actions in different processes are not shared. Sometimes it is more convenient to specify the set of labels to be exposed... When applied to a process P, the interface operator @{a1..ax} hides all the actions in the alphabet of P not labelled in the set a1..ax.

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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}
slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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(); }

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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.

slide-24
SLIDE 24

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 first
  • btain the lock for the object
  • if the lock is already taken, then thread will block until lock is

available

  • exception releases lock
  • scheduling policy for waiting threads
slide-25
SLIDE 25

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
slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

Car Park

slide-28
SLIDE 28

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(); } }

slide-29
SLIDE 29

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){} } }

slide-30
SLIDE 30

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){} } }

slide-31
SLIDE 31

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

Thread Lifecycle

slide-32
SLIDE 32

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.

slide-33
SLIDE 33

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

LTS for Thread Lifecycle