java monitor objects: introduction - vanderbilt university
TRANSCRIPT
![Page 1: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/1.jpg)
Java Monitor Objects:
Introduction
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/2.jpg)
2
Critical Section
3. wait()
1. Enter monitor object
6. Leave monitor object
4. notifyAll()
2. Acquire lock
5. Release lock
See www.artima.com/insidejvm/ed2/threadsynch.html
Learning Objectives in this Part of the Lesson• Understand what monitors are & know how Java built-in monitor objects can
ensure mutual exclusion & coordination between threads
![Page 3: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/3.jpg)
3
Check
in
Operating
Room
Pre-Operation
Waiting Room
Pre-Operation
Waiting Room
Post-
Operation
Waiting
Room
Critical Section
Learning Objectives in this Part of the Lesson• Understand what monitors are & know how Java built-in monitor objects can
ensure mutual exclusion & coordination between threads
• Note a human known use of monitors
![Page 4: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/4.jpg)
4
Overview of Monitors
![Page 5: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/5.jpg)
5
• A monitor is a synchronization mechanism designed in the early 1970s
Overview of Monitors
See en.wikipedia.org/wiki/Monitor_(synchronization)
![Page 6: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/6.jpg)
6
Overview of Monitors
T2
T3 T4
T1
• A monitor provides three capabilities to concurrent programs
Critical Section
![Page 7: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/7.jpg)
7
Overview of Monitors
T2T3
T4
Acquire lock
• A monitor provides three capabilities to concurrent programs
1. Only one thread at a time has mutually exclusive access to a critical section
See en.wikipedia.org/wiki/Critical_section
Critical SectionRunning Thread
T1
![Page 8: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/8.jpg)
8
Overview of Monitors
Wait on condition
Critical SectionRunning Thread
T4
T1
T2
T3Acquire lock
• A monitor provides three capabilities to concurrent programs
1. Only one thread at a time has mutually exclusive access to a critical section
2. Threads running in a monitor canblock awaiting certain conditions to become true
![Page 9: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/9.jpg)
9
• A monitor provides three capabilities to concurrent programs
1. Only one thread at a time has mutually exclusive access to a critical section
2. Threads running in a monitor canblock awaiting certain conditions to become true
3. A thread can notify one or morethreads that conditions they’re waiting on have been met
Overview of Monitors
Unblock on wait queue
T4T3
T2Critical Section
Running Thread
T1
Acquire lock
![Page 10: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/10.jpg)
10
Overview of Built-in Java Monitor Objects
![Page 11: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/11.jpg)
11
• All objects in Java can be used as built-in monitor objects, which support two types of thread synchronization
Overview of Java Built-in Monitor Objects
SynchronizedQueue
synchronized m1()synchronized m2()
Thread1
m1() m2()Thread2
A Java Monitor Object
See en.wikipedia.org/wiki/Monitor_(synchronization)#Implicit_condition_variable_monitors
![Page 12: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/12.jpg)
12
• All objects in Java can be used as built-in monitor objects, which support two types of thread synchronization
• Mutual exclusion – allows concurrent access & updates to shared data without race conditions
Overview of Java Built-in Monitor Objects• All objects in Java can be used as built-in monitor objects, which support
two types of thread synchronization
• Mutual exclusion – allows concurrent access & updates to shared data without race conditions
SynchronizedQueue
synchronized m1()synchronized m2()
Thread1
m1() m2()Thread2
A Java Monitor Object
![Page 13: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/13.jpg)
13
• All objects in Java can be used as built-in monitor objects, which support two types of thread synchronization
• Mutual exclusion – allows concurrent access & updates to shared data without race conditions
Overview of Java Built-in Monitor Objects
SynchronizedQueue
1
Entrance Queue
synchronized m1()synchronized m2()
Thread1
m1() m2()Thread2
A Java Monitor Object
<<contains>>
Java’s execution environment supports mutual exclusion via an entrance queue & synchronized methods/statements
Every Java object has a single “intrinsic lock” associated with it
![Page 14: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/14.jpg)
14
• All objects in Java can be used as built-in monitor objects, which support two types of thread synchronization
• Mutual exclusion – allows concurrent access & updates to shared data without race conditions
• Coordination – Ensures computations run properly, e.g., in the right order, at the right time, under the right conditions, etc.
Overview of Java Built-in Monitor Objects
SynchronizedQueue
1
Entrance Queue
synchronized m1()synchronized m2()
Thread1
m1() m2()Thread2
A Java Monitor Object
<<contains>>
![Page 15: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/15.jpg)
15
• All objects in Java can be used as built-in monitor objects, which support two types of thread synchronization
• Mutual exclusion – allows concurrent access & updates to shared data without race conditions
• Coordination – Ensures computations run properly, e.g., in the right order, at the right time, under the right conditions, etc.
Overview of Java Built-in Monitor Objects
SynchronizedQueue
1 1
Entrance Queue
synchronized m1()synchronized m2()
Thread1
m1() m2()Thread2
A Java Monitor Object
<<contains>><<contains>>
Java’s execution environment supports coordination via a wait queue & notification mechanisms
Every Java object has one “intrinsic condition” associated with it
Wait Queue
wait()notify()notifyAll()
![Page 16: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/16.jpg)
16See www.dre.vanderbilt.edu/~schmidt/PDF/monitor.pdf
• These mechanisms implement a variant of the Monitor Object pattern
Overview of Java Built-in Monitor Objects
SynchronizedQueue
<<contains>>
Monitor Condition Monitor Lock
synchronized m1()synchronized m2()
wait()notify()notifyAll()
Thread1
m1() m2()Thread2
Monitor Object
<<contains>>
0..* 0..*
![Page 17: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/17.jpg)
17
Overview of Java Built-in Monitor Objects• These mechanisms implement a variant of the Monitor Object pattern
• Intent – Ensure that only one method runs within an object & allow an object’s methods to cooperatively schedule their execution sequences
SynchronizedQueue
synchronized m1()synchronized m2()
Thread1
m1() m2()Thread2
<<contains>><<contains>>
0..* 0..*
Monitor Object
Monitor Condition Monitor Lock
wait()notify()notifyAll()
![Page 18: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/18.jpg)
18
Human Known Use of Monitors
![Page 19: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/19.jpg)
19
• A human known use of a monitor is an operating room in a hospital
Human Know Use of Monitors
Check
in
Operating
Room
Waiting
Room
Waiting
Room
Waiting
Room
Critical Section
![Page 20: Java Monitor Objects: Introduction - Vanderbilt University](https://reader031.vdocuments.us/reader031/viewer/2022020706/61fc8cc8082f2301006303c7/html5/thumbnails/20.jpg)
20
End of Java Monitor Objects: Introduction