Today Quiz not yet graded Final report requirements posted More on Multithreading Happens-Before in Java SE-2811 Slide design: Dr. Mark L. Hornick Content:

Slides:



Advertisements
Similar presentations
Concurrency 101 Shared state. Part 1: General Concepts 2.
Advertisements

Week 9, Class 3: Model-View-Controller Final Project Worth 2 labs Happens-Before ( SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
SE3910 Week 2, Class 2 Today Real-Time Systems Embedded Systems Other ??? Week 3 Tuesday Lab Lab start TBA SE-2811 Slide design: Dr. Mark L. Hornick Content:
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
CS2852 Week 8, Class 2 Today Tree terminology Non-Binary and Non-Search Trees Tree Traversals (Remaining slides not yet shown) Tomorrow: Quiz Implementing.
Today TBA Tomorrow Good Friday SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 5, Class 3.
CS2852 Week 2, Class 1 Today Generics (Section 051) Big-O runtime analysis Muddiest Point Lab Quiz Includes writing a method from ArrayList class (See.
Internet Software Development Controlling Threads Paul J Krause.
Today Quiz Multithreading Options Qt socket buffers between threads Quiz today & Tuesday in lab SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr.
Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose.
SE3910 Week 1, Class 2 Today – Introductions Safety Review Policy The course Tomorrow Lab 1, S365 (Purchase Beaglebone black kit BEFORE) Thursday Class.
Today C/C++:.h/.cpp; compiling and linking Fourier Transform (corrected) Nyquist Theorem Audio sampling rates SE-2811 Slide design: Dr. Mark L. Hornick.
Week 3, Day 2: Threads Questions about Threads “Multithreading” in Swing Lab tomorrow: Quiz Lab 3: Threading! SE-2811 Slide design: Dr. Mark L. Hornick.
SE-3910 Real-time Systems Week 5, Class 2 – Lab turn-in page is up! – Use interrupts in a Linux/C environment – Scheduling – Watchdog follow-up Watchdog.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Today Nervous System Response Time SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 3, Class 2.
Today Return Quiz Class Climate Survey Review SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 10, Class.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
CS2852 Week 6, Class 1 Today The run-time stack Writing and proving recursive methods SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS2852 Week 5, Class 2 Today Queue Applications Circular Queue Implementation Testing SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS2852 Week 7, Class 1 Today Binary Search Tree Implementing add Implementing find Return Quiz 4 (second attempt) Both sections are graded SE-2811 Slide.
Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.
Today Final Project “Preview” Real-time concurrency Multithreading Options pthreads qthreads GPIO.h & GPIO.cpp Quiz tomorrow & Tuesday in lab SE-2811 Slide.
CS2852 Week 6, Class 2 Today Class exercise: Implementing a recursive method Binary Search Trees Tomorrow: Quiz at start of lab Implementing a recursive.
Today Return Quiz First true release of final project (Lab 8) Multithreading Options Qt socket buffers between threads SE-2811 Slide design: Dr. Mark L.
Week 9, Class 3: Java’s Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CS2852 Week 3, Class 2 Today Big-O runtime analysis Linked Lists Muddiest Point Lab Quiz Includes writing a method from ArrayList class (See next slide)
Today Signals Nyquist Theorem (revisited) Audio Sampling Latency and Bandwidth Scheduling Theory SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr.
CS2852 Week 2, Class 2 Today Big-O runtime analysis Linked Lists Muddiest Point Lab Quiz Includes writing a method from ArrayList class (See next slide)
Week 7, Class 1: The Command Pattern (cont.) Get Ready for Poll Everywhere Labs 2 & 3 returned Lab 7 due this evening at 11pm Quiz tomorrow at start of.
Today (or Thursday) Qt Thursday Quiz SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 6, Lab.
CS2910 Week 8, Class 2 Today Return Quiz Look at Schedule TCP implementation! Week 8, Monday Quiz on SMTP May include some questions requiring you to interpret.
Week 10, Day 3 Review for the quarter SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Today Quiz not yet graded (Sorry!) More on Multithreading Happens-Before in Java Other langauges with Happens-Before Happens-Before in C++ SE-2811 Slide.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Week 6, Class 3: Composite Swing composites File composites Computer composites SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Week 8, Class 3: Model-View-Controller Final Project Worth 2 labs Cleanup of Ducks Reducing coupling Finishing FactoryMethod Cleanup of Singleton SE-2811.
CS2910 Week 6, Lab Today Dictionaries in Python SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
SE3910 Week 8, Class 2 Week 4 Lab: Please return your graded Lab 4 to me so I can enter it in my gradebook Week 7, Class 2 (Wednesday) Half-Exam 2 Done.
SE3910 Week 8, Class 3 Week 4 Lab: Please return your graded Lab 4 to me so I can enter it in my gradebook Week 9 Lab: Individual demos of working sub-modules.
Java Thread Programming
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Week 2, Day 1: The Factory Method Pattern
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Threads and Memory Models Hal Perkins Autumn 2011
Week 6, Class 2: Observer Pattern
Week 7, Class 1: The Command Pattern (cont.)
Threads and Memory Models Hal Perkins Autumn 2009
SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder
Slide design: Dr. Mark L. Hornick
SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder
Polling vs. Interrupts CS2852 4/9/2019
Polling vs. Interrupts CS2852 4/21/2019
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Problems with Locks Andrew Whitaker CSE451.
SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder
Slide design: Dr. Mark L. Hornick
More concurrency issues
Presentation transcript:

Today Quiz not yet graded Final report requirements posted More on Multithreading Happens-Before in Java SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1 SE3910 Week 9, Class 1

We need a “Memory Model” (updated) “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.” /jls-17.html#jls-17.4 SE-2811 Dr.Yoder 2

Happens-Before (review) “If one action happens-before another, then the first is visible to and ordered before the second.” “Happens-Before Does Not Imply Happening Before” “Happening Before Does Not Imply Happens- Before” /jls-17.html#jls before-relation/ SE-2811 Dr. Yoder 3

“Happening Before Does Not Imply Happens-Before” void publishMessage() { answer = 42; isReady = true; } void consumeMessage() { if (isReady) sout(answer); } 2/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-2811 Dr.Yoder 4

Some happens-before relationships Every action in a thread Exit synch. section Write to volatile field Any action avase/8/docs/api/java/ut il/concurrent/package- summary.html#Memory Visibility actions later in program order in same thread Enter synch. section locked on same object (later in time) Read of volatile field (later in time) Any action which happens before an action which … happens before this action (chaining) SE-2811 Dr. Yoder 5

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-2811 Dr.Yoder 6

Data-race free programs 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 This is nice, but there can still be problems E.g. single-locked singleton SE-2811 Dr.Yoder 7

Slow program… (review) synchronized(MySingleton.class) { if(theInstance == null) { theInstance == new MySingleton(…); } This program WILL work correctly. Just has a lock that we want to avoid. SE-2811 Dr.Yoder 8

Data-race free program with problems… (review) if(theInstance == null) { synchronized(MySingleton.class) { theInstance == new MySingleton(…); } There is a data rate here, but not one according to the Java Language Spec definition of a data race. The spec. does discuss this sort of problem (§17.4.3)§ SE-2811 Dr.Yoder 9

The double-locked Singleton (Review) if(theInstance == null) { synchronized(MySingleton.class) { if(theInstance == null){ theInstance = new MySingleton(); } return theInstance; SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 10

The faster double-locked Singleton (new!!!) MySingleton 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. Hornick Content: Dr. Hornick Errors: Dr. Yoder 11

Ex. public void a() { System.out.println(“x”); System.out.println(“y”); } public void b() { System.out.println(“x”); System.out.println(“y”); } SE-2811 Dr.Yoder 12 Prove the following code is(n’t) free of data races:

Ex. public void a() { x = 5; System.out.println(“x”); } public void a() { x = 5; System.out.println(“x”); } SE-2811 Dr.Yoder 13 Prove the following code is(n’t) free of data races:

Ex. public void a() { synchronized { x = 5; System.out.println(“x”); } SE-2811 Dr.Yoder 14 Prove the following code is(n’t) free of data races:

Ex. if(theLogger==null){ synchronized (EventLogger.class){ if(theLogger == null){ theLogger = new EventLogger(path); } SE-2811 Dr.Yoder 15 Write whether this code contains any data races. Explain your answer. Assume loggers is not volatile.

Ex. if(theLogger==null){ synchronized (EventLogger.class){ if(theLogger == null){ theLogger = new EventLogger(path); } SE-2811 Dr.Yoder 16 Write whether this code contains any data races. Explain your answer. Assume loggers is volatile.

Ex. if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ Logger logger = new EventLogger(path); loggers.put(path, logger); } SE-2811 Dr.Yoder 17 Write whether this code contains any data races. Explain your answer. Assume loggers is not thread safe.

Ex. if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ Logger logger = new EventLogger(path); loggers.put(path, logger); } SE-2811 Dr.Yoder 18 Write whether this code contains any data races. Explain your answer. Assume loggers is thread safe.

Volatile caveat 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 19

Exercise: Determine if this implementation of the double- locked Singleton is correct. Explain. (See next slide) if(loggers.get(path)==null){ synchronized (loggers){ if(loggers.get(path) == null){ EventLogger n = new EventLogger(path); } SE-2811 Dr.Yoder 20

Exercise Loggers 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-2811 Dr.Yoder 21

An alternative: java.util.concurrent Lock-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-2811 Dr. Yoder 22

A set of operations that happen all at once; they cannot be interrupted if(theInstance == null) { theInstance = new MySingleton(); } Atomic SE-2811 Dr.Yoder 23 Example: Should be atomic

Muddiest Point Wait for the slides, or follow this link to answer both questions at once: SE-2811 Dr.Yoder 24

SE-2811 Dr. Josiah Yoder 25

SE-2811 Dr. Josiah Yoder 26

References EBB: Derek Malloy, Exploring Beaglebone, Wiley, 2015 RTS: Laplante and Ovaska, Real-Time Systems Design and Analysis by, Fourth Edition, Wiley, 2012 SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 27

Someday soon Late next week? Real-Time Operating Systems What is a RTOS? How does it relate to the rest? OS roles Interrupts and the OS Definitions, Flowchart, Timing diagram Detailed steps Scheduling Task states & pre-runtime vs runtime SE-2811 Dr.Yoder 28

Task States Wiki:Process (computing) See also Laplante and Ovaske 4E p. 97 SE-2811 Dr.Yoder 29