Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reducing synchronization.

Slides:



Advertisements
Similar presentations
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
Advertisements

50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Concurrency 101 Shared state. Part 1: General Concepts 2.
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.
Chapter 6: Using Design Patterns
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
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 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.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Chapter 10 Classes Continued
Factory Design Pattern, cont’d COMP 401 Fall 2014 Lecture 13 10/2/2014.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
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.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Runtime Refinement Checking of Concurrent Data Structures (the VYRD project) Serdar Tasiran Koç University, Istanbul, Turkey Shaz Qadeer Microsoft Research,
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Refactoring1 Improving the structure of existing code.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Cross cutting Cross-cutting of components and aspects ordinary program structure-shy functionality structure synchronization better program Components.
Review – Final Test Chapters 8,10,11. Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input.
Structural Design Patterns
Concurrent Programming in JAVA What is it? When/How do I use it? When is my class safe? What should I think about during Design?
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Exceptions, cont’d. Factory Design Pattern COMP 401 Fall 2014 Lecture 12 9/30/2014.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Refactoring1 Improving the structure of existing code.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Confinement.
The Singleton Pattern (Creational)
XAspects slides Cross-cutting of concerns ordinary program structure-shy functionality structure synchronization better program Chapter 1 Chapter 2 Chapter.
Fundamentals Design Patterns Delegation Interface Immutable Marker Interface Proxy 10/14/2001 8:35 PM.
Specifying Multithreaded Java semantics for Program Verification Abhik Roychoudhury National University of Singapore (Joint work with Tulika Mitra)
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:
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Catalog of Refactoring (6) Making Method Calls Simpler.
EECE 310: Software Engineering
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Other Important Synchronization Primitives
University of Central Florida COP 3330 Object Oriented Programming
Improving the structure of existing code
Problems with Locks Andrew Whitaker CSE451.
Threads and concurrency / Safety
Presentation transcript:

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reducing synchronization

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 2 Removing synchronization from queries Two issues to consider to safely remove it: –Legality: value of field never assumes an illegal value. –Staleness: clients do not necessarily require latest updated value, can live with stale value.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 3 Not always legal field value Synchronize its query and all commands that update it. Omit query. In multi-threaded programming queries are used infrequently as their values can change asynchronously.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 4 Double-check pattern If callers of unsynchronized queries realize they have just read an illegal value, sometimes they can take a corrective action. –Reaccess the field under synchronization –Get its most current value –Take appropriate action. This is the essence of double check pattern. Useful for lazy initialization Should be used to create singletons in multi- threaded environments.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 5 Singleton pattern for multi-threaded apps public class Singleton { public static Singleton instance(){ //first check if (instance == null) { synchronized(singletonLock){ //double check: second one if (instance == null){ instance = new Singleton; } return instance; } // other methods private Singleton instance; private final singletonLock = new Object(); }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 6 When to use the double-check pattern App has one or more critical sections of code that must execute sequentially. Multiple threads can potentially attempt to execute critical section simultaneously. Critical section is execute just once. Synchronizing the query on every access to critical section causes excessive overhead. This pattern can be used to access the value of a field as well, reducing the use of synchronization.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 7 When not use use double-check pattern Unwise to use it for fields containing references to objects or arrays. Visibility of a reference read without synchronization does not guarantee visibility of non-volatile fields accessed from the reference.Even if a reference is non-null, fields access through it may have stale values. It is difficult at best to use single flag field as an indicator that a whole set of fields must be initialized. The sequential reorderings may cause flag to be visible before other fields are visibly initialized.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 8 Stateless and synchronization Methods in fully immutable objects are stateless, so no sync is needed But statelessness may occur in other kinds of classes. Do not need to synchronize stateless part of methods, allowing for synchronized methods to execute in these sections of code. However can split synchronization only when different parts of the method are not in any way dependent, so that is acceptable for other methods to see and use the object before the full method completion. Data structures that are linked are also amenable to this kinds of manipulations.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 9 Example class ServerWithStateUpdate { private double state; private final Helper helper = new Helper(); public synchronized void service() { state = 2.0f; //...; // set to some new value helper.operation(); } public synchronized double getState() { return state; } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 10 Example class ServerWithOpenCall { private double state; private final Helper helper = new Helper(); private synchronized void updateState() { state = 2.0f; //...; // set to some new value } public void service() { updateState(); // it grabs the lock helper.operation(); // releases lock } public synchronized double getState() { return state; } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 11 Splitting synchronization Situation: class can be partitioned into subsets which are: –Independent –Non-interactive –Non-conflicting Useful design technique in OO but much more beneficial in multi-threaded prog. General rule: –The more finely you can subdivide internal synchronization in a class, better liveness properties.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 12 Splitting synchronization Refactoring: –Partition some functionality of a Host class into a Helper –In Host class, declare Helper instance final. –In Host class, forward appropriate methods to Helper as open calls; this works because the methods being called are stateless w.r.t. Host HelperHost Nonsynch method synch method

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 13 class Shape { // Incomplete protected double x = 0.0; protected double y = 0.0; protected double width = 0.0; protected double height = 0.0; public synchronized double x() { return x;} public synchronized double y() { return y; } public synchronized double width() { return width;} public synchronized double height() { return height; } public synchronized void adjustLocation() { x = 1; // longCalculation1(); y = 2; //longCalculation2(); } public synchronized void adjustDimensions() { width = 3; // longCalculation3(); height = 4; // longCalculation4(); } //... }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 14 class PassThroughShape { protected final AdjustableLoc loc = new AdjustableLoc(0, 0); protected final AdjustableDim dim = new AdjustableDim(0, 0); public double x() { return loc.x(); } public double y() { return loc.y(); } public double width() { return dim.width(); } public double height() { return dim.height(); } public void adjustLocation() { loc.adjust(); } public void adjustDimensions() { dim.adjust(); } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 15 class AdjustableLoc { protected double x; protected double y; public AdjustableLoc(double initX, double initY) { x = initX; y = initY; } public synchronized double x() { return x;} public synchronized double y() { return y; } public synchronized void adjust() { x = longCalculation1(); y = longCalculation2(); } protected double longCalculation1() { return 1; /*... */ } protected double longCalculation2() { return 2; /*... */ } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 16 class AdjustableDim { protected double width; protected double height; public AdjustableDim(double initW, double initH) { width = initW; height = initH; } public synchronized double width() { return width;} public synchronized double height() { return height; } public synchronized void adjust() { width = longCalculation3(); height = longCalculation4(); } protected double longCalculation3() { return 3; /*... */ } protected double longCalculation4() { return 4; /*... */ } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 17 Splitting locks Idiom: Do not use the “this” lock on an object, use a synchronized block where the lock comes from an instance of Object. Refactoring: –For each independent subset of functionality, declare a final object, lock, initialized in the constructor for the Host and never reinitialized. –Lock object can be of Object or any subclass. –If a subset is uniquely associated with some existing object uniquely referenced from a field, you may use that object as the lock. –One of these locks can be “this” itself. –Declare all methods corresponding to each subset as unsynchronized, but use synchronized blocks in code.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 18 class LockSplitShape { // Incomplete protected double x = 0.0; protected double y = 0.0; protected double width = 0.0; protected double height = 0.0; protected final Object locationLock = new Object(); protected final Object dimensionLock = new Object(); public double x() { synchronized(locationLock) { return x; } public double y() { synchronized(locationLock) { return y; } public void adjustLocation() { synchronized(locationLock) { x = 1; // longCalculation1(); y = 2; // longCalculation2(); } … // etc }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 19 Isolated fields Sometimes classes manage a set of isolated fields, that can be manipulated independent of one another. Use a simple form of splitting to offload synchronization protection to objects used solely to protect basic operations on basic types. These new created classes promise atomicity instead of immutability as in Integer, Float, etc.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 20 class SynchronizedInt { private int value; public SynchronizedInt(int v) { value = v; } public synchronized int get() { return value; } public synchronized int set(int v) { // returns previous value int oldValue = value; value = v; return oldValue; } public synchronized int increment() { return ++value; } // and so on }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 21 class Person { // Fragments //... protected final SynchronizedInt age = new SynchronizedInt(0); protected final SynchronizedBoolean isMarried = new SynchronizedBoolean(false); protected final SynchronizedDouble income = new SynchronizedDouble(0.0); public int getAge() { return age.get(); } public void birthday() { age.increment(); } //... }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 22 Linked data structures Lock splitting can minimize access contention to objects serving as entry points to a linked data structure. In the case of linked structures you can carefully do more refinement in the synchronization.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 23 class LinkedQueue { protected Node head = new Node(null); protected Node last = head; protected final Object pollLock = new Object(); protected final Object getLock = new Object(); public void put(Object x) { Node node = new Node(x); synchronized (putLock) { // insert at end of list synchronized (last) { last.next = node; // extend list last = node; }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 24 Read-only adapters Classes with components should not expose in general their components; but there are cases where clients need to access components for inspection What is commonly done: –Send copies of the components –Use the clone method of component if it has one. All the above may be expensive or impossible.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 25 Read-only adapters In many cases you can selectively permit some leakage by constructing and returning an adapter object surrounding the part that exposes only the operations that clients may use without any interference – usually giving access only to queries.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 26 public Object get() { // returns null if empty synchronized (getLock) { synchronized (head) { Object x = null; Node first = head.next; // get to first real node if (first != null) { x = first.object; first.object = null; // forget old object head = first; // first becomes new head } return x; } static class Node { // local node class for queue Object object; Node next = null; Node(Object x) { object = x; } } } // end of LinkedQueue

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 27 Read-only adapters Refactoring: –Define a base interface describing some non- mutative functionality. –Optionally, define a subinterface that supports additional commands used in the Host class. –Define a read-only adapter that forwards only the exported operations. This adapter is final.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 28 class InsufficientFunds extends Exception {} interface Account { long balance(); } interface UpdatableAccount extends Account { void credit(long amount) throws InsufficientFunds; void debit(long amount) throws InsufficientFunds; } // Sample implementation of updatable version class UpdatableAccountImpl implements UpdatableAccount { private long currentBalance; public UpdatableAccountImpl(long initialBalance) { currentBalance = initialBalance; } public synchronized long balance() { return currentBalance; }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 29 public synchronized void credit(long amount) throws InsufficientFunds { if (amount >= 0 || currentBalance >= -amount) currentBalance += amount; else throw new InsufficientFunds(); } public synchronized void debit(long amount) throws InsufficientFunds { credit(-amount); } final class ImmutableAccount implements Account { private Account delegate; public ImmutableAccount(long initialBalance) { delegate = new UpdatableAccountImpl(initialBalance); } ImmutableAccount(Account acct) { delegate = acct; } public long balance() { // forward the immutable method return delegate.balance(); }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 30 class AccountRecorder { // A logging facility public void recordBalance(Account a) { System.out.println(a.balance()); // or record in file } class AccountHolder { private UpdatableAccount acct = new UpdatableAccountImpl(0); private AccountRecorder recorder; public AccountHolder(AccountRecorder r) { recorder = r; } public synchronized void acceptMoney(long amount) { try { acct.credit(amount); recorder.recordBalance(new ImmutableAccount(acct));//(*) } catch (InsufficientFunds ex) { System.out.println("Cannot accept negative amount."); }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 31 class EvilAccountRecorder extends AccountRecorder { private long embezzlement; //... public void recordBalance(Account a) { super.recordBalance(a); if (a instanceof UpdatableAccount) { UpdatableAccount u = (UpdatableAccount)a; try { u.debit(10); embezzlement += 10; } catch (InsufficientFunds quietlyignore) {} } Use of a read-only wrapper in last example seems unnecessary; but it guards against what might happen if someone were to write the following subclass, and use it in conjunction with AccountHolder:

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 32 Copy-on-write When a set of fields comprising the state of the object must maintain a set of inter- related invariants, you can isolate these fields in another object that preserves the intended semantics. Rely on immutable representation objects.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 33 class ImmutablePoint { private final int x; private final int y; public ImmutablePoint(int initX, int initY) { x = initX; y = initY; } public int x() { return x; } public int y() { return y; } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 34 class Dot { protected ImmutablePoint loc; public Dot(int x, int y) { loc = new ImmutablePoint(x, y); } public synchronized ImmutablePoint location() { return loc; } protected synchronized void updateLoc(ImmutablePoint newLoc) { loc = newLoc; } public void moveTo(int x, int y) { updateLoc(new ImmutablePoint(x, y)); } public synchronized void shiftX(int delta) { updateLoc(new ImmutablePoint(loc.x() + delta, loc.y())); }