星期四, 十月 27, 2005

JVM Memory Model

JVM Memory Model

Thread可执行的操作:
use(使用)
assign(赋值)
load
store,
lock,
unlock
useassign
Main Memory SubSystem 可执行的操作:
read,
write,
lock,
unlock

这些操作都是Atomic的操作
Main Memory:主内存
Working Memory:工作区内存
关于这些Operation的定义说明:
  • use 动作:使用一个Threaduse 动作把当前Thread里的变量内容传送到Thread的执行引擎.这个动作发生在ThreadJVM指令中使用该变量的值的时候.

  • assign 动作:赋值 一个Threadassign动作 传送Thread引擎的变量值到Threadcopy变量里.这个动作在Thread执行JVM赋值指令的时候.

  • read 动作:读 主内存用稍侯的load动作传送原版变量的值到thread的工作区内存.

  • load 动作:载入 一个Thread把一个值从主内存通过read动作放到thread的工作区的复制的变量

  • store动作: 存储  一个Thread的存储动作是用下面的Write操作传送Thread工作区的复制变量值到主内存。

  • write 动作:写入  主内存的写入动作是通过store动作放一个值从Thread工作区复制变量到在主内存里的原版变量里

  • lock 动作:缩定 Thread和主内存紧密偶合的同步 lock动作造成Thread得到了一个特定锁的声明

  • unlock 动作:解锁 Thread和猪内存的紧密同步,让Thread释放关于特定锁的声明


(image placeholder)


http://java.sun.com/docs/books/vmspec/2nd-edition/html/Threads.doc.html

8.7 Rules for volatile Variables
如果一个变量被申明为volatile,哪个对操作这个变量的Thread有多了额外的约束
T 是个线程.
VWVolatile变量

  • A use operation by T on V is permitted only if the previous operation by T on V was load, and a load operation by T on V is permitted only if the next operation by T on V is use. The use operation is said to be "associated" with the read operation that corresponds to the load.

  • A store operation by T on V is permitted only if the previous operation by T on V was assign, and an assign operation by T on V is permitted only if the next operation by T on V is store. The assign operation is said to be "associated" with the write operation that corresponds to the store.

  • Let action A be a use or assign by thread T on variable V, let action F be the load or store associated with A, and let action P be the read or write of V that corresponds to F. Similarly, let action B be a use or assign by thread T on variable W, let action G be the load or store associated with B, and let action Q be the read or write of W that corresponds to G. If A precedes B, then P must precede Q. (Less formally: operations on the master copies of volatile variables on behalf of a thread are performed by the main memory in exactly the order that the thread requested.)