Why does the Java Memory Model allow reads to observe future writes?

There seems to be a similar question (Java Specification: reads see writes that occur later in the execution order), but it focuses on a different example.


The Java Memory Model states in §17.4.8:

Although allowing reads to see writes that come later in the execution order is sometimes undesirable, it is also sometimes necessary.

It then refers to a program and an execution trace from §17.4.5 as an example where this behaviour is necessary.

Here’s the program:

Thread 1 Thread 2
B = 1; A = 2;
r2 = A; r1 = B;

And the corresponding execution trace:

1: r2 = A;  // sees write of A = 2
3: r1 = B;  // sees write of B = 1
2: B = 1;
4: A = 2;

The specification explains:

The trace in Table 17.4.5-A requires some reads to see writes that occur later in the execution order. Since the reads come first in each thread, the very first action in the execution order must be a read. If that read cannot see a write that occurs later, then it cannot see any value other than the initial value for the variable it reads. This is clearly not reflective of all behaviors.

Could someone help clarify what this means? Why can’t we simply declare executions that allow reads to observe future writes as illegal?

For instance, if we disallow such executions, wouldn’t we still be able to produce the result r1 == 1 and r2 == 2 through the following legal execution?

B = 1;
A = 2;
r2 = A;  // sees write of A = 2
r1 = B;  // sees write of B = 1

What would be lost or break if we restricted the model to disallow reads from seeing future writes?

1

There are two sides of things here: the specific section you quoted, and the general motivation for defining things this way.

The line you’ve quoted is confusingly worded. When constructing the formal concept of a well-formed execution and defining how actions can be committed, for these concepts to correctly reflect the program behaviors allowed and disallowed earlier, it is necessary to allow reads to observe the values of later writes. Disallowing this would break consistency with the earlier sections of the spec.

That’s all that section is saying. But there’s the broader issue of why the whole spec wasn’t written to disallow this. And the answer there is simply that the performance impact would be prohibitive. It’d inhibit compiler optimizations and force constant memory barriers. It’d be like making every field in the entire program volatile and locking around every array indexing operation. And it still wouldn’t be enough – making every field in an entire program volatile and locking around every array indexing operation isn’t enough to make a program thread-safe.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Why does the Java Memory Model allow reads to observe future writes?

There seems to be a similar question (Java Specification: reads see writes that occur later in the execution order), but it focuses on a different example.


The Java Memory Model states in §17.4.8:

Although allowing reads to see writes that come later in the execution order is sometimes undesirable, it is also sometimes necessary.

It then refers to a program and an execution trace from §17.4.5 as an example where this behaviour is necessary.

Here’s the program:

Thread 1 Thread 2
B = 1; A = 2;
r2 = A; r1 = B;

And the corresponding execution trace:

1: r2 = A;  // sees write of A = 2
3: r1 = B;  // sees write of B = 1
2: B = 1;
4: A = 2;

The specification explains:

The trace in Table 17.4.5-A requires some reads to see writes that occur later in the execution order. Since the reads come first in each thread, the very first action in the execution order must be a read. If that read cannot see a write that occurs later, then it cannot see any value other than the initial value for the variable it reads. This is clearly not reflective of all behaviors.

Could someone help clarify what this means? Why can’t we simply declare executions that allow reads to observe future writes as illegal?

For instance, if we disallow such executions, wouldn’t we still be able to produce the result r1 == 1 and r2 == 2 through the following legal execution?

B = 1;
A = 2;
r2 = A;  // sees write of A = 2
r1 = B;  // sees write of B = 1

What would be lost or break if we restricted the model to disallow reads from seeing future writes?

1

There are two sides of things here: the specific section you quoted, and the general motivation for defining things this way.

The line you’ve quoted is confusingly worded. When constructing the formal concept of a well-formed execution and defining how actions can be committed, for these concepts to correctly reflect the program behaviors allowed and disallowed earlier, it is necessary to allow reads to observe the values of later writes. Disallowing this would break consistency with the earlier sections of the spec.

That’s all that section is saying. But there’s the broader issue of why the whole spec wasn’t written to disallow this. And the answer there is simply that the performance impact would be prohibitive. It’d inhibit compiler optimizations and force constant memory barriers. It’d be like making every field in the entire program volatile and locking around every array indexing operation. And it still wouldn’t be enough – making every field in an entire program volatile and locking around every array indexing operation isn’t enough to make a program thread-safe.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật