• No results found

Java ConditionObject Usage Considerations

N/A
N/A
Protected

Academic year: 2021

Share "Java ConditionObject Usage Considerations"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)

Java ConditionObject

Usage Considerations

Douglas C. Schmidt

[email protected]

www.dre.vanderbilt.edu/~schmidt

(2)

2

• Understand what condition variables are • Note a human known use of condition

variables

• Know what pattern they implement • Recognize common use cases where

condition variables are applied

• Recognize the structure & functionality of Java ConditionObject

• Know the key methods defined by the Java ConditionObject class

• Master the use of ConditionObjects in practice

• Appreciate ConditionObject usage considerations

(3)

3

(4)

4

• ConditionObject is a highly flexible synchronization mechanism

Java ConditionObject Usage Considerations

T2 Critical Section T3 lock Cond

Obj1 CondObj2

(5)

5

• ConditionObject is a highly flexible synchronization mechanism • Allows threads to cooperatively suspend & resume

their execution based on shared state

Critical

Section

T3

lock

Cond

Obj1 CondObj2

T1

Java ConditionObject Usage Considerations

T2

e.g., threads T1 & T2 can take turns sharing a critical section

(6)

6

• ConditionObject is a highly flexible synchronization mechanism • Allows threads to cooperatively suspend & resume

their execution based on shared state

Critical

Section

T3

lock

Cond

Obj1 CondObj2

T1 T2

Java ConditionObject Usage Considerations

e.g., threads T1 & T2 can take turns sharing a critical section

(7)

7

• ConditionObject is a highly flexible synchronization mechanism • Allows threads to cooperatively suspend & resume

their execution based on shared state • A user object can define

multiple ConditionObjects Critical Section T3 lock Cond

Obj1 CondObj2

T1 T4

T2

(8)

8

• ConditionObject is a highly flexible synchronization mechanism • Allows threads to cooperatively suspend & resume

their execution based on shared state • A user object can define

multiple ConditionObjects • Each ConditionObject

can provide a separate “wait set” Critical Section T3 lock Cond

Obj1 CondObj2

T1 T4

T2

(9)

9

(10)

10

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop public class ArrayBlockingQueue<E>

... { ...

public E take() ... {

final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == 0) notEmpty.await(); return extract(); } finally { lock.unlock(); } }

(11)

11

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop

• (Re)test state that’s being waited for since it may change due to non-determinism of concurrency See docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html public class ArrayBlockingQueue<E> ... { ... public E take() ... {

final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == 0) notEmpty.await(); return extract(); } finally { lock.unlock(); } }

(12)

12

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop

• (Re)test state that’s being waited for since it may change due to non-determinism of concurrency • Guard against spurious wakeups

See en.wikipedia.org/wiki/Spurious_wakeup public class ArrayBlockingQueue<E> ... { ... public E take() ... {

final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == 0) notEmpty.await(); return extract(); } finally { lock.unlock(); } }

Java ConditionObject Usage Considerations

A thread might be awoken from its waiting state even though

(13)

13

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock

Critical

Section

T3

lock

Cond

Obj1 CondObj2

T1 T4

T2

(14)

14

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Needed to avoid the

“lost wakeup problem”

Critical

Section

T3

lock

Cond

Obj1 CondObj2

T1 T4

T2

Java ConditionObject Usage Considerations

See docs.oracle.com/cd/E19253-01/816-5137/sync-30

• A thread calls signal() or signalAll()

(15)

15

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Needed to avoid the

“lost wakeup problem” • await() internally releases

& reacquires its associated lock! Critical Section T3 lock Cond

Obj1 CondObj2

T1 T4

T2

(16)

16

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Choosing between signal()

& signalAll() can be subtle

(17)

17

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Choosing between signal()

& signalAll() can be subtle • Using signal() is more

efficient & avoids the

“Thundering Herd” problem..

Java ConditionObject Usage Considerations

(18)

18

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Choosing between signal()

& signalAll() can be subtle

Java ConditionObject Usage Considerations

Uniform

waiters Only one condition expression that await() is waiting for is associated with the ConditionObject wait set & each thread executes the same logic when returning from await()

One-in &

one-out A signal() on the ConditionObjectenables at most one thread to proceed

Conditions under which signal() can be used

See earlier discussion in “

Java ConditionObject: Example Application

The implementation of Java ArrayBlockingQueue

(19)

19

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Choosing between signal()

& signalAll() can be subtle

Java ConditionObject Usage Considerations

Uniform

waiters Only one condition expression that await() is waiting for is associated with the ConditionObject wait set & each thread executes the same logic when returning from await()

One-in &

one-out A signal() on the ConditionObjectenables at most one thread to proceed

Conditions under which signal() can be used

public E take() ... { ... while (count == 0) notEmpty.await(); return extract(); ... }

(20)

20

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Choosing between signal()

& signalAll() can be subtle

Uniform

waiters Only one condition expression that await() is waiting for is associated with the ConditionObject wait set & each thread executes the same logic when returning from await()

One-in &

one-out A signal() on the ConditionObjectenables at most one thread to proceed

Java ConditionObject Usage Considerations

Conditions under which signal() can be used

private void insert(E x) { items[putIndex] = x;

putIndex = inc(putIndex); ++count;

notEmpty.signal(); }

(21)

21

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Choosing between signal()

& signalAll() can be subtle

Uniform

waiters Only one condition expression that await() is waiting for is associated with the ConditionObject wait set & each thread executes the same logic when returning from wait()

One-in &

one-out A signal() on the ConditionObjectenables at most one thread to proceed

Java ConditionObject Usage Considerations

Conditions under which signal() can be used

(22)

22

• However, a ConditionObject must be used carefully to avoid problems • It should (almost) always

be waited upon in a loop • It is always used in

conjunction with a lock • Choosing between signal()

& signalAll() can be subtle • ConditionObject inherits the

wait(), notify(), & notifyAll() methods from Java Object!!

Java ConditionObject Usage Considerations

(23)

23

• Name condition object fields to reflect their usage

Java ConditionObject Usage Considerations

Critical

Section

T3

lock

not

Empty Fullnot

T1 T4

T2

Used to wait until the

(24)

24

(25)

25

• ConditionObject is used in java.util.concurrent & java.util.concurrent.locks • However, it’s typically hidden

within higher-level abstractions

(26)

26

• ConditionObject is used in java.util.concurrent & java.util.concurrent.locks • However, it’s typically hidden

within higher-level abstractions • e.g., ArrayBlockingQueue &

LinkedBlockingQueue

Java ConditionObject Usage Considerations

(27)

27

References

Related documents

Compliance monitoring, as well as monitoring to identify potential improvements in the management of environmental, social and cultural heritage issues, is a key component of the

During the RAY 2015 photography triennial, the MMK Museum für Moderne Kunst will present the four finalists of the Deutsche Börse Photography Prize 2015 at the MMK 3: Nikolai

Differences in the process of UCB collection and the fail- ure to type samples by private cord blood banks make it diffi- cult to search for unrelated HLA-matched donors in private

Our previous study showed that ASCT2 (Na + -dependent amino acid transporter (AAT)) mediates fl uciclovine uptake in androgen-dependent PCa cells; its expression is in fl uenced

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,

Information on how payroll taxes affect the three relevant income concepts – labour cost, gross earnings (net of employer payroll taxes) and net earnings (net of employer and

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; (4) if not, whether the claimant’s residual functional capacity permits him to perform his past work; and (5) if not, whether the claimant’s RFC, age, education, and work