CP — Concurrent Programming 6. Liveness and Guarded Methods Prof. O. Nierstrasz Wintersemester 2005 / 2006.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
5. Liveness and Guarded Methods Prof. O. Nierstrasz Selected material © Magee and Kramer.
ESE Einführung in Software Engineering 7. Modeling Behaviour Prof. O. Nierstrasz.
Introduction to Software Engineering 7. Modeling Behaviour.
CP — Concurrent Programming 9. Condition Objects Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
8. Condition Objects Prof. O. Nierstrasz Selected material © 2005 Bowbeer, Goetz, Holmes, Lea and Peierls.
CP — Concurrent Programming 5. Safety and Liveness Properties Prof. O. Nierstrasz Wintersemester 2005 / 2006.
CP — Concurrent Programming 8. Liveness and Asynchrony Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
OORPT Object-Oriented Reengineering Patterns and Techniques 7. Problem Detection Prof. O. Nierstrasz.
10. Petri Nets Prof. O. Nierstrasz. Roadmap  Definition: —places, transitions, inputs, outputs —firing enabled transitions  Modelling: —concurrency.
Object-Oriented Reengineering Patterns and Techniques Prof. O. Nierstrasz Prof. S. Ducasse T.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
Group Communications Group communication: one source process sending a message to a group of processes: Destination is a group rather than a single process.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
CP — Concurrent Programming 12. Petri Nets Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
CP — Concurrent Programming 3. Safety and Synchronization Prof. O. Nierstrasz Wintersemester 2005 / 2006.
7. Liveness and Asynchrony Prof. O. Nierstrasz. Roadmap  Asynchronous invocations  Simple Relays —Direct invocations —Thread-based messages —Command-based.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
CP — Concurrent Programming 10. Fairness and Optimism Prof. O. Nierstrasz Wintersemester 2005 / 2006.
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
More Multithreaded Programming in Java David Meredith Aalborg University.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Internet Software Development More stuff on Threads Paul Krause.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Inter-Thread communication State dependency: Guarded Methods.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
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.
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.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
About the Author A Lifetime of Software Development Started Writing Code at Age 11 Programming Summer Camp at Age 12 Writing Code Ever Since At Age 25,
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Multithreading / Concurrency
Background on the need for Synchronization
Condition Variables and Producer/Consumer
Cancellation.
Condition Variables and Producer/Consumer
Multithreading.
More concurrency issues
Presentation transcript:

