简体   繁体   English

Java-多线程CPU缓存

[英]Java - Multithreads CPU cache

I know the threads save the values of the variables in the cpu cache where it is running because in this way the cpu doesnt have wait so much time when it's necessary to get the values inside in the variables. 我知道线程将变量的值保存在运行它的cpu缓存中,因为这样,当需要在变量中获取值时,cpu不会等待那么多时间。

But for example if i have this object 但是例如如果我有这个对象

public class MyObject {
    int a = 2;
}

and now the thread do something like this: 现在线程执行以下操作:

MyObject obj = new MyObject();
obj.a=3;

My question is: 我的问题是:
what will be saved in the cpu cache ? 什么将保存在cpu缓存中? all the MyObject structure or just the reference? 所有MyObject结构还是仅引用? I think all the structure (have more sense) but i prefer to ask because i would like to be sure about that. 我认为所有结构(都有更多意义),但我更喜欢问,因为我想对此有所确定。

I'm a noob about multithread and i'm sure is more complex how a cpu cache works, but at the moment i need just basic information. 我对多线程不是一个菜鸟,而且我确定CPU缓存的工作方式更为复杂,但是目前我只需要基本信息。

In your example, only one thread is acting. 在您的示例中,只有一个线程在起作用。 For this thread, cache is transparent - there is no way to determine if a value is in cache, in main memory, or both. 对于此线程,缓存是透明的-无法确定值是在缓存中,在主内存中还是在这两者中。 First all values are put in the cache but then very soon, in an unknown moment of time they are pushed out. 首先,所有值都放入高速缓存中,然后很快,在未知的时间将它们推出。

"i would like to be sure about that" - why? “我想确定这一点”-为什么? Your program behaviour does not depend on this. 您的程序行为不依赖于此。

These question has two sides: 这些问题有两个方面:

What the CPU is doing: The CPU is designed to keep everything in the cache that is needed very often. CPU在做什么:CPU旨在将经常需要的所有内容保留在缓存中。 If you change a value it will keep changes in the cache until it is needs to write it to the main memory (actually it depends on the CPUs strategy write-back vs write-through). 如果更改一个值,它将一直保留在高速缓存中,直到需要将其写入主内存为止(实际上,这取决于CPU策略的写回还是直写)。 The "need" to write it to main memory is programatically controlled or the CPU descides its needing space for other stuff. 将其写入主存储器的“需要”是通过程序控制的,或者CPU会为其其他内容分配所需的空间。 To answer one part of your question: For the CPU everything is data, the value you set in Java, and the internal object data structures. 要回答问题的一部分:对于CPU,一切都是数据,在Java中设置的值以及内部对象数据结构。 To access your value, you need the object address first, so that is very probably in the cache, too :) 要访问您的值,您首先需要对象地址,因此很有可能也在缓存中:)

The second point, is what Java programmer should expect and not expect: This is very exactly defined in the Java Memory Model. 第二点是Java程序员应该期望和不期望的:这是在Java内存模型中非常精确地定义的。 Just start here: http://en.wikipedia.org/wiki/Java_Memory_Model 从这里开始: http : //en.wikipedia.org/wiki/Java_Memory_Model

So for your lines: 因此,对于您的行:

MyObject obj = new MyObject();
obj.a=3;

There is no guarantee that another thread running after this code, sees the new value. 无法保证在此代码之后运行的另一个线程会看到新值。 And it also may not see your new object reference but null instead. 而且它也可能看不到您的新对象引用,而是显示为null。 You need a synchronized block or a volatile variable. 您需要同步块或易失性变量。

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

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