Chapter 8 Implementing FSP Models in Java



Similar documents
Lecture 6: Introduction to Monitors and Semaphores

Monitors & Condition Synchronization

3C03 Concurrency: Condition Synchronisation

Outline of this lecture G52CON: Concepts of Concurrency

Parallel Programming

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Concurrent Programming

Chapter 6 Concurrent Programming

Lecture 8: Safety and Liveness Properties

Creating a Simple, Multithreaded Chat System with Java

Last Class: Semaphores

CS11 Java. Fall Lecture 7

Threads & Tasks: Executor Framework

TESTING JAVA MONITORS BY STATE SPACE EXPLORATION MONICA HERNANDEZ. Presented to the Faculty of the Graduate School of

MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors

Threads 1. When writing games you need to do more than one thing at once.

Resurrecting Ada s Rendez-Vous in Java

Mutual Exclusion using Monitors

OBJECT ORIENTED PROGRAMMING LANGUAGE

Concurrent programming in Java

4. Monitors. Monitors support data encapsulation and information hiding and are easily adapted to an object-oriented environment.

Lecture 6: Semaphores and Monitors

Using Files as Input/Output in Java 5.0 Applications

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Multithreaded Programming

Monitors, Java, Threads and Processes

Building a Multi-Threaded Web Server

Monitors and Semaphores

AP Computer Science Static Methods, Strings, User Input

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

Word Count Code using MR2 Classes and API

How To Run A Thread On A Microsoft Macbook (A.A.S.A) (A) Or Macbook) (Macrosoft) (B.A).A.I.A

JAVA - MULTITHREADING

Java Memory Model: Content

Socket-based Network Communication in J2SE and J2ME

Traditional Software Development. Model Requirements and JAVA Programs. Formal Verification & Validation. What is a state?

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

Java Coding Practices for Improved Application Performance

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Crash Course in Java

Introduction to Object-Oriented Programming

CS5233 Components Models and Engineering

Tutorial: Getting Started

Deadlock Victim. dimanche 6 mai 12

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Network Communication

Lesson: All About Sockets

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

WRITING DATA TO A BINARY FILE

JAVA Program For Processing SMS Messages

Getting Started with the Internet Communications Engine

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Object-Oriented Programming in Java

Hoare-Style Monitors for Java

Transparent Redirection of Network Sockets 1

An Overview of Java. overview-1

Transparent Redirection of Network Sockets 1

Object Oriented Software Design

A generic framework for game development

Chapter 7: Sequential Data Structures

Object Oriented Software Design

Java Concurrency Framework. Sidartha Gracias

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

JAVA ARRAY EXAMPLE PDF

Advanced Multiprocessor Programming

Manual. Programmer's Guide for Java API

Chapter 2 Introduction to Java programming

Using the CoreSight ITM for debug and testing in RTX applications

Amazon Glacier. Developer Guide API Version

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Working With Hadoop. Important Terminology. Important Terminology. Anatomy of MapReduce Job Run. Important Terminology

Network Programming using sockets

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Sample CSE8A midterm Multiple Choice (circle one)

HW3: Programming with stacks

The following program is aiming to extract from a simple text file an analysis of the content such as:

Web Development in Java

Java Network. Slides prepared by : Farzana Rahman

Building Java Programs