CP — Concurrent Programming 6. Liveness and Guarded Methods Prof. O. Nierstrasz Wintersemester 2005 / 2006

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.2 Liveness and Guarded Methods Overview  Guarded Methods —Checking guard conditions —Handling interrupts —Structuring notification – Encapsulating assignment – Tracking state – Tracking state variables – Delegating notifications

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.3 Achieving Liveness There are various strategies and techniques to ensure liveness:  Start with safe design and selectively remove synchronization  Start with live design and selectively add safety  Adopt design patterns that limit the need for synchronization  Adopt standard architectures that avoid cyclic dependencies

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.4 Pattern: Guarded Methods Intent: Temporarily suspend an incoming thread when an object is not in the right state to fulfil a request, and wait for the state to change rather than balking (raising an exception).

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.5 Guarded Methods — applicability  Clients can tolerate indefinite postponement. (Otherwise, use a balking design.)  You can guarantee that the required states are eventually reached (via other requests), or if not, that it is acceptable to block forever.  You can arrange that notifications occur after all relevant state changes. (Otherwise consider a design based on a busy-wait spin loop.) …

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.6 Guarded Methods — applicability … …  You can avoid or cope with liveness problems due to waiting threads retaining all synchronization locks.  You can construct computable predicates describing the state in which actions will succeed. (Otherwise consider an optimistic design.)  Conditions and actions are managed within a single object. (Otherwise consider a transactional form.)

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.7 Guarded Methods — design steps The basic recipe is to use wait in a conditional loop to block until it is safe to proceed, and use notifyAll to wake up blocked threads. public synchronized Object service() { while (wrong State) { try {wait(); } catch (InterruptedException e) { } } // fill request and change state... notifyAll(); return result; } public synchronized Object service() { while (wrong State) { try {wait(); } catch (InterruptedException e) { } } // fill request and change state... notifyAll(); return result; }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.8 Step: Separate interface from policy Define interfaces for the methods, so that classes can implement guarded methods according to different policies. public interface BoundedCounter { public static final long MIN = 0;// min value public static final long MAX = 10;// max value public long value();// inv’t: MIN <= value() <= MAX // init: value() == MIN public void inc();// pre: value() < MAX public void dec();// pre: value() > MIN } public interface BoundedCounter { public static final long MIN = 0;// min value public static final long MAX = 10;// max value public long value();// inv’t: MIN <= value() <= MAX // init: value() == MIN public void inc();// pre: value() < MAX public void dec();// pre: value() > MIN }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.9 Step: Check guard conditions  Define a predicate that precisely describes the conditions under which actions may proceed. (This can be encapsulated as a helper method.)  Precede the conditional actions with a guarded wait loop of the form: Optionally, encapsulate this code as a helper method. while (!condition) try { wait(); } catch (InterruptedException ex) {... } while (!condition) try { wait(); } catch (InterruptedException ex) {... }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.10 Step: Check guard conditions...  If there is only one possible condition to check in this class (and all plausible subclasses), and notifications are issued only when the condition is true, then there is no need to re-check the condition after returning from wait()  Ensure that the object is in a consistent state (i.e., the class invariant holds) before entering any wait (since wait releases the synchronization lock). The easiest way to do this is to perform the guards before taking any actions.

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.11 Step: Handle interrupts  Establish a policy to deal with InterruptedExceptions. Possibilities include:: —Ignore interrupts (i.e., an empty catch clause), which preserves safety at the possible expense of liveness. —Terminate the current thread (stop). This preserves safety, though brutally! (Not recommended.) —Exit the method, possibly raising an exception. This preserves liveness but may require the caller to take special action to preserve safety. —Cleanup and restart. —Ask for user intervention before proceeding. Interrupts can be useful to signal that the guard can never become true because, for example, the collaborating threads have terminated.

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.12 Step: Signal state changes  Add notification code to each method of the class that changes state in any way that can affect the value of a guard condition. Some options are: —use notifyAll to wake up all threads that are blocked in waits for the host object. —use notify to wake up only one thread (if any exist). This is best treated as an optimization where: – all blocked threads are necessarily waiting for conditions signalled by the same notifications, – only one of them can be enabled by any given notification, and – it does not matter which one of them becomes enabled. —You build your own special-purpose notification methods using notify and notifyAll. (For example, to selectively notify threads, or to provide certain fairness guarantees.)

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.13 notify() vs. notifyAll() Careless use of notify() may lead to race conditions. A A BC C C B B dec wait dec inc notify inc wait notify wait Now both A and C wait for nothing!

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.14 Step: Structure notifications  Ensure that each wait is balanced by at least one notification. Options include: Blanket Notifications Place a notification at the end of every method that can cause any state change (i.e., assigns any instance variable). Simple and reliable, but may cause performance problems... Encapsulating Assignment Encapsulate assignment to each variable mentioned in any guard condition in a helper method that performs the notification after updating the variable. Tracking State Only issue notifications for the particular state changes that could actually unblock waiting threads. May improve performance, at the cost of flexibility (i.e., subclassing becomes harder.) Tracking State Variables Maintain an instance variable that represents control state. Whenever the object changes state, invoke a helper method that re- evaluates the control state and will issue notifications if guard conditions are affected. Delegating Notifications Use helper objects to maintain aspects of state and have these helpers issue the notifications.

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.15 Encapsulating assignment Guards and assignments are encapsulated in helper methods: public class BoundedCounterV1 implements BoundedCounter { protected long count_ = MIN; public synchronized long value() { return count_; } public synchronized void inc() { awaitIncrementable(); setCount(count_ + 1); } public synchronized void dec() { awaitDecrementable(); setCount(count_ - 1); } … public class BoundedCounterV1 implements BoundedCounter { protected long count_ = MIN; public synchronized long value() { return count_; } public synchronized void inc() { awaitIncrementable(); setCount(count_ + 1); } public synchronized void dec() { awaitDecrementable(); setCount(count_ - 1); } …

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.16 … protected synchronized void awaitIncrementable() { while (count_ >= MAX) try { wait(); } catch(InterruptedException ex) {}; } protected synchronized void awaitDecrementable() { while (count_ <= MIN) try { wait(); } catch(InterruptedException ex) { }; } protected synchronized void setCount(long newValue) { count_ = newValue; notifyAll(); } … protected synchronized void awaitIncrementable() { while (count_ >= MAX) try { wait(); } catch(InterruptedException ex) {}; } protected synchronized void awaitDecrementable() { while (count_ <= MIN) try { wait(); } catch(InterruptedException ex) { }; } protected synchronized void setCount(long newValue) { count_ = newValue; notifyAll(); }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.17 Tracking State The only transitions that can possibly affect waiting threads are those that step away from logical states top and bottom: public class BoundedCounterVST implements BoundedCounter { protected long count_ = MIN; //... public synchronized void inc() { while (count_ == MAX) try { wait(); } catch(InterruptedException ex) {}; if (count_++ == MIN) notifyAll();// just left bottom state }... } public class BoundedCounterVST implements BoundedCounter { protected long count_ = MIN; //... public synchronized void inc() { while (count_ == MAX) try { wait(); } catch(InterruptedException ex) {}; if (count_++ == MIN) notifyAll();// just left bottom state }... }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.18 Tracking State Variables public class BoundedCounterVSV implements BoundedCounter { static final int BOTTOM = 0; // logical states static final int MIDDLE = 1; static final int TOP = 2; protected int state_ = BOTTOM;// state variable protected long count_ = MIN; public synchronized void inc() { while (state_ == TOP)// consult logical state try { wait(); } catch(InterruptedException ex) {}; ++count_;// modify actual state checkState();// sync logical state }... public class BoundedCounterVSV implements BoundedCounter { static final int BOTTOM = 0; // logical states static final int MIDDLE = 1; static final int TOP = 2; protected int state_ = BOTTOM;// state variable protected long count_ = MIN; public synchronized void inc() { while (state_ == TOP)// consult logical state try { wait(); } catch(InterruptedException ex) {}; ++count_;// modify actual state checkState();// sync logical state }...

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP public synchronized void dec() {... } public synchronized long value() { return count_; } protected synchronized void checkState() { int oldState = state_; if (count_ == MIN)state_ = BOTTOM; else if (count_ == MAX)state_ = TOP; elsestate_ = MIDDLE; if (state_ != oldState && (oldState == TOP || oldState == BOTTOM)) notifyAll(); }... public synchronized void dec() {... } public synchronized long value() { return count_; } protected synchronized void checkState() { int oldState = state_; if (count_ == MIN)state_ = BOTTOM; else if (count_ == MAX)state_ = TOP; elsestate_ = MIDDLE; if (state_ != oldState && (oldState == TOP || oldState == BOTTOM)) notifyAll(); }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.20 Delegating notifications public class NotifyingLong { private long value_; private Object observer_; public NotifyingLong(Object o, long v) { observer_ = o; value_ = v; } public synchronized long value() { return value_; } public void setValue(long v) { synchronized(this) { value_ = v; } synchronized(observer_) { observer_.notifyAll();// NB: must be synched! } public class NotifyingLong { private long value_; private Object observer_; public NotifyingLong(Object o, long v) { observer_ = o; value_ = v; } public synchronized long value() { return value_; } public void setValue(long v) { synchronized(this) { value_ = v; } synchronized(observer_) { observer_.notifyAll();// NB: must be synched! }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.21 Delegating notifications... Notification is delegated to the helper object: public class BoundedCounterVNL implements BoundedCounter { private NotifyingLong c_ = new NotifyingLong(this, MIN); public synchronized void inc() { while (c_.value() >= MAX) { try { wait(); } catch(InterruptedException ex) {}; } c_.setValue(c_.value()+1); }... } public class BoundedCounterVNL implements BoundedCounter { private NotifyingLong c_ = new NotifyingLong(this, MIN); public synchronized void inc() { while (c_.value() >= MAX) { try { wait(); } catch(InterruptedException ex) {}; } c_.setValue(c_.value()+1); }... }

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.22 What you should know!  When can you apply the Guarded Methods pattern?  When should methods recheck guard conditions after waking from a wait()?  Why should you usually prefer notifyAll() to notify()?  When and where should you issue notification?  Why must you re-establish the class invariant before calling wait()?  What should you do when you receive an InterruptedException?  What is the difference between tracking state and using state-tracking variables?

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.23 Can you answer these questions?  When are guarded methods better than balking?  When should you use helper methods to implement guarded methods?  What is the best way to structure guarded methods for a class if you would like it to be easy for others to define correctly functioning subclasses?  When is the complexity of delegating notifications worthwhile?

© Oscar Nierstrasz CP — Liveness and Guarded Methods CP 6.24 License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.