Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007.

Slides:



Advertisements
Similar presentations
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Effective Java: Concurrency Last Updated: Fall 2011.
Jan Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Assignment – no class Wednesday All: watch the Google Techtalk “Getting C++ Threads Right” by Hans Boehm at the following link in place of Wednesday’s.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Oh what a tangled web we weave when first to thread we do conceive.
Spring 2010ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking =
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Threads CS 3250 Some of these slides contain material by Professor Chuck Allison.
COMPSCI 230 S2C 2015 Software Design and Construction Lecture 5 of Theme C Locking, blocking, mutex; visibility, consistency.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Internet Software Development Controlling Threads Paul J Krause.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Java Thread and Memory Model
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.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
More on Thread Safety CSE451 Andrew Whitaker. Review: Thread Hazards Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.
The Singleton Pattern (Creational)
System Programming Practical Session 4: Concurrency / Safety.
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:
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.
Java Thread Programming
Healing Data Races On-The-Fly
Classes and Objects.
Design Patterns – Chocolate Factory (from Head First Design Patterns)
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Atomic Operations in Hardware
Atomic Operations in Hardware
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Threads, Concurrency, and Parallelism
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Synchronization Lecture 23 – Fall 2017.
null, true, and false are also reserved.
slides created by Ethan Apter
Multithreading.
Synchronization Issues
More on Thread Safety CSE451 Andrew Whitaker.
slides created by Ethan Apter
Java Concurrency 17-Jan-19.
Simple Classes in Java CSCI 392 Classes – Part 1.
Programs and Classes A program is made up from classes
Java Concurrency.
Java Concurrency.
Lecture 8 Thread Safety.
slides created by Ethan Apter and Marty Stepp
Java Concurrency 29-May-19.
Problems with Locks Andrew Whitaker CSE451.
“The Little Book on Semaphores” Allen B. Downey
CSE 332: Concurrency and Locks
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Threads and concurrency / Safety
Presentation transcript:

Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007

Sun Proprietary/Confidential: Internal Use Only 2 Multi-Threading Primer One famous example. If you completely understand this example you will be well on your way to understanding most multi-threading issues.

Sun Proprietary/Confidential: Internal Use Only 3 Multi-Threading Primer Class Foo { private static int i = -1; public static int getInt() { if(i < 0) { i += 5; } return i; } What's wrong with this code? Answer: ● Nothing if it is single-threaded code. ● Plenty if it is multi-threaded code

Sun Proprietary/Confidential: Internal Use Only 4 Multi-Threading Primer Class Foo { private static int i = -1; public static int getInt() { if(i < 0) { synchronized(Foo.class) { if(i < 0) i += 5; } return i; } This is the infamous Double Checked Lock idiom ● Is it possible for getInt() to return anything other than 4? ● Does this work perfectly in a multi-threaded environment? ● If so – what's the problem with Double Checked Lock?

Sun Proprietary/Confidential: Internal Use Only 5 Multi-Threading Primer Class Foo { private static Foo instance = null; private boolean peace; public boolean getPeace() { return peace; } private Foo() { peace = true;} public static Foo getFoo() { if(instance == null) { synchronized(Foo.class) { if(instance == null) instance = new Foo(); } return instance; } This is the infamous Double Checked Lock idiom again. ● Is it possible for getFoo() to return anything other than a properly constructed Foo? ● Is it possible that more than one Foo instance can ever exist? ● Does this work perfectly in a multi-threaded environment? ● If so – what's the problem with Double Checked Lock?

Sun Proprietary/Confidential: Internal Use Only 6 Multi-Threading Primer class NuclearWeaponManager { public void killEveryoneIfNoPeace() { Foo foo = Foo.getFoo(); if(foo.getPeace() == false) launchAllOutNuclearWar(); } We should all live to see another day as long as Foo.getFoo() absolutely positively for sure returns a properly constructed Foo instance. See the next slide for the final result of the Double Checked Idiom in a multi-threaded environment.

Sun Proprietary/Confidential: Internal Use Only 7 Multi-Threading Primer OOPS!!! !

Sun Proprietary/Confidential: Internal Use Only 8 Multi-Threading Primer The Java Memory Model, like Quantum Mechanics is spooky and not obvious. Java has much much better performance with Multi-Threading than the old days. High Performance always exacts its tribute – which is super-complex code. Luckily we don't need to know this super-complex code, we just need to know a few simple principles

Sun Proprietary/Confidential: Internal Use Only 9 Multi-Threading Primer There are 2 main aspects of writing thread-safe code: ● Mutual Exclusion ● Visibility Things that can only be done by one thread need to be in a synchronized block. The JVM will guarantee that one and only one thread can enter the block at once. There is no guarantee of “seeing” changes to a mutable variable done by one thread in another thread unless all access to the variable is protected by synchronized blocks. E.g. You can not have a thread-safe program if a variable is “written” in a synchronized block and is “read” without synchronization.

Sun Proprietary/Confidential: Internal Use Only 10 Multi-Threading Primer A Quote from our Hero You may hear it said that to improve performance, you should avoid the use of synchronization when reading or writing atomic data. This advice is dangerously wrong. -- Joshua Bloch

Sun Proprietary/Confidential: Internal Use Only 11 Multi-Threading Primer Next Time: What's volatile and why should I care? What's a deadlock? How can I prevent it? What's a livelock? Is that bad?

Sun Proprietary/Confidential: Internal Use Only 12 Multi-Threading Primer Q & A