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´ ecnica de Madrid Wed, 15-April-2015 Este texto se distribuye bajo los t´ erminos de la Creative Commons License
HW7 Homework: HW1: Creaci´ on de threads en Java HW2: Provocar una condici´ on de carrera HW3: Garanatizar la exclusi´ on mutua con espera activa HW4: Garantizar la exlusi´ on mutua con sem´ aforos HW5: Almac´ en de un dato con sem´ aforos HW6: Almac´ en de varios datos con sem´ aforos HW7: Especificaci´ on 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
HW7: Multibuffer con m´ etodos synchronized
HW7: Multibuffer con m´ etodos synchronized
HW7: Multibuffer con m´ etodos synchronized
Model-Based Development Requirements
Model-Based Development Model Formal, precise Requirements Concurrent Interaction
Model-Based Development fix no Simulation Testing yes 2 activities Model Formal, precise Requirements Concurrent Interaction
Model-Based Development fix no Simulation Testing yes 2 activities Model Code Formal, precise Requirements Generation Concurrent Interaction recurso.java
Model-Based Development fix no Simulation Testing yes Model Code Generation recurso.java
Model-Based Development fix no Simulation Testing yes Model Code Generation recurso.java
Model-Based Development fix no Simulation Testing yes synchronized Model methods Code Generation monitors systematic automatic recurso.java message passing
Model-Based Development fix no Simulation Testing yes synchronized Model methods Code Generation monitors systematic automatic recurso.java message passing Today: synchronized methods
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; } }
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; } }
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
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();
Synchronized Methods example: shared counter incr 1 decr 1 contador incr 2 decr 2 inc dec cont incr 3 decr 3
Synchronized Methods example: shared counter incr 1 decr 1 contador incr 2 decr 2 inc dec cont class ContadorSync { incr 3 decr 3 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--; } }
Synchronized Methods contador inc dec cont
Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3
Synchronized Methods incr 1 contador inc dec decr 2 cont incr 3 decr 1 incr 2 decr 3 synchronize queue
Synchronized Methods incr 1 decr 1 contador inc dec decr 2 cont incr 3 incr 2 decr 3 synchronize queue
Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3
Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3
Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3
Conditional Synchronization example: producer consumer prod 1 cons 1 Almacen1 prod 2 get cons 2 put(x) hayDato? d prod 3 cons 3
Conditional Synchronization example: producer consumer prod 1 cons 1 alm(7) Almacen1 prod 2 get cons 2 put(x) hayDato? = F alm(2) d prod 3 cons 3
Conditional Synchronization example: producer consumer cons 1 Almacen1 prod 2 get cons 2 put(x) hayDato? d cons 3 prod 1 prod 3
Conditional Synchronization example: producer consumer cons 1 Almacen1 prod 2 get cons 2 put(x) hayDato? d cons 3 prod 1 prod 3
Conditional Synchronization example: producer consumer Almacen1 prod 2 get cons 2 put(x) hayDato? d cons 3 prod 1 cons 1 prod 3
Conditional Synchronization example: producer consumer prod 1 prod 1 Almacen1 prod 2 prod 2 get cons 2 put(x) hayDato? = T d = 7 cons 3 cons 1 prod 3 CPRE for prod 3 is false!
Conditional Synchronization example: producer consumer prod 1 prod 1 Almacen1 prod 2 prod 2 get cons 2 put(x) hayDato? = T d = 7 cons 3 cons 1 prod 3 wait-set
Conditional Synchronization example: producer consumer prod 1 prod 1 Almacen1 prod 2 prod 2 get cons 2 put(x) hayDato? = T d = 7 cons 3 cons 1 prod 3 wait-set
Conditional Synchronization example: producer consumer prod 1 prod 1 cons 1 Almacen1 prod 2 prod 2 get cons 2 put(x) hayDato? = F d cons 3 prod 3 wait-set recheck CPRE
Conditional Synchronization example: producer consumer prod 1 prod 1 cons 1 Almacen1 prod 2 prod 2 get cons 2 put(x) hayDato? = F d cons 3 prod 3
Conditional Synchronization example: producer consumer prod 1 prod 1 cons 1 Almacen1 prod 2 prod 2 get cons 2 put(x) hayDato? d cons 3 prod 3
Conditional Synchronization example: producer consumer prod 1 cons 1 Almacen1 prod 2 get cons 2 put(x) hayDato? = T d = 3 prod 3 cons 3
Conditional Syncrhonization IDEA: check (at the beginning of a synchronized method) the CPRE. synchronized public get(int n) { // CPRE : nDatos > 0 while ( ) { try { wait(); } catch (InterruptedException e) {} }
Conditional Syncrhonization IDEA: check (at the beginning of a synchronized method) the CPRE. synchronized public get(int n) { // CPRE : nDatos > 0 while ( ) { try { wait(); } catch (InterruptedException e) {} } Q: what to put here?
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) {} }
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 ?
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?
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?
Conditional Syncrhonization IDEA: check (at the beginning of a synchronized method) the CPRE. synchronized public get(int n) { // CPRE : nDatos > 0 wait() releases the synchronize while (nDatos <= 0) { lock. try { wait(); } catch (InterruptedException e) {} } . . . and reacquires the synchronize lock right after being scheduled. // // body of the work here // notifyAll(); }
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.
Recommend
More recommend