CS 140 Lecture Notes: ConcurrencySlide 1 Too Much Milk Person A 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at store 3:15Leave store 3:20Arrive home, put milk away
CS 140 Lecture Notes: ConcurrencySlide 2 Too Much Milk Person APerson B 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at storeArrive home: no milk 3:15Leave storeLeave for store 3:20Arrive home, put milk awayArrive at store 3:25Leave store 3:30Arrive home: too much milk!
CS 140 Lecture Notes: ConcurrencySlide 3 Computerized Milk Purchase if (!GotMilk) { if (NoNote) { Leave Note; Buy Milk; Remove Note; }
CS 140 Lecture Notes: ConcurrencySlide 4 Still Too Much Milk Thread A:Thread B: if (!GotMilk) { if (NoNote) { if (!GotMilk) { if (NoNote) { Leave Note; Buy Milk; Remove Note; } Leave Note; Buy Milk; Remove Note; }
CS 140 Lecture Notes: ConcurrencySlide 5 Second Attempt Thread A: if (NoNote) { if (!GotMilk) { Buy Milk; } Leave Note; } Thread B: if (Note) { if (!GotMilk) { Buy Milk; } Remove Note; }
CS 140 Lecture Notes: ConcurrencySlide 6 Third Attempt Thread A: 1 Leave NoteA; 2 if (No NoteB) { 3 if (!GotMilk) { 4 Buy Milk; 5 } 6 } 7Remove NoteA; Thread B: 1 Leave NoteB; 2 if (No NoteA) { 3 if (!GotMilk) { 4 Buy Milk; 5 } 6 } 7Remove NoteB;
CS 140 Lecture Notes: ConcurrencySlide 7 Fourth Attempt Thread A: 1 Leave NoteA; 2 if (No NoteB) { 3 if (!GotMilk) { 4 Buy Milk; 5 } 6 } 7Remove NoteA; Thread B: 1 Leave NoteB; 2 while (NoteA) { 3 do nothing 4 } 5 if (!GotMilk) { 6 Buy Milk; 7 } 8Remove NoteB;
CS 140 Lecture Notes: ConcurrencySlide 8