David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 13: Concurring Concurrently.

Slides:



Advertisements
Similar presentations
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
Advertisements

David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.
5/17/2015 OO Design: Liskov Substitution Principle 1.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Object-Oriented Software Engineering Concurrent Programming.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Threads CS 3250 Some of these slides contain material by Professor Chuck Allison.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Java Thread and Memory Model
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans.
Multithreaded Programming in Java David Meredith Aalborg University.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
David Evans CS150: Computer Science University of Virginia Computer Science Class 37: How to Find Aliens (and Factors)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Concurrent Programming Acknowledgements: Some slides adapted from David Evans, U. Virginia.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Multithreading / Concurrency
Design David Evans cs205: engineering software
Locking cs205: engineering software
Type Abstraction SWE Spring 2009.
Subtyping Rules David Evans cs205: engineering software BlackBear
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Concurring Concurrently
Data Abstraction David Evans cs205: engineering software
Multithreading.
Lecture 4: Data Abstraction CS201j: Engineering Software
cs205: engineering software
More on Creating Classes
Threads and Multithreading
David Evans Lecture 19: ||ism I don’t think we have found the right programming concepts for parallel computers yet.
Lecture 13: Subtyping Rules Killer Bear Climber
Type Abstraction SWE Spring 2013.
CMSC 202 Threads.
Presentation transcript:

David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 13: Concurring Concurrently

17 October 2002CS 201J Fall Menu Subtyping Rules Review –Overriding and Overloading Concurrency

17 October 2002CS 201J Fall Substitution Principle …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; …(anything could be here) mt1 = mt2.m (mt3); If the Java compiler is happy with this code, which of these are guaranteed to be true: a.The apparent type of mt2 is MysteryType2 b.At the last statement, the actual type of mt2 is MysteryType2 c.MysteryType2 has a method named m d.The MysteryType2.m method takes a parameter of type MysteryType3 e.The MysteryType2.m method returns a subtype of MysteryType1 f.After the last statement, the actual type of mt1 is MysteryType1

17 October 2002CS 201J Fall …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; …(anything could be here) mt1 = mt2.m (mt3); If the Java compiler is happy with this code, which of these are guaranteed to be true: a.The apparent type of mt2 is MysteryType2 b.At the last statement, the actual type of mt2 is MysteryType2 c.MysteryType2 has a method named m d.The MysteryType2.m method takes a parameter of type MysteryType3 e.The MysteryType2.m method returns a subtype of MysteryType1 f.After the last statement, the actual type of mt1 is MysteryType1 TRUE: the apparent type is obvious from the declaration. FALSE: we only know the actual type <= MysteryType2 TRUE FALSE: we only know it takes a parameter >= MysteryType3 TRUE: the assignment type checking depends on this FALSE: we only know that the actual type <= MysteryType1

17 October 2002CS 201J Fall Subtyping Rules class A { public RA m (PA p) ; } …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; … mt1 = mt2.m (mt3); RA must be a subtype of MysteryType1: RA <= MysteryType1 MysteryType3 must be a subtype of PA: PA >= MysteryType3 If A is MysteryType2, what do we know about RA and PA?

17 October 2002CS 201J Fall Subtyping Rules class A { public RA m (PA p) ; } class B extends A { public RB m (PB a); } …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; … mt1 = mt2.m (mt3); RB must be a subtype of RA: RB <= RA PA must be a subtype of PB:PB >= PA If B <= A, what do we know about RB and PB?

17 October 2002CS 201J Fall Substitution Principle class A { public RA m (PA p) ; } class B extends A { public RB m (PB a); } …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; … mt1 = mt2.m (mt3); Substitution Principle: Parameters PB >= PA Preconditions pre_A  pre_B Result RB <= RA Postconditions post_B  post_A

17 October 2002CS 201J Fall Substitution Principle / Eiffel class A { public RA m (PA p) ; } class B extends A { public RB m (PB a); } …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; … mt1 = mt2.m (mt3); Substitution PrincipleEiffel Parameters PB >= PAPB <= PA Preconditions pre_A  pre_Bpre_B  pre_A Result RB <= RARB <= RA Postconditions post_B  post_A post_B  post_A

17 October 2002CS 201J Fall Overloading and Overriding Overriding: replacing a supertype’s method in a subtype –Dynamic dispatch finds method of actual type Overloading: providing two methods with the same name but different parameter types –Statically select most specific matching method of apparent type

