Concurrency: model-based design ©Magee/Kramer Claus Brabrand University of Aarhus Model-Based Design Concurrency.

Slides:



Advertisements
Similar presentations
CSC321 §1 Concurrent Programming 1 CSC 321 Concurrent Programming Course web site
Advertisements

Concurrent Programming Abstraction & Java Threads
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Lecture 8: Three-Level Architectures CS 344R: Robotics Benjamin Kuipers.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrency: model-based design1 ©Magee/Kramer Chapter 8 Model-Based Design.
Concurrency: introduction1 ©Magee/Kramer 2 nd Edition Concurrency State Models and Java Programs Jeff Magee and Jeff Kramer.
Concurrency: safety & liveness properties1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 14 Safety and.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Formal Methods for Software Engineering Lecture, Part II: Liveness.
CP — Concurrent Programming 5. Safety and Liveness Properties Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Spin Tutorial (some verification options). Assertion is always executable and has no other effect on the state of the system than to change the local.
Concurrency.
Model Checking. Used in studying behaviors of reactive systems Typically involves three steps: Create a finite state model (FSM) of the system design.
THE OBJECT-ORIENTED DESIGN WORKFLOW Statechart Diagrams.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Concurrency: Deadlock1 ©Magee/Kramer 2 nd Edition Chapter 6 Deadlock.
Concurrency: shared objects & mutual exclusion1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Cheng/Dillon-Software Engineering: Formal Methods Model Checking.
Timed UML State Machines Ognyana Hristova Tutor: Priv.-Doz. Dr. Thomas Noll June, 2007.
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
(C) 2009 J. M. Garrido1 Object Oriented Simulation with Java.
Concurrency: introduction1 ©Magee/Kramer Concurrency State Models and Java Programs Jeff Magee and Jeff Kramer.
Benjamin Gamble. What is Time?  Can mean many different things to a computer Dynamic Equation Variable System State 2.
Concurrency: processes & threads1 ©Magee/Kramer Chapter 2 Processes & Threads.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Concurrency: introduction1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 5 Introduction to Concurrency:
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 16 th, 2010 The University of Georgia.
Lecture 2 Foundations and Definitions Processes/Threads.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Concurrency: dynamic systems1 ©Magee/Kramer 2 nd Edition Chapter 9 Dynamic Systems.
2015 Concurrency: model-based design 1 ©Magee/Kramer 2 nd Edition Chapter 8 Model-Based Design.
CSC321 §6 Modelling Processes using FSP 1 Chapter 6 Modelling Processes using FSP.
Safety & Liveness Properties
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
CSC321 §10 Safety & Liveness Properties 1 Section 10 Safety & Liveness Properties.
1 Qualitative Reasoning of Distributed Object Design Nima Kaveh & Wolfgang Emmerich Software Systems Engineering Dept. Computer Science University College.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
1 Kyung Hee University Statecharts Spring Kyung Hee University Specifying Objects’ Behaviour  Interaction diagrams show message-passing behaviour.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
1 Model Checking of Robotic Control Systems Presenting: Sebastian Scherer Authors: Sebastian Scherer, Flavio Lerda, and Edmund M. Clarke.
2015 Concurrency: logical properties 1 ©Magee/Kramer 2 nd Edition Chapter 14 Logical Properties Satisfied? Not satisfied?
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
CIS 842: Specification and Verification of Reactive Systems Lecture INTRO-Examples: Simple BIR-Lite Examples Copyright 2004, Matt Dwyer, John Hatcliff,
Exceptions and Assertions Chapter 15 – CSCI 1302.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 10: Statecharts.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Software Systems Verification and Validation Laboratory Assignment 4 Model checking Assignment date: Lab 4 Delivery date: Lab 4, 5.
Winter 2007SEG2101 Chapter 121 Chapter 12 Verification and Validation.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda  Quick Review  Finish Introduction  Java Threads.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Multithreaded Programming
Threads and Multithreading
Foundations and Definitions
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Synchronization and liveness
Presentation transcript:

