`
jimode2013
  • 浏览: 39396 次
社区版块
存档分类
最新评论

Chapter 4. Composing Objects

 
阅读更多

 

Chapter 4. Composing Objects

So far, we've covered the lowlevel basics of thread safety and synchronization. But we don't want to have to analyze each memory access to ensure that our program is threadsafe; we want to be able to take threadsafe components and safely compose them into larger components or programs. This chapter covers patterns for structuring classes that can make it easier to make them threadsafe and to maintain them without accidentally undermining their safety guarantees.

 

4.1. Designing a Thread­safe Class

While it is possible to write a threadsafe program that stores all its state in public static fields, it is a lot harder to verify its thread safety or to modify it so that it remains threadsafe than one that uses encapsulation appropriately. Encapsulation makes it possible to determine that a class is threadsafe without having to examine the entire program.

 

The design process for a threadsafe class should include these three basic elements:

  • Identify the variables that form the object's state;

  • Identify the invariants that constrain the state variables;

  • Establish a policy for managing concurrent access to the object's state.

An object's state starts with its fields. If they are all of primitive type, the fields comprise the entire state. Counter in Listing 4.1 has only one field, so the value field comprises its entire state. The state of an object with n primitive fields is just the ntuple of its field values; the state of a 2D Point is its (x, y) value. If the object has fields that are references to other objects, its state will encompass fields from the referenced objects as well. For example, the state of a LinkedList includes the state of all the link node objects belonging to the list.

 

The synchronization policy defines how an object coordinates access to its state without violating its invariants or postconditions. It specifies what combination of immutability, thread confinement, and locking is used to maintain thread safety, and which variables are guarded by which locks. To ensure that the class can be analyzed and maintained, document the synchronization policy.

 

分享到:
评论

相关推荐

    Mastering.Clojure.1785

    Chapter 4. Metaprogramming with Macros Chapter 5. Composing Transducers Chapter 6. Exploring Category Theory Chapter 7. Programming with Logic Chapter 8. Leveraging Asynchronous Tasks Chapter 9. ...

    Android.Design.Patterns.and.Best.Practices.epub

    Chapter 4. Layout Patterns Chapter 5. Structural Patterns Chapter 6. Activating Patterns Chapter 7. Combining Patterns Chapter 8. Composing Patterns Chapter 9. Observing Patterns Chapter 10. ...

    C# Programming Cookbook [EPUB+MOBI+PDF]

    Chapter 4. Composing Event-Based Programs Using Reactive Extensions Chapter 5. Create Microservices on Azure Service Fabric Chapter 6. Making Apps Responsive with Asynchronous Programming Chapter 7. ...

    Mastering.Angular.2.Components.1785884646.epub

    Chapter 4. No Comments, Please! Chapter 5. Component-Based Routing Chapter 6. Keeping Up with Activities Chapter 7. Components for User Experience Chapter 8. Time Will Tell Chapter 9. Spaceship ...

    More.Math.Into.LaTeX.3319237950

    For over two decades, this comprehensive manual has been the standard introduction and complete reference for writing ...Chapter 4. pdf Documents Chapter 5. Customization Chapter 6. Long Documents

    Clojure.High.Performance.JVM.Programming.epub

    Chapter 4. Collections and Functional Programming Chapter 5. Multimethods and Protocols Chapter 6. Concurrency Chapter 7. Macros in Clojure Chapter 8. Application Performance Chapter 9. Reactive ...

    Composing.Software.2019.2.pdf

    #### Composing Software: An Introduction 本书《Composing Software》是一本深入探讨函数式编程和对象组合在 JavaScript 中应用的书籍。作者 Eric Elliott 通过本书旨在帮助读者理解并掌握如何构建可扩展、可维护...

    Python Cookbook, 2nd Edition

    Chapter 4. Python Shortcuts Introduction Recipe 4.1. Copying an Object Recipe 4.2. Constructing Lists with List Comprehensions Recipe 4.3. Returning an Element of a List If It Exists Recipe...

    Composing.Software.2019.2.mobi

    Composing.Software.2019.2

    C++ Standard Library: A Tutorial and Reference

    STL Function Objects <br/>8.1 The Concept of Function Objects <br/>8.2 Predefined Function Objects <br/>8.3 Supplementary Composing Function Objects <br/>Chapter 9. STL Algorithms...

    LiteBlog(MVC)博客源码2012711

    1.Composing of posts using FckEditor (Rich editor) 2.Ability to add code in the blog from the editor 3.Syntax Highlighter to highlight the code 4.Completely XML based 5.Scheduled publishing 6....

    Composing Software.zip

    // 输出4 ``` 总结,函数式编程在JavaScript中的应用极大地提高了代码的简洁性和可维护性。通过理解并掌握柯里化、Lens以及Monad等概念,开发者能够编写出更高效、更可靠的代码,同时也为代码的测试和重构提供了...

    spring-framework-reference4.1.4

    Composing XML-based configuration metadata .......................................... 25 Using the container .......................................................................................... ...

    spring-framework-reference-4.1.2

    Composing XML-based configuration metadata .......................................... 25 Using the container .......................................................................................... ...

    Painless Docker

    Chapter IX - Composing Services Using Compose Chapter X - Docker Logging Chapter XI - Docker Debugging And Troubleshooting Chapter XIII - Orchestration - Kubernetes Chapter XIV - Orchestration - ...

Global site tag (gtag.js) - Google Analytics