17 October 2002CS 201J Fall Overloading Example public class Overloaded extends Object { public int tryMe (Object o) { return 17; } public int tryMe (String s) { return 23; } public boolean equals (String s) { return true; } public boolean equals (Object) is inherited from Object

17 October 2002CS 201J Fall Overloading public class Overloaded { public int tryMe (Object o) { return 17; } public int tryMe (String s) { return 23; } public boolean equals (String s) { return true; } static public void main (String args[]) { Overloaded over = new Overloaded (); System.err.println (over.tryMe (over)); System.err.println (over.tryMe (new String ("test"))); Object obj = new String ("test"); System.err.println (over.tryMe (obj)); System.err.println (over.equals (new String ("test"))); System.err.println (over.equals (obj)); Object obj2 = over; System.err.println (obj2.equals (new String ("test"))); } true false

17 October 2002CS 201J Fall Overkill Overloading and overriding together can be overwhelming! Avoid overloading whenever possible: names are cheap and plentiful One place you can’t easily avoid it: constructors (they all have to have the same name)

17 October 2002CS 201J Fall My Favorite C++ Program #include class A { public: void other () { printf("is an empty func in A\n"); }; virtual void other (class A *a) { printf("In A\n"); } }; class B: public A { public: void other (class B *b) { printf("In B\n"); } }; class C: public A { public: void other (class C *c) { printf("In C\n"); } }; void main(void) { A a; B b; C c; A *aPtr = &a; B *bPtr = &b; C *cPtr = &c; aPtr = bPtr; aPtr->other(bPtr); bPtr->other(); } (On notes, for experts only)

17 October 2002CS 201J Fall Concurrency

17 October 2002CS 201J Fall Our computer can only do one instruction at a time, why would we want to program pretending it can do many things at once?

17 October 2002CS 201J Fall Concurrent Programming Some problems are clearer to program concurrently: –Modularity Don’t have to explicitly interleave code for different abstractions (especially: user interfaces) High-level interactions – synchronization, communication –Modeling Closer map to real world problems: things in the real world aren’t sequential

17 October 2002CS 201J Fall Concurrency in Java public class Thread implements Runnable { // OVERVIEW: A thread is a thread of execution in a program. // The Java Virtual Machine allows an application to have // multiple threads of execution running concurrently. public Thread (Runnable target) // Creates a new Thread object that will run the target. public void start () // Starts a new thread of execution. … many other methods }

17 October 2002CS 201J Fall Making a Thread // from PS5 Grid class: public void startObjects() // EFFECTS: Start all object threads. { Enumeration els = simobjects.elements (); while (els.hasMoreElements ()) { SimObject current = (SimObject) els.nextElement (); Thread simObjectThread = new Thread (current); simObjectThread.start (); } public class Thread implements Runnable { public Thread (Runnable target) public void start () … many other methods } What do you know about SimObject type?

17 October 2002CS 201J Fall Runnable public interface Runnable { public void run() When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread. The general contract of the method run is that it may take any action whatsoever. } So, to be a subtype of Runnable, SimObject must have a method void run () with no preconditions and any postconditions it wants.

17 October 2002CS 201J Fall Making a Runnable abstract public class SimObject implements Runnable { … public void run () // EFFECTS: Executes one turn by calling the // executeTurn method, and sleeps for a time // and repeats. { while (true) { executeTurn (); delay (TURN_DELAY + random.nextInt(TURN_RANDOM)); }

17 October 2002CS 201J Fall Actually… abstract public class SimObject implements Runnable { … public void run () // REQUIRES: this has been initialized isInitialized // EFFECTS: Executes one turn by calling the // executeTurn method, and sleeps for a time // and repeats. { … } We are violating the substitution principle! SimObject.run() has a stronger precondition than Runnable.run().

17 October 2002CS 201J Fall Concurrency Making a concurrent Java program is easy: Make a subtype R of Runnable new Thread (new R ()).start () Making a concurrent Java program that behaves correctly is really, really hard!

17 October 2002CS 201J Fall Scheduling Meetings Alice wants to schedule a meeting with Bob and Colleen BobAliceColleen “When can you meet Friday?” “11am or 3pm” “9am or 11am” “Let’s meet at 11am” Reserves 11am for meeting Reserves 11am for meeting Picks meeting time

17 October 2002CS 201J Fall Partial Ordering of Events Sequential programs give use a total ordering of events: everything happens in a determined order Concurrency gives us a partial ordering of events: we know some things happen before other things, but not total order Alice asks to schedule meeting before Bob replies Alice asks to schedule meeting before Colleen replies Bob and Colleen both reply before Alice picks meeting time Alice picks meeting time before Bob reserves time on calendar

17 October 2002CS 201J Fall Race Condition BobAliceColleen “When can you meet Friday?” “9, 11am or 3pm” “9am or 11am” “Let’s meet at 11am” Picks meeting time Doug “When can you meet Friday?” “9, 11am or 3pm” “Let’s meet at 11am” Reserves 11am for Doug “I’m busy then…”

17 October 2002CS 201J Fall Preventing Race Conditions Use locks to impose ordering constraints After responding to Alice, Bob reserves all the times in his response until he hears back (and then frees the other times)

17 October 2002CS 201J Fall Locking BobAliceColleen “When can you meet Friday?” “9, 11am or 3pm” “9am or 11am” “Let’s meet at 11am” Picks meeting time Doug “When can you meet Friday?” “3pm” “Let’s meet at 3” Locks calendar

17 October 2002CS 201J Fall Deadlocks BobAliceColleen “When can you meet Friday?” “9, 11am or 3pm” Doug “When can you meet Friday?” Locks calendar for Alice, can’t respond to Doug “When can you meet Friday?” Locks calendar for Doug, can’t respond to Alice Can’t schedule meeting, no response from Bob Can’t schedule meeting, no response from Colleen

17 October 2002CS 201J Fall Why are threads hard? Too few ordering constraints: race conditions Too many ordering constraints: deadlocks Hard/impossible to reason modularly –If an object is accessible to multiple threads, need to think about what any of those threads could do at any time! Testing is even more impossible than it is for sequential code –Even if you test all the inputs, don’t know it will work if threads run in different order

17 October 2002CS 201J Fall Charge Computers are single-threaded machines that provide their owner the illusion of multiple threads. Brains are multi-threaded machines that provide their owner with the illusion of a single thread. Practice with races/deadlocks on Tuesday, no class on Thursday Return exams