1 / 18

cs205: engineering software university of virginia fall 2006

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)

Download Presentation

cs205: engineering software university of virginia fall 2006

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. cs205: engineering software university of virginia fall 2006 Locking Canyon Impressionism by Patrick Harrison

  2. Multiple Threads Store Shared Data Instruction Streams

  3. 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 ().

  4. 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?

  5. 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]

  6. 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 ()); } }

  7. 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()); } } }

  8. 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(); } }

  9. 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 …

  10. 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

  11. 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”

  12. 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

  13. 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…”

  14. 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

  15. 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”

  16. 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

  17. 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

  18. 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.

More Related