Java Semaphore (Part 1)
Transcript of Java Semaphore (Part 1)
![Page 1: Java Semaphore (Part 1)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/1.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/2.jpg)
2
• Appreciate the concept of semaphores
Learning Objectives in this Part of the Module
![Page 3: Java Semaphore (Part 1)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/3.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/4.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/5.jpg)
5
Overview of Semaphores
![Page 6: Java Semaphore (Part 1)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/6.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/7.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/8.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/9.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/10.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/11.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/12.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/13.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/14.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/15.jpg)
15
• There are two types of semaphores
Overview of Semaphores
![Page 16: Java Semaphore (Part 1)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/16.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/17.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/18.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/19.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/20.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/21.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/22.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/23.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/24.jpg)
24
• We’ll analyze examples of counting & binary semaphores later
Overview of Semaphores
![Page 25: Java Semaphore (Part 1)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/25.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/26.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/27.jpg)
27
Human Known Use of Semaphores
![Page 28: Java Semaphore (Part 1)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/28.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/29.jpg)
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)](https://reader033.fdocuments.net/reader033/viewer/2022041506/62524d5ed4e3421c94118c80/html5/thumbnails/30.jpg)
30
End of Java Semaphores (Part 1)