Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of...
Transcript of Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of...
![Page 1: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/1.jpg)
Overview of Concurrent
Programming in Java
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Professor of Computer Science
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/2.jpg)
2
Learning Objectives in this Part of the Lesson• Understand the meaning of key
concurrent programming concepts
• Recognize how these concepts are supported in Java
Each Java thread has its own stack,
registers, etc.
![Page 3: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/3.jpg)
3
An Overview of Concurrent Programming in Java
![Page 4: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/4.jpg)
4
An Overview of Concurrent Programming in Java• A Java thread is an object
See docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
![Page 5: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/5.jpg)
5
An Overview of Concurrent Programming in Java• A Java thread is an object, e.g.
• It contains methods & fields
See blog.jamesdbloom.com/JVMInternals.html
Each Java thread has its own stack,
registers, etc.
![Page 6: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/6.jpg)
6
An Overview of Concurrent Programming in Java• A Java thread is an object, e.g.
• It contains methods & fields
• It can also be in one of various “states”
See docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html
![Page 7: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/7.jpg)
7
An Overview of Concurrent Programming in Java• Concurrent Java threads interact via shared objects and/or message passing
See docs.oracle.com/javase/8/docs/api/?java/util/concurrent/package-summary.html
write()
read()
send()
recv()
read()
![Page 8: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/8.jpg)
8
• Concurrent Java threads interact via shared objects and/or message passing
• Shared objects
• Synchronize concurrent operations on objects to ensure certain properties
An Overview of Concurrent Programming in Java
write()
read()
send()
read()
recv()
See en.wikipedia.org/wiki/Synchronization_(computer_science)
![Page 9: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/9.jpg)
9
• Concurrent Java threads interact via shared objects and/or message passing
• Shared objects
• Synchronize concurrent operations on objects to ensure certain properties, e.g.
• Mutual exclusion
• Interactions between threads won’t corrupt shared mutable data
An Overview of Concurrent Programming in Java
write()
read()
send()
read()
recv()
See en.wikipedia.org/wiki/Monitor_(synchronization)#Mutual_exclusion
![Page 10: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/10.jpg)
10
• Concurrent Java threads interact via shared objects and/or message passing
• Shared objects
• Synchronize concurrent operations on objects to ensure certain properties, e.g.
• Mutual exclusion
• Coordination
• Operations occur in the right order, at the right time, & under the right conditions
An Overview of Concurrent Programming in Java
write()
read()
send()
read()
recv()
See en.wikipedia.org/wiki/Monitor_(synchronization)#Condition_variables
![Page 11: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/11.jpg)
11
An Overview of Concurrent Programming in Java• Concurrent Java threads interact via shared objects and/or message passing
• Shared objects
• Synchronize concurrent operations on objects to ensure certain properties
• Examples of Java synchronizers:
• Synchronized statements/methods
• Reentrant locks & intrinsic locks
• Atomic operations
• Semaphores
• Condition objects
• “Compare-and-swap” (CAS)operations in sun.misc.unsafe
See dzone.com/articles/the-java-synchronizers
write()
read()
send()
read()
recv()
![Page 12: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/12.jpg)
12
An Overview of Concurrent Programming in Java• Concurrent Java threads interact via shared objects and/or message passing
• Shared objects
• Message passing
• Send message(s) from producerthread(s) to consumer thread(s)via a thread-safe queue
See en.wikipedia.org/wiki/Message_passing
write()
read()
read()
send()
recv()
![Page 13: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/13.jpg)
13
An Overview of Concurrent Programming in Java• Concurrent Java threads interact via shared objects and/or message passing
• Shared objects
• Message passing
• Send message(s) from producerthread(s) to consumer thread(s)via a thread-safe queue
• Examples of Java thread-safe queues
• Array & linked blocking queues
• Priority blocking queue
• Synchronous queue
• Concurrent linked queue
See docs.oracle.com/javase/tutorial/collections/implementations/queue.html
write()
read()
read()
send()
recv()
![Page 14: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/14.jpg)
14
An Overview of Concurrent Programming Hazards
![Page 15: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/15.jpg)
15
• Java shared objects & message passing are designed to share resources safely & avoid concurrency hazards
An Overview of Concurrent Programming Hazards
See en.wikipedia.org/wiki/Thread_safety
![Page 16: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/16.jpg)
16See en.wikipedia.org/wiki/Race_condition#Software
• Java shared objects & message passing are designed to share resources safely & avoid concurrency hazards, e.g.
• Race conditions
• Race conditions occur when a program depends upon the sequence or timing of threadsfor it to operate properly
An Overview of Concurrent Programming Hazards
write()
read()
![Page 17: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/17.jpg)
17
An Overview of Concurrent Programming Hazards
See github.com/douglascraigschmidt/LiveLessons/tree/master/BuggyQueue
This test program induces race conditions due to lack of synchronization between producer & consumer threads accessing a bounded queue
write()
read()
• Java shared objects & message passing are designed to share resources safely & avoid concurrency hazards, e.g.
• Race conditions
• Race conditions occur when a program depends upon the sequence or timing of threadsfor it to operate properly
![Page 18: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/18.jpg)
18
An Overview of Concurrent Programming Hazards• Java shared objects & message passing
are designed to share resources safely & avoid concurrency hazards, e.g.
• Race conditions
• Memory inconsistencies
• These errors occur when different threads have inconsistent views ofwhat should be the same data
See jeremymanson.blogspot.com/2007/08/atomicity-visibility-and-ordering.html
![Page 19: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/19.jpg)
19
An Overview of Concurrent Programming Hazards• Java shared objects & message passing
are designed to share resources safely & avoid concurrency hazards, e.g.
• Race conditions
• Memory inconsistencies
• Deadlocks
• Occur when 2+ competing threads are waiting for the other(s) to finish, & thus none ever do
T2T1
<<owns>>
<<owns>> <<needs>>
<<needs>>
L1
L2
See en.wikipedia.org/wiki/Deadlock
![Page 20: Overview of Concurrent Programming in Javaschmidt/cs891s/2020-PDFs/1.2.2...13 An Overview of Concurrent Programming in Java •Concurrent Java threads interact via shared objects and/or](https://reader033.fdocuments.net/reader033/viewer/2022053000/5f04b1d47e708231d40f3e49/html5/thumbnails/20.jpg)
20
End of Overview of Concurrent Programming
in Java