38
Java Concurrent Collections: ConcurrentHashMap & BlockingQueue Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

Java Concurrent Collections:

ConcurrentHashMap & BlockingQueue

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

2

Learning Objectives in this Lesson• Understand the capabilities of Java’s

concurrent collections

• Recognize the capabilities of Java’s ConcurrentHashMap & BlockingQueue

Page 3: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

3

Overview of JavaConcurrentHashMap

Page 4: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

4

Overview of Java ConcurrentHashMap• Provides efficient concurrent operations

on key/value pairs via OO & functional programming APIs

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html

Page 5: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

5

Overview of Java ConcurrentHashMap• Provides efficient concurrent operations

on key/value pairs via OO & functional programming APIs

• A highly-optimized “associative array”

• Cannot contain duplicate keys

• i.e., each key maps to at most one value

See en.wikipedia.org/wiki/Associative_array

Page 6: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

6

Overview of Java ConcurrentHashMap• Implemented as a hash table

See en.wikipedia.org/wiki/Hash_table

Page 7: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

7

Overview of Java ConcurrentHashMap• Implemented as a hash table

• Insert & retrieve data elements by key

Map<String, Integer> map

= new ConcurrentHashMap<>();

initializeMap(map);

// Thread T1

map.put("key1", 42);

// Thread T2

Integer value = map.get("key1");

K & V K & V K & V

K & V K & V K & V K & V

ConcurrentHashMap

K & V

Array Linked lists/trees

Page 8: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

8

Overview of Java ConcurrentHashMap• Implemented as a hash table

• Insert & retrieve data elements by key

put() in thread T1 must “happen-before” get() in thread T2

See earlier lesson on “Java Happens-Before Relationships”

K & V K & V K & V

K & V K & V K & V K & V

ConcurrentHashMap

Map<String, Integer> map

= new ConcurrentHashMap<>();

initializeMap(map);

// Thread T1

map.put("key1", 42);

// Thread T2

Integer value = map.get("key1");

K & V

Array Linked lists/trees

Page 9: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

9

Overview of Java ConcurrentHashMap• Implemented as a hash table

• Insert & retrieve data elements by key

• Two items that hash to same locationin the array are placed in linked list

map.put("key2", 1066);

K & V K & V K & V

K & V K & V K & V K & V

ConcurrentHashMap

K & V K & V

Array Linked lists/trees

Page 10: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

10

Overview of Java ConcurrentHashMap• Implemented as a hash table

• Insert & retrieve data elements by key

• Two items that hash to same locationin the array are placed in linked list

• In Java 8+, a linked list is replaced by a binary tree when # of elements in a bucket reaches certain threshold

See www.nagarro.com/en/blog/post/24/performance-improvement-for-hashmap-in-java-8

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 11: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

11

• Optimized for multi-core CPUs

Overview of Java ConcurrentHashMap

See www.ibm.com/developerworks/library/j-jtp08223

Page 12: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

12

Overview of Java ConcurrentHashMap

These segment locks minimize contention

See codepumpkin.com/hashtable-vs-synchronizedmap-vs-concurrenthashmap

• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 13: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

13

Overview of Java ConcurrentHashMap

See codepumpkin.com/hashtable-vs-synchronizedmap-vs-concurrenthashmap

• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

There are common human known uses of this approach!

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 14: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

14

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 15: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

15

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 16: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

16

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists (or trees)

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 17: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

17

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists

• Reads & writes to same list areoptimized to avoid locking

Array Linked lists/trees

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Page 18: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

18

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists

• Reads & writes to same list areoptimized to avoid locking, e.g.,

• Atomic add to head of list

Array Linked lists/trees

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Page 19: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

19

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists

• Reads & writes to same list areoptimized to avoid locking, e.g.,

• Atomic add to head of list

• Remove from list by setting datafield to null, rebuild list to skip this cell

• Unreachable cells are eventually garbage collected

Array Linked lists/trees

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Page 20: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

20

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists

• Reads & writes to same list areoptimized to avoid locking

• Can be modified during iteration

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 21: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

21

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists

• Reads & writes to same list areoptimized to avoid locking

• Can be modified during iteration, e.g.

• Entire map isn’t locked

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 22: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

22

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists

• Reads & writes to same list areoptimized to avoid locking

• Can be modified during iteration, e.g.

• Entire map isn’t locked

• ConcurrentModificationException isn’t thrown

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 23: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

23

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Its methods allow read/write operations with minimal locking, e.g.

• Reads are concurrent since list cellsare immutable (except for data field)

• Reads & writes are concurrent if theyoccur in different lists

• Reads & writes to same list areoptimized to avoid locking

• Can be modified during iteration, e.g.

• Entire map isn’t locked

• ConcurrentModificationException isn’t thrown

• However, changes may not be visible immediately

K & V K & V K & V

ConcurrentHashMap

K & V K & V

K & V

K & V

K & V

K & V

K & V

K & V

Array Linked lists/trees

Page 24: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

24

