Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完善"如何保证变量的可见性"问题答案 #1850

Closed
wants to merge 2 commits into from

Conversation

shark-ctrl
Copy link
Contributor

参见Issue 关于Java 并发常见面试题总结(中)“如何保证变量的可见性?”问题的答案详解 #1849

image

@jizhuozhi
Copy link
Contributor

关于TLAB的描述并不严谨,实际上不论是否有TLAB,JVM都可以通过锁或CAS或其他同步原语来完成原子性内存分配。分配TLAB本身就是从堆中分配连续内存空间的过程,通过CAS移动碰撞指针(Bump Pointer)来完成的分配。只不过使用TLAB可以避免在分配对象过程中使用同步原语,尽最大程度减少并发冲突。

引用2004年G1论文Garbage-First Garbage Collection中的描述

2.1 Heap Layout/Heap Regions/Allocation
The Garbage-First heap is divided into equal-sized heap regions, each a contiguous range of virtual memory. Alloca- tion in a heap region consists of incrementing a boundary, top, between allocated and unallocated space. One region is the current allocation region from which storage is be- ing allocated. Since we are mainly concerned with multi- processors, mutator threads allocate only thread-local allo- cation buffers, or TLABs, directly in this heap region, using a compare-and-swap, or CAS, operation. They then allocate objects privately within those buffers, to minimize allocation contention. When the current allocation region is filled, a new allocation region is chosen. Empty regions are orga- nized into a linked list to make region allocation a constant time operation.
Larger objects may be allocated directly in the current allocation region, outside of TLABs. Objects whose size exceeds 3/4 of the heap region size, however, are termed humongous. Humongous objects are allocated in dedicated (contiguous sequences of) heap regions; these regions con- tain only the humongous object.1

@jizhuozhi
Copy link
Contributor

同时volatile本身并没有关注体系结构上是如何实现的缓存一致性协议,它仅仅关心在读之前使用读写屏障,在写之后使用写读屏障而已。

具体的源码可以看这一部分
https://github.com/openjdk/jdk/blob/master/src/hotspot/share/interpreter/zero/bytecodeInterpreter.cpp#L1768-L1805
https://github.com/openjdk/jdk/blob/master/src/hotspot/share/interpreter/zero/bytecodeInterpreter.cpp#L1899-L1934

实际上是Java内存模型开发时间远远早于C++内存顺序定义时间,不然Java可以不应定义访问顺序,直接使用C++内存顺序的实现的(https://en.cppreference.com/w/cpp/atomic/memory_order)

  1. Read-write coherence: if a value computation A of some atomic M (a read) happens-before an operation B on M (a write), then the value of A comes from a side-effect (a write) X that appears earlier than B in the modification order of M
  2. Write-read coherence: if a side effect (a write) X on an atomic object M happens-before a value computation (a read) B of M, then the evaluation B shall take its value from X or from a side effect Y that follows X in the modification order of M

@Snailclimb Snailclimb closed this May 5, 2023
@shark-ctrl shark-ctrl deleted the shark-branch branch June 21, 2023 04:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants