ch9 - Chapter 9 Dynamic Systems Concurrency: dynamic...

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

View Full Document Right Arrow Icon
Concurrency: dynamic systems 1 ©Magee/Kramer 2 nd Edition Chapter 9 Dynamic Systems
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: dynamic systems 2 ©Magee/Kramer 2 nd Edition Dynamic Systems Concepts : dynamic creation and deletion of processes Resource allocation example varying number of users and resources. master-slave interaction Models : static - fixed populations with cyclic behavior interaction Practice : dynamic creation and deletion of threads (# active threads varies during execution) Resource allocation algorithms Java join() method
Background image of page 2
9.1 Golf Club Program Expert players tend not to lose any golf balls and only hire one or two. Novice 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.
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: 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); }
Background image of page 4
Concurrency: dynamic systems 5 ©Magee/Kramer 2 nd Edition Java implementation - SimpleAllocator monitor get blocks a calling thread until sufficient golf balls are 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 thread requesting a large number of balls may be overtaken and remain blocked!
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 terminates after releasing golf balls. New player threads are created dynamically.
Background image of page 6
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] ->BALL[b+j] ).
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.

Page1 / 28

ch9 - Chapter 9 Dynamic Systems Concurrency: dynamic...

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