ch4 - Concurrency: shared objects & mutual...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Concurrency: shared objects & mutual exclusion 1 Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion 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). 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. Or namental gar den pr oblem: Concurrency: shared objects & mutual exclusion 4 Magee/Kramer 2 nd Edition ornamental garden Program - class diagram 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 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. Concurrency: shared objects & mutual exclusion 6 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. 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() {...
View Full Document

Page1 / 23

ch4 - Concurrency: shared objects & mutual...

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