180 likes | 195 Views
cs205: engineering software university of virginia fall 2006. Locking. Canyon Impressionism by Patrick Harrison. Multiple Threads. Store. Shared Data. Instruction Streams. Programming Concurrency. Java API: Thread A separate execution thread Methods Thread (Runnable r)
E N D
cs205: engineering software university of virginia fall 2006 Locking Canyon Impressionism by Patrick Harrison
Multiple Threads Store Shared Data Instruction Streams
Programming Concurrency • Java API: Thread • A separate execution thread • Methods Thread (Runnable r) Create a thread. The thread’s run method will invokve r.run (). start () REQUIRES: this is not already started Schedule the thread to run. The VM will start the thread and involve run ().
Yarn publicclass Yarn extends Thread { publicvoid run () { System.err.println ("Running thread: " + currentThread ()); } publicstaticvoid main (String args[]) { Yarn y = new Yarn (); y.start (); System.err.println ("Done: " + currentThread ()); } } What could this produce?
Yarn publicclass Yarn extends Thread { publicvoid run () { System.err.println ("Running thread: " + currentThread ()); } publicstaticvoid main (String args[]) { Yarn y = new Yarn (); y.start (); System.err.println ("Done: " + currentThread ()); } } Done: Thread[main,5,main] Running thread: Thread[Thread-0,5,main] Actual output: Plausible output: Running thread: Thread[Thread-0,5,main] Done: Thread[main,5,main]
Yarn Done: Thread[main,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] Running thread: Thread[Thread-0,5,main] publicclass Yarn extends Thread { publicvoid run () { while (true) { System.err.println ("Running thread: " + currentThread ()); } } publicstaticvoid main (String args[]) { Yarn y = new Yarn (); y.start (); System.err.println ("Done: " + currentThread ()); } }
classCounter{ privateintcount; publicCounter(){count=0;} publicvoidincrement(){count++;} publicvoiddecrement(){count--;} publicintgetValue(){returncount;} } classIncThreadextendsThread{ privateCounterc; publicIncThread(Counterp_c){c=p_c;} publicvoidrun(){ while(true){ c.increment(); System.err.println("Running inc thread: "+currentThread() +" / Value: "+c.getValue()); } } }
classDecThreadextendsThread{ privateCounterc; publicDecThread(Counterp_c){c=p_c;} publicvoidrun(){ while(true){ c.decrement(); System.err.println("Running dec thread: "+currentThread() +" / Value: "+c.getValue()); } } } publicclassYarn{ publicstaticvoidmain(Stringargs[]){ Counterc=newCounter(); IncThreadithread=newIncThread(c); DecThreaddthread=newDecThread(c); ithread.start(); dthread.start(); } }
Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running inc thread: Thread[Thread-0,5,main] / Value: 2 Running inc thread: Thread[Thread-0,5,main] / Value: 3 Running inc thread: Thread[Thread-0,5,main] / Value: 4 … Running inc thread: Thread[Thread-0,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running inc thread: Thread[Thread-0,5,main] / Value: 62 Running dec thread: Thread[Thread-1,5,main] / Value: 61 Running dec thread: Thread[Thread-1,5,main] / Value: 60 … Running dec thread: Thread[Thread-1,5,main] / Value: 1 Running dec thread: Thread[Thread-1,5,main] / Value: 0 Running dec thread: Thread[Thread-1,5,main] / Value: -1 Running inc thread: Thread[Thread-0,5,main] / Value: -1 Running inc thread: Thread[Thread-0,5,main] / Value: 0 Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running inc thread: Thread[Thread-0,5,main] / Value: 2 …
PS5 Design Reviews • Turn in your answer to question 7 on paper in class Monday • Sign up for a design review time now • Come to your design review prepared to explain and answer questions about your design
Scheduling Meetings Alice wants to schedule a meeting with Bob and Colleen “When can you meet Friday?” “When can you meet Friday?” Bob Alice 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”
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
Race Condition “When can you meet Friday?” “When can you meet Friday?” Bob 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…”
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) Richard Hsu and Mike Liu
Locking “When can you meet Friday?” “When can you meet Friday?” Bob 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”
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
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
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.