Concurrency: model-based design ©Magee/Kramer Claus Brabrand University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer Repetition Safety and Liveness Properties

Concurrency: model-based design ©Magee/Kramer Safety & Liveness Properties Concepts : Properties: true for every possible execution Safety : nothing bad happens Liveness : something good eventually happens Models : Safety : no reachable ERROR/STOP state Progress : an action is eventually executed ( fair choice and action priority) Practice : Threads and monitors Aim : property satisfaction.

Concurrency: model-based design ©Magee/Kramer 7.1 Safety  ERROR conditions state what is not required (~ exceptions).  In complex systems, it is usually better to specify safety properties by stating directly what is required. property SAFE_ACTUATOR = (command -> respond -> SAFE_ACTUATOR). ACTUATOR = (command -> (respond -> ACTUATOR |command -> ERROR) |respond -> ERROR). A safety property asserts that nothing bad happens.

Concurrency: model-based design ©Magee/Kramer 7.2 Single Lane Bridge Problem

Concurrency: model-based design ©Magee/Kramer Single Lane Bridge - Safety Property “ONEWAY” property ONEWAY = (red[ID].enter -> RED[1] |blue[ID].enter -> BLUE[1]), RED[i:ID] = (red[ID].enter -> RED[i+1] |when (i==1) red[ID].exit -> ONEWAY |when (i>1) red[ID].exit -> RED[i-1]), BLUE[j:ID]= (blue[ID].enter -> BLUE[j+1] |when (j==1) blue[ID].exit -> ONEWAY |when (j>1) blue[ID].exit -> BLUE[j-1]). We now specify a safety property to check that cars do not collide!: When the bridge is empty, either a red or a blue car may enter. While red cars are on the bridge only red cars can enter; similarly for blue cars.

Concurrency: model-based design ©Magee/Kramer Single Lane Bridge Trace to property violation in ONEWAY: red.1.enter blue.1.enter Without bridge => controller

Concurrency: model-based design ©Magee/Kramer 7.3 Liveness A safety property asserts that nothing bad happens. A liveness property asserts that something good eventually happens. Does every car eventually get an opportunity to cross the bridge (i.e., make progress)? A progress property asserts that it is always the case that an action is eventually executed. Progress is the opposite of starvation = the name given to a concurrent programming situation in which an action is never executed.

Concurrency: model-based design ©Magee/Kramer Progress Properties Suppose that there were two possible coins that could be picked up: a regular coin and a trick coin TWOCOIN = (pick->COIN | pick->TRICK), COIN = (toss->heads->COIN | toss->tails->COIN). TRICK = (toss->heads->TRICK), progress HEADS = {heads} ? progress TAILS = {tails} ?  Terminal Transition Set

Concurrency: model-based design ©Magee/Kramer Revised single lane bridge implementation - FairBridge ||CongestedBridge =(SingleLaneBridge) >> {red[ID].exit,blue[ID].exit}. Low/high action priorities: P >> {low 1, …, low N } P << {high 1, …, high N }

Concurrency: model-based design ©Magee/Kramer 7.5 Readers and Writers

Concurrency: model-based design ©Magee/Kramer Chapter 8 Model-Based Design

Concurrency: model-based design ©Magee/Kramer Model-Based Design Concepts : design process: - from requirements to models - from models to implementations Models : check properties of interest: - safety of the appropriate (sub-)system - progress of the overall system Practice : model “interpretation”: - to infer actual system behavior active threads and passive monitors Aim : rigorous design process. Case Study: cruise controller

Concurrency: model-based design ©Magee/Kramer From Requirements to Models

Concurrency: model-based design ©Magee/Kramer  goals of the system  scenarios (Use-Case models)  properties of interest 8.1 From Requirements to Models Requirements Model 1. identify the main events, actions, and interactions 2. identify and define the main processes 3. identify and define the properties of interest 4. structure the processes into an architecture  check traces of interest  check properties of interest Any appropriate design approach can be used Determine appropriate level of abstraction

