week 9, class 3: javas happens-before memory model (slides used and skipped in class) se-2811 slide...
DESCRIPTION
Why is this OK? A = B+1; B = 1; tmp = B; A = tmp + 1; B = 1; SE-2811 Dr.Yoder 3 SAMETRANSCRIPT
![Page 1: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/1.jpg)
Week 9, Class 3:Java’s Happens-Before Memory Model (Slides used and skipped in class)
SE-2811Slide design: Dr. Mark L. Hornick
Content: Dr. HornickErrors: Dr. Yoder
1
![Page 2: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/2.jpg)
Surprising Fact: Compiler can re-order instructions
When compiled, A = B+1; B = 1;May be executed as tmp = B; B = 1; tmp++; A = tmp;Source: Preshing (see notes for this slide)SE-2811
Dr.Yoder 2
![Page 3: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/3.jpg)
Why is this OK? A = B+1; B = 1;
tmp = B;A = tmp + 1;B = 1;
SE-2811Dr.Yoder 3
SAME
![Page 4: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/4.jpg)
Why is this OK? A = B+1; B = 1;
tmp = B;tmp++;A = tmp;B = 1;
SE-2811Dr.Yoder 4
SAME
![Page 5: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/5.jpg)
Why is this OK? A = B+1; B = 1;
tmp = B;tmp++;A = tmp;B = 1;
SE-2811Dr.Yoder 5
SAMEtmp = B;B = 1; tmp++;A = tmp;
SAME
![Page 6: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/6.jpg)
Why does the compiler do this?
Machine instructions are like this.tmp = B;
tmp++;A = tmp;B = 1;
Exercise: What machine instructions are these?6
![Page 7: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/7.jpg)
Why does the compiler do this?
We can read and write faster to “tmp”tmp = B;B = 1;tmp++;A = tmp;
Exercise: What is tmp? Why can we read and write faster to it?
SE-2811Dr.Yoder 7
![Page 8: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/8.jpg)
Result: Seemingly impossible things can happenA = 0;…r1 = A;B = 2;
B = 0;…r2 = B;A = 1;
SE-2811Dr.Yoder 8
Exercise: How could we get r1 = 1 and r2 = 0?Java Lang. Spec Example 17.4-1.
![Page 9: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/9.jpg)
It is possible for both r1 = 1 and r2 = 2!A = 0;…r1 = A;B = 2;
B = 0;…A = 1;r2 = B;
SE-2811Dr.Yoder 9
Exercise: How could we get r1 = 1 and r2 = 2?(Would this be possible without reordering?)Java Lang. Spec Example 17.4-1.
![Page 10: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/10.jpg)
How to ensure multithreaded applications work as expected? Avoid problems due to
reordering caching (as seen yesterday)
While still being fast!
SE-2811Dr. Yoder 10
![Page 11: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/11.jpg)
We need a “Memory Model”
“The Java programming language memory model works by examining each read in an execution trace and checking that the write observed by that read is valid according to certain rules.”http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4
SE-2811Dr.Yoder 11
![Page 12: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/12.jpg)
Shared VariablesShared instance fields static fields array elements
Never shared local variables method parameters exception parameters e.g. catch(Exception e)
SE-2811Dr.Yoder 12
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.1
![Page 13: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/13.jpg)
Happens-Before
“If one action happens-before another, then the first is visible to and ordered before the second.”
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.1 SE-2811
Dr. Yoder 13
![Page 14: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/14.jpg)
Important caveat:
“An implementation is free to produce any code it likes, as long as all resulting executions of a program produce a result that can be predicted by the memory model.”
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4
SE-2811Dr.Yoder 14
![Page 15: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/15.jpg)
Some happens-before relationships “Each action in a thread happens-
before every action in that thread that comes later in the program's order.
“An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor.
“A write to a volatile field happens-before every subsequent read of that same field. SE-2811
Dr. Yoder 15
![Page 16: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/16.jpg)
Some happens-before relationships “A call to start on a thread happens-
before any action in the started thread. “All actions in a thread happen-before any
other thread successfully returns from a join on that thread.”
http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility
SE-2811Dr.Yoder 16
![Page 17: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/17.jpg)
“Happens-Before Does Not Imply Happening Before”
“It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results consistent with a legal execution, it is not illegal.”
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.5
SE-2811Dr.Yoder 17
![Page 18: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/18.jpg)
“Happening Before Does Not Imply Happens-Before”
“More specifically, if two actions share a happens-before relationship, they do not necessarily have to appear to have happened in that order to any code with which they do not share a happens-before relationship. Writes in one thread that are in a data race with reads in another thread may, for example, appear to occur out of order to those reads.”
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.5
SE-2811Dr.Yoder 18
![Page 19: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/19.jpg)
“Happening Before Does Not Imply Happens-Before”
void publishMessage() { answer = 42; // (1) isReady = true; // (2)}
void consumeMessage() { if (isReady) // (3) sout(answer); // (4)}http://preshing.com/20130702/the-happens-before-relation/
http://preshing.com/20130702/the-happens-before-relation/
SE-2811Dr.Yoder 19
![Page 20: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/20.jpg)
“Happening Before Does Not Imply Happens-Before”void publishMessage() { answer = 42; isReady = 1; }
void consumeMessage() { if (isReady) sout("%d\n", answer);}http://preshing.com/20130702/the-happens-before-relation/
Exercise: How might this code execute to print 0 instead of 42?
(You may use reordering and/or caching in your solution)
SE-2811Dr.Yoder 20
![Page 21: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/21.jpg)
Happens-Before
“If one action happens-before another, then the first is visible to and ordered before the second.”
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.1 SE-2811
Dr. Yoder 21
![Page 22: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/22.jpg)
Important caveat:
“An implementation is free to produce any code it likes, as long as all resulting executions of a program produce a result that can be predicted by the memory model.”
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4
SE-2811Dr.Yoder 22
![Page 23: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/23.jpg)
“Happening Before Does Not Imply Happens-Before”void publishMessage() { answer = 42; isReady = 1; }
void consumeMessage() { if (isReady) printf("%d\n", answer);}http://preshing.com/20130702/the-happens-before-relation/
http://preshing.com/20130702/the-happens-before-relation/
Exercise: How might this code execute to print 0 instead of 42?
(You may use reordering and/or caching in your solution)
SE-2811Dr.Yoder 23
![Page 24: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/24.jpg)
Some happens-before relationships “Each action in a thread happens-
before every action in that thread that comes later in the program's order.
“An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor.
“A write to a volatile field happens-before every subsequent read of that same field. SE-2811
Dr. Yoder 24
![Page 25: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/25.jpg)
Some happens-before relationships “A call to start on a thread happens-
before any action in the started thread. “All actions in a thread happen-before any
other thread successfully returns from a join on that thread.”
http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility
SE-2811Dr.Yoder 25
![Page 26: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/26.jpg)
Some definitions
“Two accesses to (reads of or writes to) the same variable are said to be conflicting if at least one of the accesses is a write.” (§17.4.1)
“When a program contains two conflicting accesses that are not ordered by a happens-before relationship, it is said to contain a data race.” (§17.4.5)
SE-2811Dr.Yoder 26
![Page 27: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/27.jpg)
What is a “Memory Model”?
“A memory model describes, given a program and an execution trace of that program, whether the execution trace is a legal execution of the program.”http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4
SE-2811Dr.Yoder 27
![Page 28: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/28.jpg)
Total and Partial OrderPartial Order One Total Order
SE-2811Dr.Yoder 28
![Page 29: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/29.jpg)
Total and Partial OrderPartial Order One Total Order
SE-2811Dr.Yoder 29
![Page 30: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/30.jpg)
Total and Partial OrderPartial Order Another Total Order
SE-2811Dr.Yoder 30
![Page 31: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/31.jpg)
Total and Partial OrderEach thread has a Total
Order
SE-2811Dr.Yoder 31
![Page 32: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/32.jpg)
My conclusion If a program has no data races, then we can
treat that program as if it behaved according to our simple “switch-back-and-forth-between-threads” model which we used to illustrate multithreading at the beginning of the class.
SE-2811Dr.Yoder 32
![Page 33: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/33.jpg)
Caveat
“Sequential consistency and/or freedom from data races still allows errors arising from groups of operations that need to be perceived atomically and are not.” (§17.4.3)
SE-2811Dr.Yoder 33
![Page 34: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/34.jpg)
A set of operations that happen all at once; they cannot be interrupted
if(theInstance == null) { theInstance = new MySingleton();}
Atomic
SE-2811Dr.Yoder 34
Example: Should be atomic
![Page 35: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/35.jpg)
The double-locked Singleton
if(theInstance == null) { synchronized(MySingleton.class) { if(theInstance == null){ theInstance = new MySingleton(); } }}return theInstance;
SE-2811Slide design: Dr. Mark L. Hornick
Content: Dr. HornickErrors: Dr. Yoder
35
![Page 36: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/36.jpg)
The faster double-locked SingletonMySingleton local = theInstance;if(local == null) { synchronized(MySingleton.class) { local = theInstance; if(local == null) { local = theInstance = new MySingleton(); } }}return local; SE-2811
Slide design: Dr. Mark L. HornickContent: Dr. Hornick
Errors: Dr. Yoder
36
![Page 37: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/37.jpg)
DO VARIABLES STORE OBJECTS?
You may be wondering…
SE-2811Dr.Yoder 37
![Page 38: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/38.jpg)
Volatile caveat “A write to a volatile field happens-
before every subsequent read of that same field.”
A field is an instance variable or a static variable
A variable stores either a primitive type or a reference type
A variable does not store an object.
SE-2811Dr. Yoder 38
![Page 39: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/39.jpg)
What does this mean? Although writes to references are protected
by volatile, writes to objects are not. If you want a volatile object, you have to declare
all its fields volatile (not recommended) Even then…
You can’t do this for objects in the standard library Your class may still have parts that need to be atomic
but are not If you are calling a method that is not
designed to be used without synchronization, you should synchronize around it.SE-2811
Dr.Yoder 39
![Page 40: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/40.jpg)
Exercise: Determine if this implementation of the double-locked Singleton is correct. Explain.if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ EventLogger n = new EventLogger(path); } }}
SE-2811Dr.Yoder 40
![Page 41: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/41.jpg)
ExerciseLoggers is a map.
If this map is not designed to be used from multiple threads, and we run this program, it isn’t safe. Explain what might go wrong.
if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ EventLogger n = new EventLogger(path); } }}
SE-2811Dr.Yoder 41
![Page 42: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/42.jpg)
An alternative: java.util.concurrentLock-free multi-threaded data structures: ConcurrentHashMap
Like HashMap, only “concurrent” ConcurrentSkipListMap
Like TreeMap, only “concurrent” Discussed in Dean & Dean
SE1011 book recommended by a student for studying data
structures
SE-2811Dr. Yoder 42
![Page 43: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/43.jpg)
The rigormeter is at 2 today
https://www.rose-hulman.edu/~bryan/images/rigor.htmlSE-2811Dr.Yoder 43
![Page 44: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/44.jpg)
Ex.
public void a() { System.out.println(“x”); System.out.println(“y”);
}
public void b() { System.out.println(“x”); System.out.println(“y”);
}
SE-2811Dr.Yoder 44
Prove the following code is(n’t) free of data races:
![Page 45: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/45.jpg)
Ex.
public void a() { x = 5; System.out.println(“x”);
}
public void a() { x = 5; System.out.println(“x”);
}
SE-2811Dr.Yoder 45
Prove the following code is(n’t) free of data races:
![Page 46: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/46.jpg)
Ex.
public void a() { synchronized { x = 5; System.out.println(“x”); }}
SE-2811Dr.Yoder 46
Prove the following code is(n’t) free of data races:
![Page 47: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/47.jpg)
Ex.
if(theLogger==null){ synchronized (EventLogger.class){ if(theLogger == null){ theLogger = new EventLogger(path); } }}
SE-2811Dr.Yoder 47
Write whether this code contains any data races. Explain your answer. Assume loggers is not volatile.
![Page 48: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/48.jpg)
Ex.
if(theLogger==null){ synchronized (EventLogger.class){ if(theLogger == null){ theLogger = new EventLogger(path); } }}
SE-2811Dr.Yoder 48
Write whether this code contains any data races. Explain your answer. Assume loggers is volatile.
![Page 49: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/49.jpg)
Ex.
if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ Logger logger = new EventLogger(path); loggers.put(path, logger); } }}
SE-2811Dr.Yoder 49
Write whether this code contains any data races. Explain your answer. Assume loggers is not thread safe.
![Page 50: Week 9, Class 3: Javas Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:](https://reader034.vdocument.in/reader034/viewer/2022050804/5a4d1b897f8b9ab0599be192/html5/thumbnails/50.jpg)
Ex.
if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ Logger logger = new EventLogger(path); loggers.put(path, logger); } }}
SE-2811Dr.Yoder 50
Write whether this code contains any data races. Explain your answer. Assume loggers is thread safe.