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

Java Semaphore (Part 1)

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java Semaphore (Part 1)

Java Semaphore (Part 1)

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Java Semaphore (Part 1)

2

• Appreciate the concept of semaphores

Learning Objectives in this Part of the Module

Page 3: Java Semaphore (Part 1)

3

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

• Recognize the two types of semaphores

Page 4: Java Semaphore (Part 1)

4

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

Page 5: Java Semaphore (Part 1)

5

Overview of Semaphores

Page 6: Java Semaphore (Part 1)

6

Overview of Semaphores• A semaphore is conceptually an “object”

that can be atomically incremented & decremented to control access to a shared resource

See en.wikipedia.org/wiki/Semaphore_(programming)

Page 7: Java Semaphore (Part 1)

7

• A semaphore is conceptually an “object” that can be atomically incremented & decremented to control access to a shared resource

• e.g., originally used to control access to a shared railroad track

Overview of Semaphores

See en.wikipedia.org/wiki/Railway_semaphore_signal

Page 8: Java Semaphore (Part 1)

8

• Concurrent programs use semaphoresto coordinate interactions between multiple threads

See tutorials.jenkov.com/java-concurrency/semaphores.html

Overview of Semaphores

Semaphore

Page 9: Java Semaphore (Part 1)

9

• Concurrent programs use semaphoresto coordinate interactions between multiple threads, e.g.,

• A semaphore can control the access of threads to a limited # of resources

See www.youtube.com/watch?v=RAv71VbdkBc for the Semaphore anthem ;-)

Overview of Semaphores

Semaphore

Page 10: Java Semaphore (Part 1)

10

• Concurrent programs use semaphoresto coordinate interactions between multiple threads, e.g.,

• A semaphore can control the access of threads to a limited # of resources

• It records a count (“permits”) of how many units of a resource are available

Overview of Semaphores

Semaphore3

Page 11: Java Semaphore (Part 1)

11

• Concurrent programs use semaphoresto coordinate interactions between multiple threads, e.g.,

• A semaphore can control the access of threads to a limited # of resources

• It records a count (“permits”) of how many units of a resource are available

• It provides operations to adjust the permit count atomically as units are acquired or released

Overview of Semaphores

Semaphore2

Page 12: Java Semaphore (Part 1)

12

• Concurrent programs use semaphoresto coordinate interactions between multiple threads, e.g.,

• A semaphore can control the access of threads to a limited # of resources

• It records a count (“permits”) of how many units of a resource are available

• It provides operations to adjust the permit count atomically as units are acquired or released

Overview of Semaphores

Semaphore1

Page 13: Java Semaphore (Part 1)

13

• Concurrent programs use semaphoresto coordinate interactions between multiple threads, e.g.,

• A semaphore can control the access of threads to a limited # of resources

• It records a count (“permits”) of how many units of a resource are available

• It provides operations to adjust the permit count atomically as units are acquired or released

• Threads can wait (timed or blocking) until a unit of the resource is available

Overview of Semaphores

Semaphore0

Page 14: Java Semaphore (Part 1)

14

• Concurrent programs use semaphoresto coordinate interactions between multiple threads, e.g.,

• A semaphore can control the access of threads to a limited # of resources

• It records a count (“permits”) of how many units of a resource are available

• It provides operations to adjust the permit count atomically as units are acquired or released

• Threads can wait (timed or blocking) until a unit of the resource is available

• When a thread is done with a resource the permit count is incremented atomically & another waiting thread can acquire it

Overview of Semaphores

Semaphore1

Page 15: Java Semaphore (Part 1)

15

• There are two types of semaphores

Overview of Semaphores

Page 16: Java Semaphore (Part 1)

16

• There are two types of semaphores

• Counting semaphores

Overview of Semaphores

See javarevisited.blogspot.com/2012/05/counting-semaphore-example-in-java-5.html

Page 17: Java Semaphore (Part 1)

17

• There are two types of semaphores

• Counting semaphores

• Have # of permits defined by a counter (N) with precise meaning

Overview of Semaphores

Page 18: Java Semaphore (Part 1)

18

• There are two types of semaphores

• Counting semaphores

• Have # of permits defined by a counter (N) with precise meaning

• Negative

• exactly -N threads queued waiting to acquire semaphore

Overview of Semaphores

Page 19: Java Semaphore (Part 1)

19

• There are two types of semaphores

• Counting semaphores

• Have # of permits defined by a counter (N) with precise meaning

• Negative

• Zero == no waiting threads

• an acquire operation will block the invoking thread until thecounter N is positive

Overview of Semaphores

Page 20: Java Semaphore (Part 1)

20

• There are two types of semaphores

• Counting semaphores

• Have # of permits defined by a counter (N) with precise meaning

• Negative

• Zero == no waiting threads

• Positive == no waiting threads

• an acquire operation will notblock the invoking thread

Overview of Semaphores

Page 21: Java Semaphore (Part 1)

21

• There are two types of semaphores

• Counting semaphores

• Binary semaphores

Overview of Semaphores

See howtodoinjava.com/core-java/multi-threading/binary-semaphore-tutorial-and-example

Page 22: Java Semaphore (Part 1)

22

• There are two types of semaphores

• Counting semaphores

• Binary semaphores

• Have only 2 states: acquired (0) & not acquired (1)

Overview of Semaphores

Page 23: Java Semaphore (Part 1)

23

• There are two types of semaphores

• Counting semaphores

• Binary semaphores

• Have only 2 states: acquired (0) & not acquired (1)

• Restrict the counter N to the values 0 & 1

Overview of Semaphores

In practice, binary semaphores are often implemented via counting semaphores

Page 24: Java Semaphore (Part 1)

24

• We’ll analyze examples of counting & binary semaphores later

Overview of Semaphores

Page 25: Java Semaphore (Part 1)

25

• We’ll analyze examples of counting & binary semaphores later, e.g.

• The PalantiriSimulator app use a counting semaphore

Overview of Semaphores

1 Semaphores 0

run()

ping :

PingPongThread

pong :

PingPongThread

print("ping")

run()

print("pong")

0 Semaphore

See github.com/douglascraigschmidt/CS891/tree/master/assignments

Page 26: Java Semaphore (Part 1)

26

• We’ll analyze examples of counting & binary semaphores later, e.g.

• The PalantiriSimulator app use a counting semaphore

• The Ping/Ping app uses a pair of binary semaphores

Overview of Semaphores

1 Semaphores 0

run()

ping :

PingPongThread

pong :

PingPongThread

print("ping")

run()

print("pong")

See github.com/douglascraigschmidt/LiveLessons/tree/master/PingPongApplication

Page 27: Java Semaphore (Part 1)

27

Human Known Use of Semaphores

Page 28: Java Semaphore (Part 1)

28

• A human known use of counting semaphores applies them to schedule access to beach volleyball courts

See en.wikipedia.org/wiki/Corona_del_Mar_State_Beach

Human Known Uses of Semaphores

Page 29: Java Semaphore (Part 1)

29

• A human known use of counting semaphores applies them to schedule access to beach volleyball courts

• A bag full of balls is used to limit the number of teams that can concurrently play volleyball

Human Known Uses of Semaphores

Page 30: Java Semaphore (Part 1)

30

End of Java Semaphores (Part 1)