Concurrency: model-based design ©Magee/Kramer Example: A Cruise Control System - Requirements When the car ignition is switched on and the ‘on’ button is pressed, the current speed is recorded and the system is enabled: Pressing ‘resume’ or ‘on’ re-enables the system. Pressing the ‘brake’, ‘accelerator’ or ‘off’ button disables the system. buttons Cruise control Cruise control display It maintains the speed of the car at the recorded setting. Requirements:

Concurrency: model-based design ©Magee/Kramer A Cruise Control System - Hardware Wheel Revolution Sensor generates interrupts to enable the car speed to be calculated. Parallel Interface Adapter (PIA) is polled every 100msec. It records the actions of the sensors: buttons ( on, off, resume ) brake (pressed) accelerator (pressed) engine ( on, off ). Output: The cruise control system controls the car speed by setting the throttle via the digital-to-analogue (D/A) converter. throttle

Concurrency: model-based design ©Magee/Kramer Model - Design 1.Identify the main events, actions, and interactions: engineOn, engineOff, accelerator, brake, on, off, resume, speed, setThrottle, zoom, clearSpeed,recordSpeed, enableControl,disableControl.

Concurrency: model-based design ©Magee/Kramer Model - Design 1.Identify the main events, actions, and interactions: engineOn, engineOff, accelerator, brake, on, off, resume, speed, setThrottle, zoom clearSpeed,recordSpeed, enableControl,disableControl. set Sensors = {engineOn, engineOff, accelerator, brake, on, off, resume} set Engine = {engineOn, engineOff} set Prompts = {enableControl, disableControl, clearSpeed, recordSpeed}

Concurrency: model-based design ©Magee/Kramer Model - Outline Design 2. Identify and define the main processes: Input Speed monitors the speed (when the engine is on), and provides the current speed readings to the speed control Sensor Scan monitors the buttons, brake, accelerator and engine events Cruise State Controller triggers clearSpeed and recordSpeed, and enables- /disables the speed control Speed Control clears and records the speed, and sets the throttle accordingly when enabled Throttle sets the actual throttle Sensors PromptsEngine speed setThrottle set Sensors = {engineOn, engineOff, accelerator, brake, on, off, resume} set Engine = {engineOn, engineOff} set Prompts = {enableControl, disableControl, clearSpeed, recordSpeed} Controlling the state (of the controller) Controlling the throttle

Concurrency: model-based design ©Magee/Kramer Model – Design (Step 1+2+3) 1. Identify the main events, actions, and interactions: on, off, resume, brake, accelerator engineOn, engineOff, speed, setThrottle clearSpeed,recordSpeed, enableControl,disableControl 2. Identify and define the main processes: Sensor Scan, Input Speed, Cruise Controller, Speed Control and Throttle 3.Identify and define the main properties of interest: safety: cruise control disabled when off, brake or accelerator pressed. Sensors Prompts

Concurrency: model-based design ©Magee/Kramer Model - Design (Step 4) The CONTROL system is structured as two processes: - CRUISECONTROLLER (controlling the state); and - SPEEDCONTROL (controlling the throttle) 4. Structure the processes into an architecture:

Concurrency: model-based design ©Magee/Kramer Model Elaboration - Process Definitions SENSORSCAN = ({Sensors} -> SENSORSCAN). // monitor speed when engine on INPUTSPEED = (engineOn -> CHECKSPEED), CHECKSPEED = (speed -> CHECKSPEED // monitor speed |engineOff -> INPUTSPEED). // zoom when throttle set THROTTLE =(setThrottle -> zoom -> THROTTLE). // perform speed control when enabled SPEEDCONTROL = DISABLED, DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

Concurrency: model-based design ©Magee/Kramer Model Elaboration - Process Definitions CRUISECONTROLLER = INACTIVE, INACTIVE =(engineOn -> clearSpeed -> ACTIVE), ACTIVE =(engineOff -> INACTIVE |on-> recordSpeed-> enableControl-> CRUISING), // enable speed control when cruising, // disable when off, brake, or accelerator pressed CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl-> STANDBY |on-> recordSpeed-> enableControl-> CRUISING), STANDBY =(engineOff -> INACTIVE |resume -> enableControl-> CRUISING |on-> recordSpeed-> enableControl-> CRUISING).

