l17 model based development
play

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


  1. 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

  2. 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

  3. HW7: Multibuffer con m´ etodos synchronized

  4. HW7: Multibuffer con m´ etodos synchronized

  5. HW7: Multibuffer con m´ etodos synchronized

  6. Model-Based Development Requirements

  7. Model-Based Development Model Formal, precise Requirements Concurrent Interaction

  8. Model-Based Development fix no Simulation Testing yes 2 activities Model Formal, precise Requirements Concurrent Interaction

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

  10. Model-Based Development fix no Simulation Testing yes Model Code Generation recurso.java

  11. Model-Based Development fix no Simulation Testing yes Model Code Generation recurso.java

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

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

  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; } }

  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; } }

  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

  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();

  18. Synchronized Methods example: shared counter incr 1 decr 1 contador incr 2 decr 2 inc dec cont incr 3 decr 3

  19. 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--; } }

  20. Synchronized Methods contador inc dec cont

  21. Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3

  22. Synchronized Methods incr 1 contador inc dec decr 2 cont incr 3 decr 1 incr 2 decr 3 synchronize queue

  23. Synchronized Methods incr 1 decr 1 contador inc dec decr 2 cont incr 3 incr 2 decr 3 synchronize queue

  24. Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3

  25. Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3

  26. Synchronized Methods incr 1 decr 1 contador incr 2 inc dec decr 2 cont incr 3 decr 3

  27. Conditional Synchronization example: producer consumer prod 1 cons 1 Almacen1 prod 2 get cons 2 put(x) hayDato? d prod 3 cons 3

  28. 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

  29. Conditional Synchronization example: producer consumer cons 1 Almacen1 prod 2 get cons 2 put(x) hayDato? d cons 3 prod 1 prod 3

  30. Conditional Synchronization example: producer consumer cons 1 Almacen1 prod 2 get cons 2 put(x) hayDato? d cons 3 prod 1 prod 3

  31. Conditional Synchronization example: producer consumer Almacen1 prod 2 get cons 2 put(x) hayDato? d cons 3 prod 1 cons 1 prod 3

  32. 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!

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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) {} }

  40. 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?

  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) {} }

  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 ?

  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?

  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?

  45. 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(); }

  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.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend