Oh what a tangled web we weave...... when first to thread we do conceive.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Ch 7 B.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
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.
In Sync (No, not the lame boy band.). Today in history... Last time: Thinking about design: interface design Survey of code complexity: P1 vs. P3 Intro.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
The Joy of Synchronization Lecture 19, Nov 5. Administrivia Reminder: Schedule P3M1 with us Only 3 groups scheduled so far… Time slots running out...
The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread...
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Oh what a tangled web we weave when first to thread we do conceive.
More Multithreaded Programming in Java David Meredith Aalborg University.
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
!!! Global Variables!!! are EVIL SSimply because you just write a school boy/gal?
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Internet Software Development Controlling Threads Paul J Krause.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Confinement.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Lecture 10: Object Oriented Programming Tami Meredith.
Component-Based Software Engineering Understanding Thread Safety Paul Krause.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007.
Concurrency 2 CS 2110 – Spring 2016.
Threads Chate Patanothai.
Variables and Their scope
More on Thread Safety CSE451 Andrew Whitaker.
Building Java Programs
Dr. Mustafa Cem Kasapbaşı
Java Concurrency 17-Jan-19.
Workshop for Programming And Systems Management Teachers
Building Java Programs
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
Java Concurrency.
Java Concurrency.
Threads and Multithreading
Java Concurrency 29-May-19.
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

Oh what a tangled web we weave when first to thread we do conceive.

History Last time Threads & synchronization This time: Team tips Synchronization, cont’d. Design exercise

Team tip: code ownership Who “owns” a module? Who is responsible for it? Model 1: Strong individual ownership One person is exclusive owner of each module  Knows it inside out  Can easily fix bugs, etc.  Clear-cut lines of responsibility  Each developer has limited responsibility  All changes have to pass through one person  Can be a serious bottleneck

Team tip: code ownership Loose ownership model Whole code base is “owned” by group Indivs may work primarily on one aspect, but anybody has rights to change anything  Fewer bottlenecks  More responsive to individual needs  Individuals have to be generalists, not specialists  Less familiarity ⇒ very easy to introduce bugs Esp. integration bugs Regression testing critical

The truth of synchronized Java synchronized keyword just means: get lock before proceeding; release lock when done

The truth of synchronized public class Panopticon { private double[] _data; public synchronized void set(int idx, double v) { _data[idx]=v; } public synchronized double get(int idx) { return _data[idx]; }

The truth of synchronized public class Panopticon { private double[] _data; hidden Object _lockID_=null; public void set(int idx, double v) { while (!_testAndSet_(_lockID_==null || _lockID==this,this)); _data[idx]=v; _atomicSet_(_lockID,null); } public double get(int idx) { while (!_testAndSet_(_lockID_==null || _lockID_==this,this)); int result=_data[idx]; _atomicSet_(_lockID_,null); return result; }

The truth of synchronized Java synchronized keyword just means: get lock before proceeding; release lock when done synchronized method: only one thread can execute this method at a time (*) synchronized block: only one thread can execute this block at a time (*)

Where is the lock? Caveat: synchronized methods/blocks are only mutex with respect to a single lock Have to make sure that all threads are using the same lock Where does the lock variable live? non-static synchronized methods: lives on the object instance static synchronized methods: lives on the class variable synchronized block: lives on the object named in the argument

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; }

Who owns the lock? The memory picture:

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } } Answer: “ this ” owns it (object on which getX0() is being called)

Who owns the lock? The memory picture:

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} } Answer: x itself owns the lock

Who owns the lock? The memory picture:

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } } Again, “ this ” owns it (duh)

Who owns the lock? The memory picture:

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } public synchronized List getX3() { synchronized(x) { return x; } } }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getX0() { return x; } public List getX1() { synchronized(x){return x; }} public List getX2() { synchronized(this) { return x; } } public synchronized List getX3() { synchronized(x) { return x; } } } Trick question! Both locks ( this and x ) have to be acquired!

Who owns the lock? The memory picture:

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } } Same old, same old. “ this ” has the lock.

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } public int getA1() { synchronized(a){ return a; }} }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getA0() { return a; } public int getA1() { synchronized(a){ return a; }} } Another trick question! a is atomic -- it’s not an Object, and doesn’t have hidden state.

Who owns the lock? The memory picture: ???

Who owns the lock? Error you get from trying to synchronize on an atomic field: WhoOwnsTheLock.java:12: incompatible types found : int required: java.lang.Object public int getA1() { synchronized(a) { return a; } } ^ 1 error

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } } Again, “ this ” owns the lock.

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } } Again, “ this ” owns the lock. Danger Will Robinson! Can have multiple access to y !!!

Who owns the lock? Consider: WhoOwnsTheLock foo=new WhoOwnsTheLock(); WhoOwnsTheLock bar=new WhoOwnsTheLock();

Who owns the lock?

Consider: WhoOwnsTheLock foo=new WhoOwnsTheLock(); WhoOwnsTheLock bar=new WhoOwnsTheLock(); // in thread 0 foo.getY0(); // in thread 1 bar.getY0();

Who owns the lock? thread 0 synch w/ this lock

Who owns the lock? thread 0 synch w/ this lock & thread 1 synch w/ this one

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; } Remember: “ static ”==“associated with class” (i.e., class object). So this is synchronized on the class object. No more conflict. (yay!)

Who owns the lock?

public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; } public static List getY2() { synchronized(y) { return y; } }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized List getY0() { return y; } public static synchronized List getY1() { return y; } public static List getY2() { synchronized(y) { return y; } } Effectively, just as good as previous version...

Who owns the lock?

public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } Synchronized on “ this ”, but can have concurrent access via y ( x is safe)

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } public static synchronized int getXY1() { return x.size()+y.size(); }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } public static synchronized int getXY1() { return x.size()+y.size(); } Same problem, but in reverse -- y is safe, but can have multi-access to x.

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public synchronized int getXY0() { return x.size()+y.size(); } public static synchronized int getXY1() { return x.size()+y.size(); } public int getXY2() { synchronized(this) { // or on x itself synchronized(y) { return x.size()+y.size(); } } } }

Who owns the lock?

public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public int getAB0() { synchronized(this) { synhronized(b) { // oh oh... b is atomic return a+b; } } } }

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public int getAB0() { synchronized(this) { synhronized(y) { return a+b; } } } } Answer 1: synchronize on something “near” b (i.e., something else static) that isn’t, itself, atomic. Sometimes introduce spurious “ Object ” just for this

Who owns the lock? public class WhoOwnsTheLock { private List x=new LinkedList(); private static List y=new LinkedList(); private int a=3; private static int b=42; public static synchronized int getAB1() { synchronized(this) { return a+b; } } } Answer 2: synchronize on class object (via “ static synchronized ” method declaration)