Concurrency: model-based design ©Magee/Kramer Model - CONTROL Sub-System ||CONTROL = (CRUISECONTROLLER||SPEEDCONTROL). - Is control enabled after the engine is switched on and the ‘on’ button is pressed? - Is control disabled when the brake is then pressed? - Is control re-enabled when resume is then pressed? Testing (animation in LTSA) will check particular traces: Safety: Is the control always disabled when off, brake, or accelerator is pressed? Progress: Can every action eventually be selected? Verification (aka. model-checking) will exhaustively analyze all possible traces:

Concurrency: model-based design ©Magee/Kramer Model - Safety Properties Thus: Safety properties should be composed with the appropriate (sub-)system to which the property refers. Safety checks are compositional! If there is no violation at a sub-system level, then there cannot be a violation when the sub-system is composed with other sub-systems. This is because, if the ERROR state of a particular safety property is unreachable in the LTS of the sub-system, it remains unreachable in any subsequent parallel composition which includes the sub-system. P |= S  Q |= S => (P||Q) |= S /* safety */ property S =...

Concurrency: model-based design ©Magee/Kramer Model - Safety Properties Verify CRUISESAFETY ? ||CONTROL = (CRUISECONTROLLER||SPEEDCONTROL||CRUISESAFETY). property CRUISESAFETY = {on,resume} -> SAFETYCHECK, |{off,accelerator,brake,disableControl} -> CRUISESAFETY SAFETYCHECK = ({off,accelerator,brake} -> SAFETYACTION |disableControl -> CRUISESAFETY |{on,resume} -> SAFETYCHECK), SAFETYACTION = (disableControl->CRUISESAFETY). Composition with CONTROL processes: Is the control always disabled when off / brake / acc pressed?

