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

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Serialization Flatten your object for automated storage or network.
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.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
An Approach to Safe Object Sharing Ciaran Bryce & Chrislain Razafimahefa University of Geneva, Switzerland.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Slides Credit Umair Javed LUMS Web Application Development.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
In the name of Allah The Proxy Pattern Elham moazzen.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Partitioning Patterns How to partition complex actors and concepts into multiple classes. Layered Initialization Filter Composite.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reducing synchronization.
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Session 7 Methods Strings Constructors this Inheritance.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
System Programming Practical Session 4: Concurrency / Safety.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Chapter 11 Exceptions and Input/Output Operations.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Singleton Pattern Presented By:- Navaneet Kumar ise
Fundamentals Design Patterns Delegation Interface Immutable Marker Interface Proxy 10/14/2001 8:35 PM.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
CSC Multiprocessor Programming, Spring, 2012 Outline for Chapter 4 – Composing Objects – thread-safe object-oriented composition, Dr. Dale E. Parson,
Inheritance ndex.html ndex.htmland “Java.
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.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Topic 4: Distributed Objects Dr. Ayman Srour Faculty of Applied Engineering and Urban Planning University of Palestine.
Catalog of Refactoring (6) Making Method Calls Simpler.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Threads in Java Two ways to start a thread
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Java Programming Language
Delegates and Events 14: Delegates and Events
Programming Design Patterns
Java Programming Language
Java Concurrency 17-Jan-19.
7 Arrays.
Java Concurrency.
Java Concurrency.
Exceptions 10-May-19.
Java Concurrency 29-May-19.
Chapter 5 Classes.
Threads and concurrency / Safety
Presentation transcript:

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

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 2 Encapsulation technique to structurally guarantee that at most one activity (thread) at a time can possibly access a given object. –Ensure uniqueness of access –No need to rely on dynamic locking (synch)

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 3 Reference Leakage A reference r to an object x can escape from method m executing some activity: –m passes r as an argument to a method –m passes r as the return value from a method invocation –m records r in some field accessible from other activity ( worst case: static fields) –m releases another reference that can be traversed to access r.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 4 Allowed leakage Some leakages are allowed if they guarantee no state changes.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 5 Confinement across methods If a given method creates an object, and does not let it escape, thus no other thread will interfere with it: –Hiding access within local scope You may allow to escape as a tail-call: original method will have no more use of reference. Hand-off protocol: at any time, at most one actively executing method can access an object: –Tail call –Factory methods.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 6 Confinement across methods Sessions –A public method creates an object confined to a sequence of operation comprising the service –Method performs any cleanup needed via a finally clause

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 7 Confinement across methods Multiple calls: tail calls do not apply if calling method needs to access object after call. –Caller copies -- identity is not needed –Receiver copies -- ditto –Using scalar arguments instead of references: Provide enough information for receiver to construct object if need by. –Trust

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 8 Confinement within threads Thread-per-session design. class ThreadPerSessionBasedService { // fragments //... public void service() { Runnable r = new Runnable() { public void run() { OutputStream output = null; try { output = new FileOutputStream("..."); doService(output); } catch (IOException e) { handleIOFailure(); } finally { try { if (output != null) output.close(); } catch (IOException ignore) {} } }; new Thread(r).start(); } void handleIOFailure() {} void doService(OutputStream s) throws IOException { s.write(0); //... possibly more hand-offs... } }// end ThreadPerSessionBasedService

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 9 Confinement within threads Thread-specific fields: –Static method Thread.currentThread() –Add fields to thread subclass, and access them within current thread.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 10 class ThreadWithOutputStream extends Thread { private OutputStream output; ThreadWithOutputStream(Runnable r, OutputStream s) { super(r); output = s; } static ThreadWithOutputStream current() throws ClassCastException { return (ThreadWithOutputStream) (currentThread()); } static OutputStream getOutput() { return current().output; } static void setOutput(OutputStream s) { current().output = s;} }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 11 class ServiceUsingThreadWithOutputStream { // Fragments //... public void service() throws IOException { OutputStream output = new FileOutputStream("..."); Runnable r = new Runnable() { public void run() { try { doService(); } catch (IOException e) { } } }; new ThreadWithOutputStream(r, output).start(); } void doService() throws IOException { ThreadWithOutputStream.current().getOutput().write(0); }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 12 ThreadLocal class ServiceUsingThreadLocal { // Fragments static ThreadLocal output = new ThreadLocal(); public void service() { try { final OutputStream s = new FileOutputStream("..."); Runnable r = new Runnable() { public void run() { output.set(s); try { doService(); } catch (IOException e) { } finally { try { s.close(); } catch (IOException ignore) {} } }; new Thread(r).start(); } catch (IOException e) {} } void doService() throws IOException { ((OutputStream)(output.get())).write(0); //... } }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 13 Confinement within threads Housing object references in Thread objects allow methods running in the same thread to share them freely Thread specific variables hide parameters and makes hard error checking and leakage No synchronization needed, but Hinders reusability as it increases coupling.

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 14 Confinement within objects Can confine all access to be only internal to an object, so no further locking is needed. –Exclusive control of host of container object propagates to its internal parts. –Need synchronization at all entry points in the Host object. –Host own the parts, or parts are “contained” in Host. –Host constructs new instances of the parts, –Host does not leak references –Best host: all parts are fixed. No need for updates. Typical implementation of such Host: –Use of adapters –Use of subclassing

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 15 Confinement within groups: Adapters Can be used o wrap bare unsynchronized objects within fully synchronized Hosts. class BarePoint { public double x; public double y; } class SynchedPoint { protected final BarePoint delegate = new BarePoint(); public synchronized double getX() { return delegate.x;} public synchronized double getY() { return delegate.y; } public synchronized void setX(double v) { delegate.x = v; } public synchronized void setY(double v) { delegate.y = v; } } The Java.util.collection framework uses adapter-based allowing layered synchronization of collection classes. Except for Vector and Hashtable, basic collection classes are unsynchronized. Synchronized adapters can be constructed: List l = Collections.synchronizedList(new ArrayList());

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 16 Confinement within groups: Adapters When you cannot guarantee containment, define multiple versions of a class and instantiate appropriately for given usage. class Address { // Fragments protected String street; protected String city; public String getStreet() { return street; } public void setStreet(String s) { street = s; } //... public void printLabel(OutputStream s) { } } class SynchronizedAddress extends Address { //... public synchronized String getStreet() { return super.getStreet(); } public synchronized void setStreet(String s) { super.setStreet(s); } public synchronized void printLabel(OutputStream s) { super.printLabel(s); }

Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 17 Confinement within groups Groups of objects accessible across multiple threads can together ensure that only one of them can access a given resource: –Tokens –Batons –Linear objects –Capabilities –Resources These groups need strict protocols to manage the resource: –Acquire –Forget –Put (give) –Take –Exchange