• No results found

3C03 Concurrency: Condition Synchronisation

N/A
N/A
Protected

Academic year: 2021

Share "3C03 Concurrency: Condition Synchronisation"

Copied!
10
0
0

Loading.... (view fulltext now)

Full text

(1)

1

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Mark Handley

3C03 Concurrency:

Condition Synchronisation

Goals

n Introduce concepts ofCondition synchronisationFairnessStarvation n Modelling:

Relationship between guarded actions and condition synchronisation?

n Implementation:

Condition Monitors in Java,

(2)

3

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Thread Waiting Queues in Java

n public final void notify()

Wakes up a single thread that is waiting on this object’s queue

n public final void notifyAll()

Wakes up all threads that are waiting on this object’s queue

n public final void wait()

throws InterruptedException

Waits to be notified by another thread. When notified must reacquire monitor.

Monitor

data

n Thread enters monitor when it acquires mutual

exclusion lock of monitor

n Thread exits monitor when releasing lock n Wait causes thread to exit monitor

Condition synchronisation in Java

Wait() Notify()

(3)

5

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Semaphore as a Java Monitor

class Semaphore { private int value_;

Semaphore (int initial) { value_=initial;

}

public synchronised down() { while (value_==0) wait(); --value;

}

public synchronised up() { ++value_;

notify(); }

}

Condition Synchronisation in Java

n FSP Model: when cond act -> NEWSTATE

n Java:

public synchronized void act() throws InterruptedException {

while (! cond) wait(); act

notifyAll(); }

n Loop re-tests cond to make sure that it is valid when it

(4)

7

© Wolfgang Emmerich, Mark Handley 1998 - 2003

CarParkControl revisited

CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] =

(when(i>0) arrive-> SPACES[i-1] |when(i<N) depart-> SPACES[i+1] ).

ARRIVALS = (arrive-> ARRIVALS). DEPARTURES = (depart-> DEPARTURES). ||CARPARK =

(ARRIVALS||CARPARKCONTROL||DEPARTURES).

CarParkControl revisited

class CarParkControl { private int spaces; private int N;

synchronized public void arrive() { while (spaces<=0) { try { wait(); } catch(InterruptedException e){} } --spaces; notify(); }

(5)

9

© Wolfgang Emmerich, Mark Handley 1998 - 2003

FSP and Condition Synchronisation

For each guarded action in the FSP model of a monitor:

n Implement action as a synchronised method

That invokes wait() in a while loop before it begins

While condition is negation of guard condition

n Every change in the monitor is signalled to waiting

threads using notify() or notifyAll()

Example: Producer/Consumer

Demo

Buffer

Producer Consumer

(6)

11

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Producer Consumer in FSP

PRODUCER = (put -> PRODUCER). CONSUMER = (get -> CONSUMER). BUFFER(SIZE=5) = BUFFER[0], BUFFER[count:0..SIZE] = (

when (count<SIZE) put->BUFFER[count+1] |when (count>0) get -> BUFFER[count-1]). ||PC=(PRODUCER||BUFFER||CONSUMER).

LTSA

Bounded Buffer - Outline

class Buffer {

private Object[] buf;

private int in = 0; //index for put private int out = 0; //index for get private int count = 0; //no of items private int size;

Buffer(int size) { this.size = size;

buf = new Object[size]; }

synchronized public void put(Object o) {…} synchronized public Object get() {…}

(7)

13

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Bounded Buffer - put

synchronized public void put(Object o) { while (count>=size) { try { wait(); } catch(InterruptedException e){} } buf[in] = o; ++count; in=(in+1) % size; notifyAll(); }

Bounded Buffer - get

synchronized public Object get() { while (count==0) {

try { wait();

} catch (InterruptedException e){} }

Object o =buf[out];

buf[out]=null; // for display purposes --count;

out=(out+1) % size;

notifyAll(); // [count < size] return (o);

(8)

15

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Monitor Invariants

Monitor invariant is assertion concerning attributes encapsulated by a monitor.

n Assertion must hold when no thread is in monitor.

Examples:

CarParkControl: 0<=spaces<=N

Semaphore: 0<=value

BoundedBuffer:

0<=count<=size && 0<=in<=size

&& 0<=out<=size && in=(out+count) % size n Used to reason about correctness of monitors.

Nested Monitor Problem

SEMAPHORE(I=0) = SEMA[I],

SEMA[v:0..3] = ( up->SEMA[v+1]

| when (v>0) down -> SEMA[v-1]). BUFFER = ( put -> down -> BUFFER

| get -> up -> BUFFER ). PRODUCER = (put -> PRODUCER).

CONSUMER = (get -> CONSUMER).

||BOUNDEDBUFFER = (PRODUCER || CONSUMER

(9)

17

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Nested Monitors

Normal sequence of events seems fine:

{put {down}} {get {up}} {put {down}} {get {up}} ...

But what happens when:

{put {down}} {put {down}} {put {down}} {put {down..zzz

PRODUCER will now block, waiting for an up.

n CONSUMER must do a get to cause an up to happen. n But the PRODUCER thread is still in the BUFFER

monitor, so CONSUMER cannot run.

n DEADLOCK!

This is known as the nested monitors problem.

Nested Monitors

A Thread that waits in a monitor releases only its lock, not the lock of any monitor from which it was called.

n Nested monitors calls need to be used with great care. n The only way to avoid nested monitors in Java is by

careful design.

(10)

19

© Wolfgang Emmerich, Mark Handley 1998 - 2003

Non-nested Monitors.

SEMAPHORE(I=0) = SEMA[I],

SEMA[v:0..3] = ( up->SEMA[v+1]

| when (v>0) down -> SEMA[v-1]). BUFFER = ( put -> BUFFER

| get -> BUFFER ).

PRODUCER = (data.down -> put -> spaces.up -> PRODUCER). CONSUMER = (spaces.down -> get -> data.up -> CONSUMER). ||BOUNDEDBUFFER = (PRODUCER || CONSUMER || BUFFER

|| spaces:SEMAPHORE(3)

|| data:SEMAPHORE(0))@{put,get}.

Summary

n Condition synchronization.

n In Java using wait(), notify() and notifyAll() n Used to implement Semaphores in Java.

n Relationship between an FSP model and its

implementation as a Java monitor.

n Monitor invariants.

References

Related documents

The insurance also covers costs outside of the building, provided that these pipes serve the insured building and/or physical structures or permanently in- stalled property outside

Our previous study showed that ASCT2 (Na + -dependent amino acid transporter (AAT)) mediates fl uciclovine uptake in androgen-dependent PCa cells; its expression is in fl uenced

waiters Only one condition expression that await() is waiting for is associated with the ConditionObject wait set &amp; each thread executes the same logic when returning

The objective of this research is to develop a prototype student advising expert system that assists the students of Information Systems (IS) major in selecting their

It is, therefore, not surprising that the majority of the sequence ions of protonated peptides are formed on unspecific fragmentation pathways where either the N -terminal

Information on how payroll taxes affect the three relevant income concepts – labour cost, gross earnings (net of employer payroll taxes) and net earnings (net of employer and

A new feature based on shape contexts (Belongie et al., 2002) named layout context is used to describe the arrangement of neighboring symbol bounding boxes relative to a

1; (4) if not, whether the claimant’s residual functional capacity permits him to perform his past work; and (5) if not, whether the claimant’s RFC, age, education, and work