Chapter 4. Composing Objects
So far, we've covered the low‐level basics of thread safety and synchronization. But we don't want to have to analyze each memory access to ensure that our program is thread‐safe; we want to be able to take thread‐safe 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 thread‐safe and to maintain them without accidentally undermining their safety guarantees.
4.1. Designing a Threadsafe Class
While it is possible to write a thread‐safe 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 thread‐safe than one that uses encapsulation appropriately. Encapsulation makes it possible to determine that a class is thread‐safe without having to examine the entire program.
The design process for a thread‐safe 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 n‐tuple 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 post‐ conditions. 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.
相关推荐
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...
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...
Chapter 4:Building Tests 构建测试 The Value of Self-testing Code 自测试代码的重要性 The JUnit Testing Framework Junit测试框架 Adding More Tests 添加更多测试 Chapter 5:Toward a Catalog ...
Chapter 4:Building Tests 构建测试 The Value of Self-testing Code 自测试代码的重要性 The JUnit Testing Framework Junit测试框架 Adding More Tests 添加更多测试 Chapter 5:Toward a Catalog ...
Chapter 4:Building Tests 构建测试 The Value of Self-testing Code 自测试代码的重要性 The JUnit Testing Framework Junit测试框架 Adding More Tests 添加更多测试 Chapter 5:Toward a Catalog ...
Chapter 4:Building Tests 构建测试 The Value of Self-testing Code 自测试代码的重要性 The JUnit Testing Framework Junit测试框架 Adding More Tests 添加更多测试 Chapter 5:Toward a ...
Chapter 4:Building Tests 构建测试 The Value of Self-testing Code 自测试代码的重要性 The JUnit Testing Framework Junit测试框架 Adding More Tests 添加更多测试 Chapter 5:Toward a ...
- **Chapter2: Composing a document using iText's Basic Building Blocks** - **2.1: A REAL-WORLD DATABASE**:介绍一个真实的数据库示例,展示如何使用iText处理数据并生成PDF报告。 - **2.2: ADDING CHUNK, ...