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

Info iconThis preview shows pages 1–7. 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
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 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 Concurrency: dynamic systems 3 Magee/Kramer 2 nd Edition 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. 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 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! 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 terminates after releasing golf balls....
View Full Document

Page1 / 28

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

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

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