java reentrantlock: introductionschmidt/cs891s/2020-pdfs/4.2.1... · 2020. 2. 11. · end of java...

24
Java ReentrantLock: Introduction Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 01-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

Java ReentrantLock:

Introduction

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

2

Learning Objectives in this Part of the Lesson• Understand the concept of mutual

exclusion in concurrent programs

See en.wikipedia.org/wiki/Mutual_exclusion

Waiting threads

Critical Section

Running thread

Page 3: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

3

Learning Objectives in this Part of the Lesson• Understand the concept of mutual

exclusion in concurrent programs

• Note a human-known use of mutualexclusion

Page 4: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

4

Overview of Mutual Exclusion Locks

Page 5: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

5

• A mutual exclusion lock (mutex) defines a “critical section”

Overview of Mutual Exclusion Locks

Critical Section

See en.wikipedia.org/wiki/Critical_section

A critical section is group of instructions or region of code that

must be executed atomically

Page 6: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

6

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

Overview of Mutual Exclusion Locks

Critical Section

Page 7: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

7

Critical Section

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

Overview of Mutual Exclusion Locks

Acquire lock

Page 8: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

8

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

Critical Section

Running thread

Overview of Mutual Exclusion Locks

Page 9: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

9

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

• Other threads are kept “at bay”

• Prevent corruption of shared (mutable) data that can be set/ get by concurrent operations

Critical Section

Running thread

Overview of Mutual Exclusion Locks

Waiting threads

Page 10: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

10

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

• Other threads are kept “at bay”

• Prevent corruption of shared (mutable) data that can be set/ get by concurrent operations

Critical Section

Running thread

Overview of Mutual Exclusion Locks

Waiting threads

Other threads must obey the locking protocol or chaos will ensue!!

Page 11: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

11

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

• Other threads are kept “at bay”

• Prevent corruption of shared (mutable) data that can be set/ get by concurrent operations

• Race conditions could occur if multiple threads run within a critical section

Critical Section

Running threads

Overview of Mutual Exclusion Locks

Waiting threads

See en.wikipedia.org/wiki/Race_condition

Race conditions arise when a program depends on the sequence or timing of threads for it to operate properly

Page 12: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

12

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

• Other threads are kept “at bay”

• After a thread leaves a critical section another thread can enter & start running

Critical Section

Overview of Mutual Exclusion Locks

Release lock

Waiting threads

Page 13: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

13

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

• Other threads are kept “at bay”

• After a thread leaves a critical section another thread can enter & start running

Critical Section

Overview of Mutual Exclusion Locks

Waiting threads

Acquire lock

Page 14: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

14

• A mutual exclusion lock (mutex) defines a “critical section”

• Ensures only one thread can run in a block of code at a time

• Other threads are kept “at bay”

• After a thread leaves a critical section another thread can enter & start running

Critical Section

Overview of Mutual Exclusion Locks

Waiting threads

Running thread

Page 15: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

15

• A mutex is typically implemented in hardware via atomic operations

Overview of Mutual Exclusion Locks

See en.wikipedia.org/wiki/Linearizability

Atomic operations appear to occur instantaneously & either change the state of the system

successful or have no effect

Page 16: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

16

• A mutex is typically implemented in hardware via atomic operations

• Implemented in Java via the compareAndSwap*() methods in the Unsafe class

Overview of Mutual Exclusion Locks

See earlier discussion of “Java Atomic Classes & Operations”

Page 17: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

17

Human Known Use of Mutual Exclusion Locks

Page 18: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

18

• A human known use of mutual exclusion locks is an airplane restroom protocol

Human Known Use of Mutual Exclusion Locks

Page 19: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

19

• A human known use of mutual exclusion locks is an airplane restroom protocol

Human Known Use of Mutual Exclusion Locks

Person can only enter if the restroom is vacant

Page 20: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

20

lock door

• A human known use of mutual exclusion locks is an airplane restroom protocol

Human Known Use of Mutual Exclusion Locks

Person atomically enters & locks the door

Page 21: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

21

lock door

• A human known use of mutual exclusion locks is an airplane restroom protocol

Human Known Use of Mutual Exclusion Locks

Other people who want to use the restroom must wait while it’s in use

Page 22: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

22

unlock door

• A human known use of mutual exclusion locks is an airplane restroom protocol

Human Known Use of Mutual Exclusion Locks

This protocol is “fully-bracketed,” i.e., person who locks must be the same as the person who unlocks

Page 23: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

23

• A human known use of mutual exclusion locks is an airplane restroom protocol

Human Known Use of Mutual Exclusion Locks

Once the restroom is vacant another person can enter

Page 24: Java ReentrantLock: Introductionschmidt/cs891s/2020-PDFs/4.2.1... · 2020. 2. 11. · End of Java ReentrantLock: Introduction. Title: Learning Objectives in this Part of the Module

24

End of Java ReentrantLock: Introduction