Capario B2B EDI Transaction Connection. Technical Specification for B2B Clients

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Overview. CMSC 330: Organization of Programming Languages. Synchronization Example (Java 1.5) Lock Interface (Java 1.5) ReentrantLock Class (Java 1.

Running Hadoop on Windows CCNP Server

Object-Oriented Programming in Java

Transcription:

Chapter 8 Implementing FSP Models in Java 1

8.1.1: The Carpark Model A controller is required for a carpark, which only permits cars to enter when the carpark is not full and does not permit cars to leave when there are no cars in the carpark. Car arrival and departure are simulated by separate threads. 2

Carpark model Events or actions of interest? arrive and depart Identify processes. arrivals, departures and carpark control Define each process and interactions (structure). 3

Carpark model CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = ( when(i>0) arrive->spaces[i-1] when(i<n) depart->spaces[i+1] ). ARRIVALS = (arrive->arrivals). DEPARTURES = (depart->departures). CARPARK = (ARRIVALS CARPARKCONTROL(4) DEPARTURES). Guarded actions are used to control arrive and depart. 4

Carpark model 5

8.1.2: Carpark Program Model - all entities are processes interacting by actions Program - need to identify threads and monitors thread - active entity which initiates (output) actions monitor - passive entity which responds to (input) actions. 6

Carpark Program public class CarPark { public static void main(string[] args) { CarParkControl carpark = new CarParkControl(4); Thread arrivals = new Thread(new Arrivals(carpark)); Thread departures = new Thread(new Departures(carpark)); arrivals.start(); departures.start(); 7

Carpark program - Arrivals and Departures threads class Arrivals implements Runnable { CarParkControl carpark; Arrivals(CarParkControl c) {carpark = c; public void run() { try { while(true) { carpark.arrive(); Time.delay(RandomGenerator.integer(0,520)); catch (InterruptedException e){ Similarly Departures which calls carpark.depart(). How do we implement the control of CarParkControl? 8

Carpark program - CarParkControl monitor class CarParkControl { protected int spaces; protected int capacity; CarParkControl(int capacity) {capacity = spaces = n; synchronized void arrive() { --spaces; synchronized void depart() { ++spaces; mutual exclusion by synch methods condition synchronization? block if full? (spaces==0) block if empty? (spaces==n) 9

Carpark program - FSP to Java FSP: when cond act -> NEWSTAT Java: public synchronized void act() throws InterruptedException { while (!cond) wait(); // modify monitor data notifyall() The while loop is necessary to retest the condition cond to ensure that cond is indeed satisfied when it re-enters the monitor. notifyall() is necessary to awaken other thread(s) that may be waiting to enter the monitor now that the monitor data has been changed. 10

CarParkControl - condition synchronization class CarParkControl { protected int spaces; protected int capacity; CarParkControl(int capacity) {capacity = spaces = n; synchronized void arrive() throws InterruptedException { while (spaces==0) wait(); --spaces; notify(); synchronized void depart() throws InterruptedException { while (spaces==capacity) wait(); ++spaces; notify(); Why is it safe to use notify() here rather than notifyall()? 11

Models to monitors - summary Active entities (that initiate actions) are implemented as threads. Passive entities (that respond to actions) are implemented as monitors. Each guarded action in the model of a monitor is implemented as a synchronized method which uses a while loop and wait() to implement the guard. The while loop condition is the negation of the model guard condition. Changes in the state of the monitor are signaled to waiting threads using notify() or notifyall(). 12

8.2 Bounded Buffer A bounded buffer consists of a fixed number of slots. Items are put into the buffer by a producer process and removed by a consumer process. It can be used to smooth out transfer rates between the producer and consumer. (see car park example) 13

bounded buffer - a data-independent model The behaviour of BOUNDEDBUFFER is independent of the actual data values, and so can be modelled in a data-independent manner. LTS: put put put put put 0 1 2 3 4 5 get get get get get 14

bounded buffer - a data-independent model BUFFER(N=5) = COUNT[0], COUNT[i:0..N] = (when (i<n) append->count[i+1] when (i>0) take->count[i-1] ). PRODUCER = (append->producer). CONSUMER = (take->consumer). BOUNDEDBUFFER = (PRODUCER BUFFER(5) CONSUMER). 15

bounded buffer - an implementation class Buffer { private int N =...; private Character[] buffer = new Character [N]; private int tail = 0, head = 0; private int count = 0; public synchronized void append (Character c) { while (count == N) try {wait(); catch (InterruptedException e){ buffer[tail] = c; tail = (tail + 1) % N; count++; notifyall(); public synchronized Character take() { Character c; while (count == 0) try {wait(); catch (InterruptedException e){ c = buffer[head]; head = (head + 1) % N; count--; notifyall(); return c; 16

bounded buffer program - producer process class Producer implements Runnable { Buffer buf; String alphabet= "abcdefghijklmnopqrstuvwxyz"; Producer(Buffer b) {buf = b; Similarly Consumer which calls buf.take(). public void run() { int ai = 0; while(true) { buf.append(new Character(alphabet.charAt(ai))); ai=(ai+1) % alphabet.length(); 17

Summary Concepts monitors: encapsulated data + access procedures mutual exclusion + condition synchronization Model guarded actions Practice private data and synchronized methods in Java wait(), notify() and notifyall() for condition synchronization single thread active in the monitor at a time 18