Concurrency: model-based design ©Magee/Kramer Model Analysis ||CONTROL = (CRUISECONTROLLER || SPEEDCONTROL || {Sensors,speed,setThrottle}. ||CRUISECONTROLSYSTEM = (CONTROL || SENSORSCAN || INPUTSPEED || THROTTLE). We can now compose the whole system: We know there can be no safety violations (in the whole system); i.e. due to composition What about deadlocks? What about progress…? No deadlock/errors !

Concurrency: model-based design ©Magee/Kramer Model - Progress Properties Thus: Progress checks should be conducted on the complete target system after satisfactory completion of the safety checks. Progress checks are not compositional ! Even if there is no progress violation at a sub-system level, a progress violation may “appear” when the sub-system is composed with other sub-systems. This is because an action in the sub-system may satisfy progress yet be unreachable when the sub-system is composed with other sub- systems which constrain system behavior. P |= L  Q |= L => (P||Q) |= L Assume L   (P), L   (Q): (P||Q) |= L => P |= L  Q |= L However, we have that:

Concurrency: model-based design ©Magee/Kramer Model - Progress Properties Progress violation for actions: {engineOn, clearSpeed, engineOff, on, recordSpeed, enableControl, off, disableControl, brake, accelerator} Path to terminal set of states: engineOn tau on tau engineOff engineOn Actions in terminal set: {speed, setThrottle, zoom} Progress check: (clearSpeed) (recordSpeed) (enableControl) When the engine is switched off: - CruiseController becomes inactive, whereas - SpeedControl is not disabled!

Concurrency: model-based design ©Magee/Kramer Cruise Control Model - Minimized LTS ||CRUISEMINIMIZED = {Sensors,speed}. Action hiding and minimization can help to reduce the size of the LTS diagram and make it easier to interpret. Progress violation trace: engineOn -> clearSpeed -> on -> recordSpeed -> enableControl -> engineOff -> engineOn.

Concurrency: model-based design ©Magee/Kramer Model – Revised Cruise Control System // enable speed control when cruising, // disable when off, brake, or accelerator pressed // or when the engine is turned off!!! CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl-> STANDBY |on-> recordSpeed-> enableControl-> CRUISING), Fix CRUISECONTROLLER so that it disables the SPEEDCONTROLLER when the engine is switched off: OK now? -> disableControl

Concurrency: model-based design ©Magee/Kramer Model – Revised Cruise Control System (Properties) property CRUISESAFETYv2 = ({off,accelerator,..., } -> CRUISESAFETYv2 |{on,resume} -> SAFETYCHECK), SAFETYCHECK = ({on,resume} -> SAFETYCHECK |{off,..., } -> SAFETYACTION |disableControl -> CRUISESAFETYv2), SAFETYACTION = (disableControl -> CRUISESAFETYv2). No deadlocks/errors No progress violations detected engineOff

Concurrency: model-based design ©Magee/Kramer Model - System Sensitivities (under Adverse Conditions) ||SPEEDHIGH = CRUISECONTROLSYSTEM << {speed}. Progress violation for actions: {engineOn, engineOff, on, off, brake, accelerator, resume, setThrottle, zoom} Path to terminal set of states: engineOn tau Actions in terminal set: {speed} Indicates that the system may be sensitive to the priority of the action “speed”.

Concurrency: model-based design ©Magee/Kramer Model Interpretation Models can be used to indicate system sensitivities! If it is possible that erroneous situations detected in the model may occur in the implemented system, then the model should be revised to find a design which ensures that those violations are avoided. However, if it is considered that the real system will not exhibit this behavior, then no further model revisions are necessary. Model interpretation and correspondence to the implementation are important in determining the relevance and adequacy of the model design and its analysis.

Concurrency: model-based design ©Magee/Kramer From Models to Implementation

Concurrency: model-based design ©Magee/Kramer The Central Role of Design Architecture Design architecture describes the gross organization and global structure of the system in terms of its constituent components. We consider that the implementation should be considered as an elaborated view of the basic design architecture. S = M 0  M 1  M 2 ...  M  = I // Incremental model refinement

Concurrency: model-based design ©Magee/Kramer 8.2 From Models to Implementations Model Java  identify the main active entities - to be implemented as threads  identify the main (shared) passive entities - to be implemented as monitors  (identify the interactive display environment - to be implemented as associated classes)  structure the classes as a (UML) class diagram - to be implemented

Concurrency: model-based design ©Magee/Kramer Cruise Control System - Class Diagram SpeedControl interacts with the car simulation via interface CarSpeed. CRUISECONTROLLERSPEEDCONTROL

Concurrency: model-based design ©Magee/Kramer Class state- Controller class StateController { final static int INACTIVE = 0, ACTIVE = 1, CRUISING = 2, STANDBY = 3; // controller states protected int state = INACTIVE; // initial state protected SpeedControl sc; StateController(CarSpeed cs, CruiseDisplay disp) { sc = new SpeedControl(cs, disp); } synchronized void brake() { if (state == CRUISING) { sc.disableControl(); state = STANDBY; } } synchronized void accelerator() { if (state == CRUISING) { sc.disableControl(); state = STANDBY; } } synchronized void engineOff() { if (state != INACTIVE) { if (state == CRUISING) sc.disableControl(); state = INACTIVE; } } Controller is a passive entity (it reacts to events) and thus implemented as a monitor CRUISECONTROLLER = INACTIVE, INACTIVE =(engineOn -> clearSpeed -> ACTIVE), ACTIVE =(engineOff -> INACTIVE |on-> recordSpeed-> enableControl -> CRUISING), CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl -> STANDBY |on-> recordSpeed-> enableControl -> CRUISING), STANDBY =(engineOff -> disableControl-> INACTIVE |resume -> enableControl -> CRUISING |on-> recordSpeed-> enableControl -> CRUISING).

Concurrency: model-based design ©Magee/Kramer Class state- Controller... synchronized void engineOn() { if (state == INACTIVE) { sc.clearSpeed(); state = ACTIVE; } } synchronized void on() { if (state != INACTIVE) { sc.recordSpeed(); sc.enableControl(); state = CRUISING; } } synchronized void off() { if (state == CRUISING) { sc.disableControl(); state = STANDBY; } } synchronized void resume() { if (state == STANDBY) { sc.enableControl(); state = CRUISING; } } } CRUISECONTROLLER = INACTIVE, INACTIVE =(engineOn -> clearSpeed -> ACTIVE), ACTIVE =(engineOff -> INACTIVE |on-> recordSpeed-> enableControl -> CRUISING), CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl -> STANDBY |on-> recordSpeed-> enableControl -> CRUISING), STANDBY =(engineOff -> disableControl-> INACTIVE |resume -> enableControl -> CRUISING |on-> recordSpeed-> enableControl -> CRUISING).

