Object object = someObject;
Java的写操作是原子的(atomic),所以上面的引用赋值操作是一个原子操作。
(一个例外,对long
和double
类型的写操作是非原子的;会分成两步32位的写操作。)
详见JLS:
Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values.
操作是原子的,不代表操作的结果对于其它线程的可见性(visibility)。 可见性由“happens-before”来保证。