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
s t r u c t i
! D
p r i n t
SLIDE 2 HW7
Homework: HW1: Creaci´
HW2: Provocar una condici´
HW3: Garanatizar la exclusi´
- n mutua con espera activa
HW4: Garantizar la exlusi´
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
HW7: Multibuffer con m´ etodos synchronized
SLIDE 4
HW7: Multibuffer con m´ etodos synchronized
SLIDE 5
HW7: Multibuffer con m´ etodos synchronized
SLIDE 6
Model-Based Development
Requirements
SLIDE 7
Model-Based Development
Requirements Concurrent Interaction Model Formal, precise
SLIDE 8
Model-Based Development
Requirements Concurrent Interaction Model Formal, precise 2 activities Simulation Testing yes no fix
SLIDE 9
Model-Based Development
Requirements Concurrent Interaction Model Formal, precise 2 activities Simulation Testing yes no fix Code Generation recurso.java
SLIDE 10
Model-Based Development
Model Simulation Testing yes no fix Code Generation recurso.java
SLIDE 11
Model-Based Development
Model Simulation Testing yes no fix Code Generation recurso.java
SLIDE 12
Model-Based Development
Model Simulation Testing yes no fix Code Generation recurso.java automatic systematic synchronized methods monitors message passing
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
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
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
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
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
Synchronized Methods
example: shared counter
incr1 incr2 incr3 decr1 decr2 decr3 cont contador inc dec
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
Synchronized Methods
cont contador dec inc
SLIDE 21
Synchronized Methods
incr1 incr2 incr3 decr1 decr3 decr2 cont contador dec inc
SLIDE 22
Synchronized Methods
cont contador dec inc decr1 synchronize queue incr2 decr3 decr2 incr1 incr3
SLIDE 23
Synchronized Methods
cont contador dec inc incr1 incr3 decr2 decr1 incr2 decr3 synchronize queue
SLIDE 24
Synchronized Methods
cont contador dec inc incr1 incr3 decr2 decr1 decr3 incr2
SLIDE 25
Synchronized Methods
incr1 incr2 incr3 decr1 decr3 decr2 cont contador dec inc
SLIDE 26
Synchronized Methods
incr1 incr2 incr3 decr1 decr3 decr2 cont contador dec inc
SLIDE 27
Conditional Synchronization
hayDato? Almacen1 get put(x) d
prod1 prod2 prod3 cons1 cons2 cons3
example: producer consumer
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
Conditional Synchronization
hayDato? Almacen1 get put(x) d example: producer consumer
prod2 cons1 cons2 cons3
prod1 prod3
SLIDE 30
Conditional Synchronization
hayDato? Almacen1 get put(x) d example: producer consumer
prod2 cons1 cons2 cons3
prod1 prod3
SLIDE 31
Conditional Synchronization
hayDato? Almacen1 get put(x) d example: producer consumer cons1
prod2 cons2 cons3
prod1 prod3
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
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
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
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
Conditional Synchronization
hayDato? Almacen1 get put(x) d example: producer consumer
prod2 prod1 prod2 prod1 cons2 cons3 cons1
prod3 = F
SLIDE 37
Conditional Synchronization
hayDato? Almacen1 get put(x) d example: producer consumer prod3
prod2 prod1 prod2 prod1 cons2 cons3 cons1
SLIDE 38
Conditional Synchronization
hayDato? Almacen1 get put(x) d
prod1 prod2 prod3 cons1 cons2 cons3
example: producer consumer = T = 3
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
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
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
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
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
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
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 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.