Concurrency: model-based design ©Magee/Kramer Class SpeedControl class SpeedControl implements Runnable { final static int DISABLED = 0; // speed control states final static int ENABLED = 1; protected int state = DISABLED; // initial state protected int set_speed = 0; // initial speed setting protected Thread sc; protected CarSpeed cs; // interface to the car (simulator) protected CruiseDisplay disp; SpeedControl(CarSpeed c, CruiseDisplay d){ this.cs = c; this.disp = d; d.disable(); d.record(0); } synchronized void recordSpeed() { set_speed = cs.getSpeed(); disp.record(set_speed); } synchronized void clearSpeed() { if (state == DISABLED) { set_speed = 0; disp.record(set_speed); } } SpeedControl is an active entity; when enabled, a new thread is created (which periodically obtains car speed and sets the throttle). SPEEDCONTROL = DISABLED, DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

Concurrency: model-based design ©Magee/Kramer Class SpeedControl... synchronized void enableControl() { if (state == DISABLED) { disp.enable(); sc = new Thread(this); sc.start(); state = ENABLED; } } synchronized void disableControl() { if (state == ENABLED) { disp.disable(); state = DISABLED; } }... SPEEDCONTROL = DISABLED, DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

Concurrency: model-based design ©Magee/Kramer Class SpeedControl synchronized private double speed() { return...cs.getSpeed()...; } synchronized private void setThrottle(double throttle) { cs.setThrottle(...throttle...); } public void run() { // the speed controller thread try { while (state == ENABLED) { double s = speed(); setThrottle(s); Thread.sleep(500); } } catch (InterruptedException _) {} sc = null; // throw away SpeedController thread } SpeedControl is an example of a class that combines both - synchronized methods (to update local vars); and - a thread. SPEEDCONTROL = DISABLED, DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

Concurrency: model-based design ©Magee/Kramer Summary: Model-Based Design Concepts : design process: - from requirements to models - from models to implementations Models : check properties of interest: - safety on the appropriate (sub)system - progress on the overall system Practice : model “interpretation”: - to infer actual system behavior active threads and passive monitors Aim : rigorous design process.

Concurrency: model-based design ©Magee/Kramer class Interleave extends Thread { public static final int K = 10; protected static int x = 0; // shared integer variable public void run() { for (int i=0; i<K; i++) { // repeat `K' times: x = x + 1; } public static void main(String[] args) { // create two threads (processes: p and q) Thread p = new Interleave(); Thread q = new Interleave(); // start the threads... p.start(); q.start(); try { //...and wait for them to finish p.join(); q.join(); // Finally, write value of x to standard out System.out.println("x = " + x); } catch(InterruptedException _) {} } Final possible values of x?!

Concurrency: model-based design ©Magee/Kramer Course Outline  Processes and Threads [MK 1+2]  Concurrent Execution [MK 3]  Shared Objects & Interference [MK 4]  Monitors & Condition Synchronization [MK 5]  Deadlock [MK 6]  Safety and Liveness Properties [MK 7]  Model-Based Design [MK 8]  Mini-Project: groups of 2-3; Wednesday at 15:00  Multiple-choice assignment: individual, “on-site”