Too Much MilkToo Much MilkComputerized Milk PurchaseStill Too Much MilkSecond AttemptThird AttemptFourth AttemptSlide 8CS 140 Lecture Notes: Concurrency Slide 1Too Much MilkPerson A3:00 Arrive home: no milk3:05 Leave for store3:10 Arrive at store3:15 Leave store3:20 Arrive home, put milk awayCS 140 Lecture Notes: Concurrency Slide 2Too Much MilkRoomate A Roomate B3:00 Arrive home: no milk3:05 Leave for store3:10 Arrive at store Arrive home: no milk3:15 Leave store Leave for store3:20 Arrive home, put milk away Arrive at store3:25 Leave store3:30 Arrive home: too much milk!CS 140 Lecture Notes: Concurrency Slide 3Computerized Milk Purchaseif (milk == 0) { if (note == 0) { note = 1; buyMilk(); note = 0; }}CS 140 Lecture Notes: Concurrency Slide 4Still Too Much MilkThread A: Thread B:if (milk == 0) { if (note == 0) {if (milk == 0) { if (note == 0) { note = 1; buyMilk(); note = 0; }} note = 1; buyMilk(); note = 0; }}CS 140 Lecture Notes: Concurrency Slide 5Second AttemptThread A:if (note == 0) { if (milk == 0) { buyMilk(); } note = 1;}Thread B:if (note == 1) { if (milk == 0) { buyMilk(); } note = 0;}CS 140 Lecture Notes: Concurrency Slide 6Third AttemptThread A:1 noteA = 1;2 if (noteB == 0) {3 if (milk == 0) {4 buyMilk();5 } 6 }7 noteA = 0;Thread B:1 noteB = 1;2 if (noteA == 0) {3 if (milk == 0) {4 buyMilk();5 } 6 }7 noteB = 0;CS 140 Lecture Notes: Concurrency Slide 7Fourth AttemptThread A:1 noteA = 1;2 if (noteB == 0) {3 if (milk == 0) {4 buyMilk();5 } 6 }7 noteA = 0;Thread B:1 noteB = 1;2 while (noteA == 1) {3 // do nothing4 }5 if (milk == 0) {6 buyMilk();7 } 8 noteB = 0;CS 140 Lecture Notes: Concurrency Slide
View Full Document