ch4 - Chapter 4 Shared Objects & Mutual Exclusion...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Concurrency: shared objects & mutual exclusion 1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Concurrency: shared objects & mutual exclusion 2 ©Magee/Kramer 2 nd Edition Shared Objects & Mutual Exclusion Concepts : process interference . mutual exclusion . Models : model checking for interference modeling mutual exclusion Practice : thread interference in shared Java objects mutual exclusion in Java ( synchronized objects/methods).
Background image of page 2
Concurrency: shared objects & mutual exclusion 3 ©Magee/Kramer 2 nd Edition 4.1 Interference Garden West Turnstile East Turnstile people People enter an ornamental garden through either of two turnstiles. Management wish to know how many are in the garden at any time. The concurrent program consists of two concurrent threads and a shared counter object. Ornamental garden problem:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Concurrency: shared objects & mutual exclusion 4 ©Magee/Kramer 2 nd Edition The Turnstile thread simulates the periodic arrival of a visitor to the garden every second by sleeping for a second and then invoking the increment() method of the counter object. setvalue() NumberCanvas Applet init() go() Garden Thread Turnstile run() Counter increment() display display east,west people eastD, westD, counterD
Background image of page 4
Concurrency: shared objects & mutual exclusion 5 ©Magee/Kramer 2 nd Edition ornamental garden program private void go() { counter = new Counter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); } The Counter object and Turnstile threads are created by the go() method of the Garden applet: Note that counterD , westD and eastD are objects of NumberCanvas used in chapter 2.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
©Magee/Kramer 2 nd Edition Turnstile class class Turnstile extends Thread { NumberCanvas display; Counter people; Turnstile(NumberCanvas n,Counter c) { display = n; people = c; } public void run() { try { display.setvalue(0); for (int i=1;i<=Garden.MAX;i++){ Thread.sleep(500); //0.5 second between arrivals display.setvalue(i); people.increment(); } } catch (InterruptedException e) {} } } The run() method exits and the thread terminates after Garden.MAX visitors have entered.
Background image of page 6
Concurrency: shared objects & mutual exclusion 7 ©Magee/Kramer 2 nd Edition Counter class class Counter { int value=0; NumberCanvas display; Counter(NumberCanvas n) { display=n; display.setvalue(value); } void increment() { int temp = value; //read value Simulate.HWinterrupt(); value=temp+1; //write value display.setvalue(value);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/08/2012 for the course CSC 6232 taught by Professor Rotenstreich during the Fall '11 term at GWU.

Page1 / 23

ch4 - Chapter 4 Shared Objects & Mutual Exclusion...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online