Lecture 15: Concurring Concurrently CS201j: Engineering Software

Slides:



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

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.
Object-Oriented Software Engineering Concurrent Programming.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java: Animation Chris North cs3724: HCI. Animation? Changing graphics over time Examples: cartoons Clippy, agents/assistants Hour glass Save dohicky Progress.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
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.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
111 © 2002, Cisco Systems, Inc. All rights reserved.
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.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
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.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Multithreaded Programming in Java David Meredith Aalborg University.
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)
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
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.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
CSCD 330 Network Programming
Multithreading / Concurrency
Concepts of Object Oriented Programming
Threads and Multithreading
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Lecture 21 Concurrency Introduction
Locking cs205: engineering software
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
ITEC324 Principle of CS III
Threads and Memory Models Hal Perkins Autumn 2011
Concurring Concurrently
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
Multithreading.
Threads and Multithreading
Data Abstraction David Evans cs205: engineering software
Threads and Memory Models Hal Perkins Autumn 2009
Threads and Multithreading
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads and Multithreading
Lecture 4: Data Abstraction CS201j: Engineering Software
cs205: engineering software
Unit 1 Lab14 & Lab15.
Threads and Multithreading
IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.
Multithreading in java.
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
David Evans Lecture 19: ||ism I don’t think we have found the right programming concepts for parallel computers yet.
Threads.
CSE 153 Design of Operating Systems Winter 2019
Lecture 13: Subtyping Rules Killer Bear Climber
Multithreaded Programming in Java
CSE 332: Concurrency and Locks
EECE.4810/EECE.5730 Operating Systems
CMSC 202 Threads.
Threads and concurrency / Safety
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans Lecture 15: Concurring Concurrently CS201j: Engineering Software University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

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

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 23 October 2003 CS 201J Fall 2003

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 } 23 October 2003 CS 201J Fall 2003

Making a Thread // from PS5 SimObject class: public class Thread implements Runnable { public Thread (Runnable target) public void start () … many other methods } // from PS5 SimObject class: final public void init(int x, int y, Grid grid) // REQUIRES: init has not previously been called for this. // The cell is at (row, col) on grid. // MODIFIES: this // EFFECTS: Initializes the cell at row, col on grid with isPaused = true. // NOTE: This method is final, that means subtypes cannot override this method. { this.mx = x; this.my = y; this.grid = grid; this.isPaused = true; Thread thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); //@set isInitialized = true; } What do you know about SimObject type? 23 October 2003 CS 201J Fall 2003

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. 23 October 2003 CS 201J Fall 2003

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)); } 23 October 2003 CS 201J Fall 2003

Actually… abstract public class SimObject implements Runnable { … public void run () // REQUIRES: this has been initialized //@also_requires 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(). 23 October 2003 CS 201J Fall 2003

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! 23 October 2003 CS 201J Fall 2003

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

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 23 October 2003 CS 201J Fall 2003

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

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) 23 October 2003 CS 201J Fall 2003

Locking Bob “When can you meet Friday?” Alice Colleen Doug “When can you meet Friday?” “9, 11am or 3pm” “9am or 11am” Picks meeting time Locks calendar “Let’s meet at 11am” “3pm” “Let’s meet at 11am” “Let’s meet at 3” 23 October 2003 CS 201J Fall 2003

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

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 23 October 2003 CS 201J Fall 2003

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. Friday section: return exams, practice with subtyping and concurrency 23 October 2003 CS 201J Fall 2003