Overview of Java ConcurrentHashMap

See codepumpkin.com/hashtable-vs-synchronizedmap-vs-concurrenthashmap

This single lock may cause contention

• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Conversely, a SynchronizedMap only uses a single lock

Array Linked lists

K & V K & V K & V

SynchronizedMap

K & V K & V

K & V K & V K & V K & V

Page 25: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

25

Overview of Java ConcurrentHashMap

See codepumpkin.com/hashtable-vs-synchronizedmap-vs-concurrenthashmap

There are also common human known uses of this approach!

• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Conversely, a SynchronizedMap only uses a single lock

Array Linked lists

K & V K & V K & V

SynchronizedMap

K & V K & V

K & V K & V K & V K & V

Page 26: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

26

Overview of Java ConcurrentHashMap• Optimized for multi-core CPUs

• It uses a group of locks (16 in all), eachguarding a subset of the hash table

• Conversely, a SynchronizedMap only uses a single lock

• ConcurrentHashMaps are thus much more scalable than SynchronizedMaps

public void main(String[] argv){

...

runTest(maxIterations,

new ConcurrentHashMap());

runTest(maxIterations,

Collections.synchronizedMap(new HashMap<>));

...

}

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex9

Page 27: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

27

Overview of Java ConcurrentHashMap

See dig.cs.illinois.edu/papers/checkThenAct.pdf

Memoizer

computeIfAbsent(pC1)

computeIfAbsent(pC1)

computeIfAbsent(pC2)

computeIfAbsent(pC1)

Only one computation per key is performed even if multiple threads call computeIfAbsent() using the same key

• Provides “atomic get-and-maybe-set”methods

Page 28: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

28

• Provides “atomic get-and-maybe-set”methods, e.g.

• If key isn’t already associated w/a value, compute its value using the given function & enter it into map

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent

Overview of Java ConcurrentHashMapUse

return map.computeIfAbsent

(key,

k -> mappingFunc(k)));

instead of

V value = map.get(key);

if (value == null) {

value =

mappingFunc.apply(key);

if (value != null)

map.put(key, value);

}

return value;

Page 29: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

29

• Provides “atomic get-and-maybe-set”methods, e.g.

• If key isn’t already associated w/a value, compute its value using the given function & enter it into map

• If a key isn’t already associated w/a value, associate it with the value

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#putIfAbsent

Overview of Java ConcurrentHashMapUse

return map.putIfAbsent

(key, value);

instead of

V value = map.get(key);

if (value == null)

return map.put(key, value);

else

return value;

Page 30: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

30

• Provides “atomic get-and-maybe-set”methods, e.g.

• If key isn’t already associated w/a value, compute its value using the given function & enter it into map

• If a key isn’t already associated w/a value, associate it with the value

• Replaces entry for a key only if currently mapped to some value

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#replace

Overview of Java ConcurrentHashMapUse

return map.replace(key, value);

instead of

if (map.containsKey(key))

return map.put(key, value);

else

return null;

Page 31: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

31

• Provides “atomic get-and-maybe-set”methods, e.g.

• If key isn’t already associated w/a value, compute its value using the given function & enter it into map

• If a key isn’t already associated w/a value, associate it with the value

• Replaces entry for a key only if currently mapped to some value

• Replaces entry for a key only if currently mapped to given value

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#replace

Overview of Java ConcurrentHashMapUse

return map.replace(key,

oldValue,

newValue);

instead of

if (map.containsKey(key) &&

Objects.equals(map.get(key),

oldValue)) {

map.put(key, newValue);

return true;

} else

return false;

Page 32: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

32

Overview of JavaBlockingQueue

Page 33: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

33

Overview of Java BlockingQueue• A Queue supporting operations with certain properties

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html

Page 34: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

34

Overview of Java BlockingQueue• A Queue supporting operations with certain properties• wait for the queue to become non-empty when retrieving an element &

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html

Page 35: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

35

Overview of Java BlockingQueue• A Queue supporting operations with certain properties• wait for the queue to become non-empty when retrieving an element & • wait for space to become available in queue when storing an element

See docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html

Page 36: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

36

Overview of Java BlockingQueue• When adding to a full queue or retrieving from an empty queue clients can

either block indefinitely, timeout after waiting for a designated time, or poll

put() take()

Page 37: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

37

Overview of Java BlockingQueue• Many BlockingQueue implementations use

Java ReentrantLock & ConditionObjects

usesuses2

ArrayBlocking

Queue

put()take()

Lock

lock()unlock()

uses

take() put()

Consumer Producer

ConditionVariable

await()signal()signalAll()

See earlier lessons on “Java ReentrantLock” & “Java ConditionObject” for examples

Page 38: Java Concurrent Collections: ConcurrentHashMap & BlockingQueueschmidt/cs891s/2020-PDFs/7.3.2...blocking… · 2 Learning Objectives in this Lesson •Understand the capabilities of

38

End of Java Concurrent Collections: ConcurrentHash

Map & BlockingQueue