ch9 - Concurrency: dynamic systems 1 Magee/Kramer 2 nd...

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: dynamic systems 1 Magee/Kramer 2 nd Edition Chapter 9 Dynamic Systems Concurrency: dynamic systems 2 Magee/Kramer 2 nd Edition Dynamic Systems Concepts : dynamic creation and deletion of pr ocesses Resource allocation example varying number of users and resources. master -slave interaction Models : static - fixed populations with cyclic behavior inter action Practice : dynamic creation and deletion of thr eads (# active threads varies during execution) Resource allocation algorithms Java join() method Concurrency: dynamic systems 3 Magee/Kramer 2 nd Edition 9.1 Golf Club Program Exper t players tend not to lose any golf balls and only hire one or two. N ovice players hire more balls, so that they have spares during the game in case of loss. However, they buy replacements for lost balls so that they return the same number that they originally hired. Player d4 is waiting for four balls Players at a Golf Club hire golf balls and then return them after use. Concurrency: dynamic systems 4 Magee/Kramer 2 nd Edition Golf Club - Java implementation The Java interface Allocator permits us to develop a few implementations of the golf ball allocator without modifying the rest of the program. DisplayAllocator class implements this interface and delegates calls to get and put to SimpleAllocator . GolfClub init() start() getGolfBalls() relGolfBalls() Player PlayerArrival Thread Applet SimpleAllocator DisplayAllocator StringCanvas Allocator get() put() alloc display allocDisplay SlotCanvas enter() leave() starting waiting playing ending run() new * gc public interface Allocator { public void get(int n) throws InterruptedException; public void put(int n); } Concurrency: dynamic systems 5 Magee/Kramer 2 nd Edition Java implementation - SimpleAllocator monitor get blocks a calling thr ead until sufficient golf balls ar e available. public class SimpleAllocator implements Allocator{ private int available; public SimpleAllocator(int n) { available = n; } synchronized public void get(int n) throws InterruptedException { while (n>available) wait(); available -= n; } synchronized public void put(int n) { available += n; notifyAll(); } } A novice thr ead r equesting a lar ge number of balls may be over taken and r emain blocked! Concurrency: dynamic systems 6 Magee/Kramer 2 nd Edition Java implementation - Player thread class Player extends Thread { private GolfClub gc; private String name; private int nballs; Player(GolfClub g, int n, String s) { gc = g; name = s; nballs =n; } public void run() { try { gc.getGolfBalls(nballs,name); Thread.sleep(gc.playTime); gc.relGolfBalls(nballs,name); } catch (InterruptedException e){} } } The run() method ter minates after r eleasing golf balls. N ew player thr eads ar e cr eated dynamically. Concurrency: dynamic systems 7 Magee/Kramer 2 nd Edition 9.2 Golf Club Model const N=5 //maximum #golf balls range B=0..N //available range ALLOCATOR = BALL[N], BALL[b:B] = (when (b>0) get[i:1..b]->BALL[b-i] |put[j:1..N] |put[j:1....
View Full Document

Page1 / 28

ch9 - Concurrency: dynamic systems 1 Magee/Kramer 2 nd...

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