• No results found

Java Monitor Objects: Coordination Methods

N/A
N/A
Protected

Academic year: 2021

Share "Java Monitor Objects: Coordination Methods"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)

Java Monitor Objects:

Coordination Methods

Douglas C. Schmidt

[email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

(2)

2

• Understand how Java built-in monitor objects provide waiting & notification mechanisms that coordinate threads running in a concurrent program

Critical Section

3. wait() 1. Enter monitor object

6. Leave monitor object 4. notifyAll()

2. Acquire lock

5. Release lock

(3)

3

Java Built-in Waiting &

Notification Mechanisms

(4)

4

Java Built-in Waiting & Notification Mechanisms

Java synchronized methods & statements only provide a partial solution to concurrent programs

(5)

5

Java Built-in Waiting & Notification Mechanisms

• Java monitor objects allow threads

to coordinate their interactions

Wait on condition Critical Section Running Thread

T4

T1 T2

T3

(6)

6

Java Built-in Waiting & Notification Mechanisms

• Java monitor objects allow threads

to coordinate their interactions • via the wait(), notify(), &

notifyAll() methods

See docs.oracle.com/javase/8/docs/api/java/lang/Object.html

void wait() – Causes the current thread to wait until another thread invokes the notify() method or the

notifyAll() method for this object

void notify() – Wakes up a single thread that is waiting on this object's

monitor

void notifyAll() – Wakes up all threads that are waiting on this object's monitor

(7)

7

Java Built-in Waiting & Notification Mechanisms

• Java monitor objects allow threads

to coordinate their interactions • via the wait(), notify(), &

notifyAll() methods

void wait() – Causes the current thread to wait until another thread invokes the notify() method or the

notifyAll() method for this object

void notify() – Wakes up a single thread that is waiting on this object's

monitor

void notifyAll() – Wakes up all threads that are waiting on this object's monitor

(8)

8

Java Built-in Waiting & Notification Mechanisms

• Java monitor objects allow threads

to coordinate their interactions • via the wait(), notify(), &

notifyAll() methods

void wait() – Causes the current thread to wait until another thread invokes the notify() method or the

notifyAll() method for this object

void notify() – Wakes up a single thread that is waiting on this object's

monitor

void notifyAll() – Wakes up all threads that are waiting on this object's monitor

(9)

9

Java Built-in Waiting & Notification Mechanisms

• Java monitor objects allow threads

to coordinate their interactions • via the wait(), notify(), &

notifyAll() methods

void wait() – Causes the current thread to wait until another thread invokes the notify() method or the

notifyAll() method for this object

void notify() – Wakes up a single thread that is waiting on this object's

monitor

void notifyAll() – Wakes up all threads that are waiting on this object's monitor

(10)

10

Java Built-in Waiting & Notification Mechanisms

• Java monitor objects allow threads

to coordinate their interactions • via the wait(), notify(), &

notifyAll() methods

void wait() – Causes the current thread to wait until another thread invokes the notify() method or the

notifyAll() method for this object

void notify() – Wakes up a single thread that is waiting on this object's

monitor

void notifyAll() – Wakes up all threads that are waiting on this object's monitor

(11)

11 • Java built-in monitor objects

have one entrance queue & one wait queue

Java Built-in Waiting & Notification Mechanisms

Entrance Queue Wait Queue Seeen.wikipedia.org/wiki/Monitor_(synchronization)#Implicit_condition_variable_monitors Critical Section

(12)

12 • Java built-in monitor objects

have one entrance queue & one wait queue

Java Built-in Waiting & Notification Mechanisms

Serializes thread access to monitor object’s critical section

Entrance Queue

Wait Queue

Critical Section

(13)

13 • Java built-in monitor objects

have one entrance queue & one wait queue

Java Built-in Waiting & Notification Mechanisms

All threads that call wait() are parked on the wait queue

Entrance Queue

Wait Queue

Critical Section

(14)

14 • Java built-in monitor objects

have one entrance queue & one wait queue

Java Built-in Waiting & Notification Mechanisms

All notify() & notifyAll() calls also apply to the wait queue Entrance

Queue

Wait Queue

Critical Section

(15)

15

Java Built-in Waiting & Notification Mechanisms

class SimpleBoundedBlockingQueue<E>

implements BlockingQueue<E> { ...

public void put(E msg){ synchronized(this) {

while (isFull()) wait(); mList.add(msg); notifyAll(); } } public E take() ... { synchronized(this) {

while (isEmpty()) wait(); notifyAll();

return mList.poll(); }

} ...

• Java built-in monitor objects have one entrance queue & one wait queue

See github.com/douglascraigschmidt/POSA/tree/master/ ex/M3/BoundedBuffers/SimpleBoundedBlockingQueue

This class fixes the “busy waiting” problem with

(16)

16

Java Built-in Waiting & Notification Mechanisms

class SimpleBoundedBlockingQueue<E> implements BlockingQueue<E> { ...

public void put(E msg){

synchronized(this) {

while (isFull()) wait();

mList.add(msg); notifyAll(); } } public E take() ... { synchronized(this) {

while (isEmpty()) wait(); notifyAll();

return mList.poll(); }

} ...

• Java built-in monitor objects have one entrance queue & one wait queue, e.g.

• put() calls wait() when the queue is full

See en.wikipedia.org/wiki/Guarded_suspension

Atomically releases the intrinsic lock & sleeps on the wait queue

(17)

17

Java Built-in Waiting & Notification Mechanisms

class SimpleBoundedBlockingQueue<E> implements BlockingQueue<E> { ...

public void put(E msg){ synchronized(this) {

while (isFull()) wait();

mList.add(msg); notifyAll(); } } public E take() ... { synchronized(this) {

while (isEmpty()) wait(); notifyAll();

return mList.poll(); }

} ...

• Java built-in monitor objects have one entrance queue & one wait queue, e.g.

• put() calls wait() when the queue is full

• It also calls notifyAll() after adding an item

Must wake up all the threads blocked on the wait queue since waiters are non-uniform

(18)

18

Java Built-in Waiting & Notification Mechanisms

class SimpleBoundedBlockingQueue<E> implements BlockingQueue<E> { ...

public void put(E msg){ synchronized(this) {

while (isFull()) wait(); mList.add(msg); notifyAll(); } } public E take() ... { synchronized(this) {

while (isEmpty()) wait(); notifyAll();

return mList.poll(); }

} ...

• Java built-in monitor objects have one entrance queue & one wait queue, e.g.

• put() calls wait() when the queue is full

• It also calls notifyAll() after adding an item

notifyAll() is required due to a Java monitor object only

having one wait queue

See stackoverflow.com/questions/37026/java-notify -vs-notifyall-all-over-again/3186336#3186336

(19)

19

Java Built-in Waiting & Notification Mechanisms

class SimpleBoundedBlockingQueue<E> implements BlockingQueue<E> { ...

public void put(E msg){ synchronized(this) {

while (isFull()) wait(); mList.add(msg); notifyAll(); } } public E take() ... { synchronized(this) {

while (isEmpty()) wait();

notifyAll();

return mList.poll(); }

} ...

• Java built-in monitor objects have one entrance queue & one wait queue, e.g.

• put() calls wait() when

the queue is full

• take() calls wait() when the queue is empty

Atomically releases the intrinsic lock & sleeps on the wait queue

(20)

20

Again, notifyAll() is required here due to the limitations of Java monitor objects, which only have one wait queue

Java Built-in Waiting & Notification Mechanisms

• Java built-in monitor objects

have one entrance queue & one wait queue, e.g.

• put() calls wait() when

the queue is full

• take() calls wait() when the queue is empty

• It also calls notifyAll() after removing an item

class SimpleBoundedBlockingQueue<E> implements BlockingQueue<E> { ...

public void put(E msg){ synchronized(this) {

while (isFull()) wait(); mList.add(msg); notifyAll(); } } public E take() ... { synchronized(this) {

while (isEmpty()) wait();

notifyAll();

return mList.poll(); }

} ...

Must wake up all the threads blocked on the wait queue since waiters are non-uniform

(21)

21

Java Built-in Waiting & Notification Mechanisms

• Java built-in monitor objects

have one entrance queue & one wait queue

class SimpleBoundedBlockingQueue<E> implements BlockingQueue<E> { ...

public void put(E msg){

synchronized(this) {

while (isFull()) wait(); mList.add(msg); notifyAll(); } } public E take() ... { synchronized(this) {

while (isEmpty()) wait(); notifyAll();

return mList.poll(); }

} ...

See upcoming lesson on “Java Monitor Objects: Coordination Example Implementation”

The put() & take() methods are examined later in this lesson

(22)

22

• Java built-in monitor object synchronizers can be implemented w/POSIX-like synchronizers

Java Built-in Waiting & Notification Mechanisms

Entrance Queue Wait Queue Critical Section

(23)

23

Java Built-in Waiting & Notification Mechanisms

See computing.llnl.gov/tutorials/pthreads/#Mutexes Entrance Queue Wait Queue Critical Section

• Java built-in monitor object synchronizers can be implemented w/POSIX-like synchronizers, e.g.

• Entrance queue is akin to a POSIX recursive mutex

(24)

24

• Java built-in monitor object synchronizers can be implemented w/POSIX-like synchronizers, e.g.

• Entrance queue is akin to

a POSIX recursive mutex

• Wait queue is akin to a POSIX condition variable

Java Built-in Waiting & Notification Mechanisms

See computing.llnl.gov/tutorials/pthreads/#ConditionVariables Entrance Queue Wait Queue Critical Section

(25)

25

• Java built-in monitor object synchronizers can be implemented w/POSIX-like synchronizers, e.g.

• Entrance queue is akin to

a POSIX recursive mutex

• Wait queue is akin to a POSIX condition variable • Similar to Java

ConditionObjects

Java Built-in Waiting & Notification Mechanisms

See earlier lessons on “Java ConditionObjects”

Entrance Queue Wait Queue Critical Section

(26)

26

• Java built-in monitor object synchronizers can be implemented w/POSIX-like synchronizers, e.g.

• Entrance queue is akin to

a POSIX recursive mutex

• Wait queue is akin to a

POSIX condition variable

• The implementation in the Oracle JDK uses lower-level locking primitives

Java Built-in Waiting & Notification Mechanisms

See github.com/JetBrains/jdk8u_hotspot/blob/ master/src/share/vm/runtime/objectMonitor.cpp

(27)

27

End of Java Monitor Object:

Coordination Methods

References

Related documents

Vantage Safety offers repair and maintenance services for a wide variety of gas detection and monitoring devices. We operate a 10,000-square-foot repair facility in

Inspired by the necessity of synthetic plant point cloud datasets for 3D plant phenotyping applications, we propose a general approach to sample points on the surface of virtual

1. The provision of assistance in accordance with the European Ground Handling Directive. Airlines will be able to self handle or choose an alternative service

Second, to the extent that a direct analogy to “time, place, and manner” regulations is appropriate, most—I would say almost all— regulations of weapons are indeed such

The 5 previous papers are all examples of where patient care and quality of life could be greatly improved if nutrition were given a higher priority:

Online media has an important role that lies in its ability to present news about the development of the society which could affect modern life at this time (Nguyen,

Predictive Analytics for Better Business Intelligence Data Warehousing ETL OLAP Data Mining Oracle 11 Oracle 11 g g DB DB Statistics Synopsis?. • Oracle has delivered on a strategy

 Peasants were scared that govt would seize their Peasants were scared that govt would seize their land because they couldn’t pay land because they couldn’t pay. their mortgages