L17: Model-based Development t n i r p t o n o D ! C - - PowerPoint PPT Presentation

l17 model based development
SMART_READER_LITE
LIVE PREVIEW

L17: Model-based Development t n i r p t o n o D ! C - - PowerPoint PPT Presentation

L17: Model-based Development t n i r p t o n o D ! C esar S anchez n o i t c u r t s n o c r e d Grado en Ingenier a Inform atica n U Grado en Matem aticas e Inform atica Universidad Polit


slide-1
SLIDE 1

L17: Model-based Development

Este texto se distribuye bajo los t´ erminos de la Creative Commons License

C´ esar S´ anchez

Grado en Ingenier´ ıa Inform´ atica Grado en Matem´ aticas e Inform´ atica Universidad Polit´ ecnica de Madrid Wed, 15-April-2015

U n d e r c

  • n

s t r u c t i

  • n

! D

  • n
  • t

p r i n t

slide-2
SLIDE 2

HW7

Homework: HW1: Creaci´

  • n de threads en Java

HW2: Provocar una condici´

  • n de carrera

HW3: Garanatizar la exclusi´

  • n mutua con espera activa

HW4: Garantizar la exlusi´

  • n mutua con sem´

aforos HW5: Almac´ en de un dato con sem´ aforos HW6: Almac´ en de varios datos con sem´ aforos HW7: Especificaci´

  • n de un recurso compartido

HW8: Multibuffer con m´ etodos synchronized Fecha de Cierre: Viernes 17-Abril-2015 11:00am Entrega online: http://lml.ls.fi.upm.es/~entrega

slide-3
SLIDE 3

HW7: Multibuffer con m´ etodos synchronized

slide-4
SLIDE 4

HW7: Multibuffer con m´ etodos synchronized

slide-5
SLIDE 5

HW7: Multibuffer con m´ etodos synchronized

slide-6
SLIDE 6

Model-Based Development

Requirements

slide-7
SLIDE 7

Model-Based Development

Requirements Concurrent Interaction Model Formal, precise

slide-8
SLIDE 8

Model-Based Development

Requirements Concurrent Interaction Model Formal, precise 2 activities Simulation Testing yes no fix

slide-9
SLIDE 9

Model-Based Development

Requirements Concurrent Interaction Model Formal, precise 2 activities Simulation Testing yes no fix Code Generation recurso.java

slide-10
SLIDE 10

Model-Based Development

Model Simulation Testing yes no fix Code Generation recurso.java

slide-11
SLIDE 11

Model-Based Development

Model Simulation Testing yes no fix Code Generation recurso.java

slide-12
SLIDE 12

Model-Based Development

Model Simulation Testing yes no fix Code Generation recurso.java automatic systematic synchronized methods monitors message passing

slide-13
SLIDE 13

Model-Based Development

Model Simulation Testing yes no fix Code Generation recurso.java automatic systematic synchronized methods monitors message passing Today: synchronized methods

slide-14
SLIDE 14

Java synchronized Methods

public class SynchronizedCounter { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { c--; } public synchronized int value() { return c; } }

slide-15
SLIDE 15

Java synchronized Methods

public class SynchronizedCounter { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { c--; } public synchronized int value() { return c; } }

slide-16
SLIDE 16

Java synchronized Methods

public class SynchronizedCounter { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { c--; } public synchronized int value() { return c; } }

Synchronized methods run in mutual exclusion Only conditional synchronization needs to be programed

slide-17
SLIDE 17

Java wait and NotifyAll

Each object has a wait-set in which threads can block and queue. To block in a wait-set, a thread must invoke wait(); To wake-up one thread from the wait-set, the thread that has the synchronized lock must run: notify(); To wake-up all threads from the wait-set, the thread that has the synchronized lock must run: notifyAll();

slide-18
SLIDE 18

Synchronized Methods

example: shared counter

incr1 incr2 incr3 decr1 decr2 decr3 cont contador inc dec

slide-19
SLIDE 19

Synchronized Methods

example: shared counter

incr1 incr2 incr3 decr1 decr2 decr3 cont contador inc dec

class ContadorSync { int cont; public ContadorSync (int n) { this.cont = n; } public synchronized int getValue() { return this.cont; } public synchronized void inc () { this.cont++; } public synchronized void dec () { this.cont--; } }

slide-20
SLIDE 20

Synchronized Methods

cont contador dec inc

slide-21
SLIDE 21

Synchronized Methods

incr1 incr2 incr3 decr1 decr3 decr2 cont contador dec inc

slide-22
SLIDE 22

Synchronized Methods

cont contador dec inc decr1 synchronize queue incr2 decr3 decr2 incr1 incr3

slide-23
SLIDE 23

Synchronized Methods

cont contador dec inc incr1 incr3 decr2 decr1 incr2 decr3 synchronize queue

slide-24
SLIDE 24

