composing read safe objects
play

Composing read-Safe Objects . Jesper qvist . . . Object State . - PowerPoint PPT Presentation

. Composing read-Safe Objects . Jesper qvist . . . Object State . All objects with mutable state are potentially non thread safe. To guarrantee thread safety a synchronization policy is needed. The policy documents how objects are


  1. . Composing read-Safe Objects . Jesper Öqvist .

  2. . . Object State . All objects with mutable state are potentially non thread safe. To guarrantee thread safety a synchronization policy is needed. The policy documents how objects are protected from invalid state transitions. . . 2

  3. . . Confinement . A good synchronization policy requires some form of confinement: Thread confinement Instance confinement With proper confinement we do not need to analyze the whole program for synchronization problems. . . 3

  4. . . Instance Confinement . Instance Confinement (or just confinement) means keep all state to yourself! . . 4

  5. . . Instance Confinement . However, we often want to share state. This can be tricky: class A { private final B b = new B(); public B getB() { return b; } ... } The thread-safetyness of A depends on the implementation of B. . . 5

  6. . . Instance Confinement . Even trickier: class A { private final B b1 = new B(); private final B b2 = new B(); public B getB() { return b1; } ... } Is b1 independent?! . . 6

  7. . . How to share internal state? Only publish immutable copies of the internal state OR: Only return mutable independent state OR: Be absolutely certain the returned object can be modified at any time . . 7

  8. . void addIfAbsent(T item) { . . } ... } } list.add(item); if (list.contains(item)) { synchronized . public } this .list = list; public MyList(List<T> list) { private final List<T> list; public class MyList implements List<T> { thread safe class. This can be done using encapsulation: Say we want to create a new atomic operation using a Composing Objects . 8

  9. . . Composing Objects . All methods of the List interface must be implemented. This is done simply by delegating to the underlying list object. All modifications of the list must be synchronized with the same lock used for the addIfAbsent operation (intrinsic lock). . . 9

  10. . . Conclusion . Use monitors everywhere. . . 10

  11. . . Exercises . Implement your own ConcurrentHashMap using java.util.HashMap . Document your synchronization policy. Optionally: Implement an iterator that allows iterating the values of your map during concurrent modification. OR : Find a concurrency error in chapter 4 of Goetz. . . 11

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