java semaphore (part 3) - vanderbilt university · applying a java semaphore to mediate access •...

34
Java Semaphore (Part 3) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 14-Aug-2020

28 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

Java Semaphore (Part 3)

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

2

Learning Objectives in this Part of the Module• Appreciate the concept of semaphores

• Recognize the two types of semaphores

• Know a human known use of semaphores

• Understand the structure & functionality of Java Semaphore& its methods

• Recognize how Java semaphores enable multiple threads to

• Mediate access to a limited # of shared resources

Semaphore

Page 3: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

3

Learning Objectives in this Part of the Module• Appreciate the concept of semaphores

• Recognize the two types of semaphores

• Know a human known use of semaphores

• Understand the structure & functionality of Java Semaphore& its methods

• Recognize how Java semaphores enable multiple threads to

• Mediate access to a limited # of shared resources

• Coordinate the order in whichoperations occur

1 Semaphores 0

run()

ping :

PingPongThread

pong :

PingPongThread

print("ping")

run()

print("pong")

Page 4: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

4

Applying a Java Semaphore to Mediate Access

Page 5: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

5See en.wikipedia.org/wiki/Palantir

Semaphore

Applying a Java Semaphore to Mediate Access

Each being is implemented to run in a separate thread

• This Android app show how an Java semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

Page 6: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

6

Semaphore

• This Android app show how an Java semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

• The app can be configured to restrict the # of being threads that concurrently gaze into palantiri

Applying a Java Semaphore to Mediate Access

e.g., limit use to two palantirion a quad-core device to

ensure system responsiveness

2

Page 7: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

7

Semaphore1

• This Android app show how an Java semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

• The app can be configured to restrict the # of being threads that concurrently gaze into palantiri

• A permit must be acquired from a semaphore before a being can gaze

Applying a Java Semaphore to Mediate Access

Acquiring a permit atomically decrements the permit count

Page 8: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

8

Semaphore0

Applying a Java Semaphore to Mediate Access• This Android app show how an Java

semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

• The app can be configured to restrict the # of being threads that concurrently gaze into palantiri

• A permit must be acquired from a semaphore before a being can gaze

All available permits are now in use

Page 9: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

9

Semaphore0

Applying a Java Semaphore to Mediate Access• This Android app show how an Java

semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

• The app can be configured to restrict the # of being threads that concurrently gaze into palantiri

• A permit must be acquired from a semaphore before a being can gaze

• Other being threads must block until a permit is available

Page 10: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

10

Semaphore1

Applying a Java Semaphore to Mediate Access• This Android app show how an Java

semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

• The app can be configured to restrict the # of being threads that concurrently gaze into palantiri

• A permit must be acquired from a semaphore before a being can gaze

• Other being threads must block until a permit is available

• When a being thread is done itgazing it releases the semaphore

Page 11: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

11

Semaphore0

Applying a Java Semaphore to Mediate Access• This Android app show how an Java

semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

• The app can be configured to restrict the # of being threads that concurrently gaze into palantiri

• A permit must be acquired from a semaphore before a being can gaze

• Other being threads must block until a permit is available

• When a being thread is done it gazing it releases the semaphore

• Another being thread can then acquire it & proceed to gaze

Page 12: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

12

Semaphore0

This example “fully brackets” the acquiring & releasing of permits, i.e., the thread that acquires a semaphore is the same as the one that releases it

Applying a Java Semaphore to Mediate Access• This Android app show how an Java

semaphore can be used to limit the # of Middle-Earth beings who cangaze into Palantiri concurrently

• The app can be configured to restrict the # of being threads that concurrently gaze into palantiri

• A permit must be acquired from a semaphore before a being can gaze

• Other being threads must block until a permit is available

• When a being thread is done it gazing it releases the semaphore

• Another being thread can then acquire it & proceed to gaze

Page 13: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

13

• UML sequence diagram for this app

start()

run()start()

start()

:

BeingRunnables

mPalantiriManager

: PalantiriManager

: Palantiri

Presenter

release(r)

start()

:

Palantir

r = acquire()

r.gaze()

run()

release(r)

r = acquire()

r.gaze()

run()

release(r)

r = acquire()

r.gaze()

Applying a Java Semaphore to Mediate Access

Page 14: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

14

• UML sequence diagram for this app

start()

Applying a Java Semaphore to Mediate Access

: Palantiri

Presenter

Page 15: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

15

• UML sequence diagram for this app

start()

start()

start()

start()

Applying a Java Semaphore to Mediate Access

:

BeingRunnables

: Palantiri

Presenter

Page 16: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

16

• UML sequence diagram for this app

start()

run()start()

start()

start()

run()

run()

Applying a Java Semaphore to Mediate Access

:

BeingRunnables

: Palantiri

Presenter

Page 17: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

17

• UML sequence diagram for this app

start()

run()start()

start()

mPalantiriManager

: PalantiriManager

start()

p = acquire()

run()

run()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

:

BeingRunnables

: Palantiri

Presenter

Page 18: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

18

• UML sequence diagram for this app

start()

run()start()

start()

start()

p = acquire()

run()

run()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

mPalantiriManager

: PalantiriManager

:

BeingRunnables

: Palantiri

Presenter

Page 19: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

19

• UML sequence diagram for this app

start()

run()start()

start()

start()

p :

Palantir

p = acquire()

p.gaze()

run()

run()

p.gaze()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

mPalantiriManager

: PalantiriManager

:

BeingRunnables

: Palantiri

Presenter

Page 20: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

20

• UML sequence diagram for this app

start()

run()start()

start()

release(p)

start()

p = acquire()

p.gaze()

run()

run()

release(p)

p.gaze()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

p :

Palantir

mPalantiriManager

: PalantiriManager

:

BeingRunnables

: Palantiri

Presenter

Page 21: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

21

• UML sequence diagram for this app

start()

run()start()

start()

release(p)

start()

p = acquire()

p.gaze()

run()

run()

release(p)

p.gaze()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

p :

Palantir

mPalantiriManager

: PalantiriManager

:

BeingRunnables

: Palantiri

Presenter

Page 22: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

22

• UML sequence diagram for this app

start()

run()start()

start()

release(p)

start()

p = acquire()

p.gaze()

run()

p.gaze()

run()

release(p)

p.gaze()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

p :

Palantir

mPalantiriManager

: PalantiriManager

:

BeingRunnables

: Palantiri

Presenter

Page 23: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

23

• UML sequence diagram for this app

start()

run()start()

start()

release(p)

start()

p = acquire()

p.gaze()

run()

release(p)

p.gaze()

run()

release(p)

p.gaze()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

p :

Palantir

mPalantiriManager

: PalantiriManager

:

BeingRunnables

: Palantiri

Presenter

Page 24: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

24

• UML sequence diagram for this app

start()

run()start()

start()

release(p)

start()

p = acquire()

p.gaze()

run()

release(p)

p.gaze()

run()

release(p)

p.gaze()

p = acquire()

p = acquire()

Applying a Java Semaphore to Mediate Access

p :

Palantir

mPalantiriManager

: PalantiriManager

:

BeingRunnables

: Palantiri

Presenter

Page 25: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

25

Applying Java Semaphores to Coordinate Threads

Page 26: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

26

• The Android ping-pong app coordinates thread interactions via various Java synchronizers, including Java semaphores

• i.e., these two threads alternate printing “ping” & “pong” on the display

run()

ping :

PingPongThread

pong :

PingPongThread

print("ping")

run()

print("pong")

Applying Java Semaphores to Coordinate Threads

1 Semaphores 0

See github.com/douglascraigschmidt/POSA/tree/master/ex/M3/PingPong

Page 27: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

27

• UML sequence diagram for the ping-pong app

join()

run()

start() run()

new()

new()

acquire()

run()

release()

start()

pong :

PingPongThread

ping :

PingPongThread

acquire()

release()

join()

println()

println()

Applying Java Semaphores to Coordinate Threads

: Play

PingPongThreadpingSem :Semaphore

pongSem :Semaphore

This app can be configured to use a pair of semaphores that coordinate the order in which the “ping” & “pong” threads are called to play ping-pong

Page 28: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

28

• UML sequence diagram for the ping-pong app

Applying Java Semaphores to Coordinate Threads

The PlayPingPongThread object starts two threads, ping & pong, that alternate printing "Ping" and "Pong", respectively, on the display

run()

start()

new()

new()

start()

pong :

PingPongThread

ping :

PingPongThread

: Play

PingPongThread

Page 29: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

29

• UML sequence diagram for the ping-pong app

run()

start() run()

new()

new()

run()start()

pong :

PingPongThread

ping :

PingPongThread

: Play

PingPongThread

Applying Java Semaphores to Coordinate Threads

The PingPongThread class implements the core ping-pong algorithm, but defers synchronization aspects to subclasses via the Template Method pattern

Page 30: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

30

• UML sequence diagram for the ping-pong app

run()

start() run()

new()

new()

run()start()

pong :

PingPongThreadpingSem :Semaphore

ping :

PingPongThreadpongSem :Semaphore

The pingSem & PongSem semaphores coordinate the order in which the “ping” & “pong” threads are called to play ping-pong

: Play

PingPongThread

Applying Java Semaphores to Coordinate Threads

Semaphore pingSem =

new Semaphore(1);

Semaphore pongSem =

new Semaphore(0);

Permit count initialized to

“not-acquired”

Permit count initialized to “acquired”

Page 31: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

31

• UML sequence diagram for the ping-pong app

run()

start() run()

new()

new()

acquire()

run()

release()

start()

pong :

PingPongThread

ping :

PingPongThread

acquire()

release()

println()

println()

: Play

PingPongThread

Applying Java Semaphores to Coordinate Threads

This example does not “fully bracket” acquiring & releasing permits, i.e., the thread acquiring a semaphore is different from the thread releasing it!

pingSem :Semaphore

pongSem :Semaphore

Page 32: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

32

• UML sequence diagram for the ping-pong app

run()

start() run()

new()

new()

run()start()

pong :

PingPongThread

ping :

PingPongThread

: Play

PingPongThread

Applying Java Semaphores to Coordinate Threads

This example does not “fully bracket” acquiring & releasing permits, i.e., the thread acquiring a semaphore is different from the thread releasing it!

pingSem :Semaphore

pongSem :Semaphore

private final Semaphore mMine;

private final Semaphore mOther;

...

protected void acquire() { mMine.acquire(); }

protected void release() { mOther.release(); }

Block until semaphore is acquired

Release the other semaphore

Page 33: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

33

• UML sequence diagram for the ping-pong app

join()

run()

start() run()

new()

new()

acquire()

run()

release()

start()

pong :

PingPongThread

: Play

PingPongThread

ping :

PingPongThread

acquire()

release()

join()

println()

println()

PlayPingPongThread joins with the ping & pong threads once they finish

Applying Java Semaphores to Coordinate Threads

pingSem :Semaphore

pongSem :Semaphore

Page 34: Java Semaphore (Part 3) - Vanderbilt University · Applying a Java Semaphore to Mediate Access • This Android app show how an Java semaphore can be used to limit the # of Middle-Earth

34

End of Java Semaphores (Part 3)