简体   繁体   English

Java中多线程程序中的对象可见性

[英]Object Visibility in a Multi-threaded Program in Java

I was going through Java: Concurrency in Practice , and in the third chapter, the authors write: 我正在阅读Java:Concurrency in Practice ,在第三章中,作者写道:

"...There is no guarantee that operations in one thread will be performed in the order given by the program, as long as the reordering is not detectable from within that thread—even if the reordering is apparent to other threads...." “...无法保证一个线程中的操作将按程序给出的顺序执行,只要在该线程内无法检测到重新排序 - 即使重新排序对其他线程是明显的...... “

I understand that the actual order of execution of statements in one thread might not be what was written in the program (depends upon compiler optimisations and stuff). 我理解在一个线程中执行语句的实际顺序可能不是程序中编写的(依赖于编译器优化和东西)。 But for some reason, I cannot decipher the arcane statement written by the authors. 但出于某种原因,我无法破译作者所写的神秘陈述。

"...There is no guarantee that operations in one thread will be performed in the order given by the program..." -Alright. “......无法保证一个线程中的操作将按程序给出的顺序执行......” -好的。 The actual order of execution of statements might differ. 语句的实际执行顺序可能不同。

"...as long as the reordering is not detectable from within that thread—even if the reordering is apparent to other threads..." -What do they mean by "....detectable from within the thread...." ? “...只要在该线程内无法检测到重新排序 - 即使重新排序对其他线程是明显的......” - 它们的意思是什么“......可以从线程中检测到...... “

It simply means that no re-ordering that is performed can invalidate any assertions you could make about the state visible to that thread by looking at the code. 它只是意味着没有执行的重新排序可以通过查看代码使您可以对该线程可见的状态的任何断言无效。

As a simple example: 举个简单的例子:

/* 1 */ x = 0;
/* 2 */ boolean c = x == 0;
/* 3 */ x = 1;

It's not permitted to move 3 before 2 because that would be detected by the value of c . 不允许在2之前移动3,因为这将通过c的值来检测。

In other words, a re-ordering that might appear to create a bug when viewed by other threads is allowed, but a re-ordering that creates a bug within the thread is not allowed. 换句话说,可能出现其他线程观看时产生的错误重新排序允许的,但创建线程中的错误重新排序是不允许的。

I'm having to guess a little bit here, but this is my take on it: you write a program that creates different threads for processing; 我不得不在这里猜一点,但这是我的看法:你编写一个程序,创建不同的线程进行处理; you create thread A, start it, create thread B, start it, etc. 你创建线程A,启动它,创建线程B,启动它等等。

There is no guarantee that thread A will run before thread B. It is likely, probably even most common, but part of concurrent programming is eliminating this kind of assumption from your thinking. 无法保证线程A将在线程B之前运行。很可能,甚至可能是最常见的,但并发编程的一部分正在消除您的想法中的这种假设。 If ANY PART of the logic of B depends on ANY PART of the logic of A executing first, then you need to do programming of your own to guarantee that -- the fact that you have created A before B, nor the fact that you've done anything else outside thread coordination logic, will guarantee that any part of A will execute before any part of B. 如果B的逻辑的任何部分依赖于A首先执行的逻辑的任何部分,那么你需要对自己进行编程以保证 - 你在B之前创建了A的事实,也没有你的事实'在线程协调逻辑之外做了其他任何事情,将保证A的任何部分将在B的任何部分之前执行。

As for "detectable within the thread", I assume they mean some kind of semaphore, variable, lock, etc. that allows one thread to know something about the completion of some other thread. 至于“在线程内可检测”,我认为它们意味着某种信号量,变量,锁等,它允许一个线程知道一些其他线程的完成。

I think it's confusing for them to use "the order given by the program" -- that's really ambiguous, especially to people just starting to try to think about concurrent programming. 我认为让他们使用“程序给出的顺序”令人困惑 - 这真的很模糊,特别是对于刚刚开始尝试考虑并发编程的人。 It does not mean that statements can be reordered regardless of where they are. 这并不意味着语句可以重新排序,无论它们在何处。

Does that help? 这有帮助吗?

我会说,它们意味着“检测”排序的线程将遵循执行顺序,但并非所有线程都将以这种方式运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM