Lecture 15 Concurring Concurrently CS 201 j Engineering

  • Slides: 17
Download presentation
Lecture 15: Concurring Concurrently CS 201 j: Engineering Software University Virginia 23 Octoberof 2003

Lecture 15: Concurring Concurrently CS 201 j: Engineering Software University Virginia 23 Octoberof 2003 Computer Science CS 201 J Fall 2003 David Evans http: //www. cs. virginia. edu/evans

Our computer can only do one instruction at a time, why would we want

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 201 J Fall 2003 2

Concurrent Programming • Some problems are clearer to program concurrently: – Modularity • Don’t

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 201 J Fall 2003 3

Concurrency in Java public class Thread implements Runnable { // OVERVIEW: A thread is

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 201 J Fall 2003 4

Making a Thread public class Thread implements Runnable { public Thread (Runnable target) public

Making a Thread public class Thread implements Runnable { public Thread (Runnable target) public void start () … many other methods } // from PS 5 Sim. Object 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 is. Paused = true. // NOTE: This method is final, that means subtypes cannot override this method. { this. mx = x; this. my = y; this. grid = grid; this. Paused = true; Thread thread = new Thread(this); thread. set. Priority(Thread. MIN_PRIORITY); thread. start(); //@set is. Initialized = true; } What do you know about Sim. Object type? 23 October 2003 CS 201 J Fall 2003 5

Runnable public interface Runnable { public void run() When an object implementing interface Runnable

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, Sim. Object must have a method void run () with no preconditions and any postconditions it wants. 23 October 2003 CS 201 J Fall 2003 6

Making a Runnable abstract public class Sim. Object implements Runnable { … public void

Making a Runnable abstract public class Sim. Object implements Runnable { … public void run () // EFFECTS: Executes one turn by calling the // execute. Turn method, and sleeps for a time // and repeats. { while (true) { execute. Turn (); delay (TURN_DELAY + random. next. Int(TURN_RANDOM)); } } 23 October 2003 CS 201 J Fall 2003 7

Actually… abstract public class Sim. Object implements Runnable { … public void run ()

Actually… abstract public class Sim. Object implements Runnable { … public void run () // REQUIRES: this has been initialized //@also_requires is. Initialized // EFFECTS: Executes one turn by calling the // execute. Turn method, and sleeps for a time // and repeats. { We are violating the substitution principle! … Sim. Object. run() has a stronger precondition } than Runnable. run(). 23 October 2003 CS 201 J Fall 2003 8

Concurrency • Making a concurrent Java program is easy: Make a subtype R of

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 201 J Fall 2003 9

Scheduling Meetings • Alice wants to schedule a meeting with Bob and Colleen Bob

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 “ 11 am or 3 pm” “ 9 am or 11 am” Reserves 11 am for meeting 23 October 2003 Picks meeting time “Let’s meet at 11 am” CS 201 J Fall 2003 Reserves 11 am for meeting 10

Partial Ordering of Events • Sequential programs give use a total ordering of events:

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 before Bob reserves time on calendar 23 October 2003 CS 201 J Fall 2003 11

Race Condition Bob Doug “When can you meet Friday? ” Alice “When can you

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

Preventing Race Conditions • Use locks to impose ordering constraints • After responding to

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 201 J Fall 2003 13

Locking Doug “When can you meet Friday? ” Bob “When can you meet Friday?

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

Doug “When can Bob you meet Friday? ” “When can you meet Friday? ”

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

Why are threads hard? • Too few ordering constraints: race conditions • Too many

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 201 J Fall 2003 16

Charge • Computers are single-threaded machines that provide their owner the illusion of multiple

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 201 J Fall 2003 17