Synchronized Methods

cont contador dec inc incr1 incr3 decr2 decr1 decr3 incr2

slide-25
SLIDE 25

Synchronized Methods

incr1 incr2 incr3 decr1 decr3 decr2 cont contador dec inc

slide-26
SLIDE 26

Synchronized Methods

incr1 incr2 incr3 decr1 decr3 decr2 cont contador dec inc

slide-27
SLIDE 27

Conditional Synchronization

hayDato? Almacen1 get put(x) d

prod1 prod2 prod3 cons1 cons2 cons3

example: producer consumer

slide-28
SLIDE 28

Conditional Synchronization

hayDato? Almacen1 get put(x) d

prod1 prod2 prod3 cons1 cons2 cons3

example: producer consumer = F alm(7) alm(2)

slide-29
SLIDE 29

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer

prod2 cons1 cons2 cons3

prod1 prod3

slide-30
SLIDE 30

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer

prod2 cons1 cons2 cons3

prod1 prod3

slide-31
SLIDE 31

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer cons1

prod2 cons2 cons3

prod1 prod3

slide-32
SLIDE 32

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer

prod2 cons2 cons3

prod3 cons1

prod1

= T = 7

CPRE for prod3 is false! prod2 prod1

slide-33
SLIDE 33

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer

prod2 prod1 prod2 prod1 cons2 cons3

= T = 7 prod3 cons1

wait-set

slide-34
SLIDE 34

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer

prod2 prod1 prod2 prod1 cons2 cons3

prod3

wait-set

= T = 7 cons1

slide-35
SLIDE 35

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer

cons2 cons3 cons1

prod3

wait-set prod2 prod1 prod2 prod1 recheck CPRE

= F

slide-36
SLIDE 36

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer

prod2 prod1 prod2 prod1 cons2 cons3 cons1

prod3 = F

slide-37
SLIDE 37

Conditional Synchronization

hayDato? Almacen1 get put(x) d example: producer consumer prod3

prod2 prod1 prod2 prod1 cons2 cons3 cons1

slide-38
SLIDE 38

Conditional Synchronization

hayDato? Almacen1 get put(x) d

prod1 prod2 prod3 cons1 cons2 cons3

example: producer consumer = T = 3

slide-39
SLIDE 39

Conditional Syncrhonization

synchronized public get(int n) { // CPRE : nDatos > 0 while ( ) { try { wait(); } catch (InterruptedException e) {} }

IDEA: check (at the beginning of a synchronized method) the CPRE.

slide-40
SLIDE 40

Conditional Syncrhonization

synchronized public get(int n) { // CPRE : nDatos > 0 while ( ) { try { wait(); } catch (InterruptedException e) {} }

IDEA: check (at the beginning of a synchronized method) the CPRE. Q: what to put here?

slide-41
SLIDE 41

Conditional Syncrhonization

IDEA: check (at the beginning of a synchronized method) the CPRE.

synchronized public get(int n) { // CPRE : nDatos > 0 while (nDatos <= 0) { try { wait(); } catch (InterruptedException e) {} }

slide-42
SLIDE 42

Conditional Syncrhonization

IDEA: check (at the beginning of a synchronized method) the CPRE.

synchronized public get(int n) { // CPRE : nDatos > 0 while (nDatos <= 0) { try { wait(); } catch (InterruptedException e) {} }

Q: why while and not if?

slide-43
SLIDE 43

Conditional Syncrhonization

IDEA: check (at the beginning of a synchronized method) the CPRE.

synchronized public get(int n) { // CPRE : nDatos > 0 while (nDatos <= 0) { try { wait(); } catch (InterruptedException e) {} }

Q: When and how does blocked thread awake to recheck its CPRE?

slide-44
SLIDE 44

Conditional Syncrhonization

IDEA: check (at the beginning of a synchronized method) the CPRE.

synchronized public get(int n) { // CPRE : nDatos > 0 while (nDatos <= 0) { try { wait(); } catch (InterruptedException e) {} } // // body of the work here // notifyAll(); }

Q: When and how does blocked thread awake to recheck its CPRE?

slide-45
SLIDE 45

Conditional Syncrhonization

IDEA: check (at the beginning of a synchronized method) the CPRE.

synchronized public get(int n) { // CPRE : nDatos > 0 while (nDatos <= 0) { try { wait(); } catch (InterruptedException e) {} } // // body of the work here // notifyAll(); }

wait() releases the synchronize lock. . . . and reacquires the synchronize lock right after being scheduled.

slide-46
SLIDE 46

Systematic Development

Steps:

  • 1. Map CTAD Domain to Java types
  • 2. Implement Initial condition in constructor
  • 3. Make methods synchronize (guarantees mutex)
  • 4. Use pattern for implementing CPRE:

while (~CPRE) { wait(); }

  • 5. Use notifyAll() upon exit.