我浏览JavaEye时间挺久,不过很长时间只潜水,连个账户都是2006年春天才申请。2006年八月初发表了第一个帖子,是回答一个网友关于JVM的问题。JavaEye升级到2.0之后,别的帖子都转过来了,唯独这一篇除外。幸好当初留了个草稿。
原来的问题,是问Inside The Java Virtual Machine一书中一段话是什么意思:
引用
The bytecode streams that represent Java methods are a series of one-byte instructions, called opcodes, each of which may be followed by one or more operands. The operands supply extra data needed by the Java Virtual Machine to execute the opcode instruction. The activity of executing bytecodes, one opcode after another, constitutes a thread of execution inside the Java Virtual Machine. Each thread is awarded its own Java Stack, which is made up of discrete frames. Each method invocation gets its own frame, a section of memory where it stores, among other things, local variables and intermediate results of computation. The part of the frame in which a method stores intermediate results is called the methodís operand stack. An opcode and its (optional) operands may refer to the data stored on the operand stack or in the local variables of the methodís frame. Thus, the virtual machine may use data on the operand stack, in the local variables, or both, in addition to any data stored as operands following an opcode when it executes the opcode.
(这一段我是从第一版的第三章Security,章节Phase One: Internal Checks里摘下来的,在第二版里估计没有什么变动,但章节可能不一样)
这里面有三个关键词:
- Java Stack
- Frame
- Operand stack (注意,这个stack和第一个不是一回事,下面会提到。)
线程是执行一条条机器指令的,文章中The activity of executing bytecodes, one opcode after another, constitutes a thread of execution inside the Java virtual machine 是指这个意思。原网友误解成一个又一个的opcode组成了thread,这不对。一个又一个的opcode不组成thread,而是对一个又一个opcode的执行,组成了thread,
(activity of executing bytecodes)。
Java程序由调用main方法开始,这是第一个方法调用,由main调用另外个方法,这就开始了个新的调用。每一次调用,就生成一个新的frame。
在创建thread的时候,JVM给thread独建一个属于这个thread自己的Java Stack, 然后开始调用main,调用main就建立第一个frame,放在Java Stack里。从main调用另外个方法,生成个新的frame,放在Java Stack最顶上,调用结束,这个最顶上的frame去掉。可以想象java thread在执行时,随着方法调用,Java Stack里面frame增增减减。所以说Java Stack是由一个个frame组成的。
(可以玩玩这个Thread.currentThread().dumpStack(); 就明白了。)
Discrete frames 这里该理解成一个个的frame, 而不是“不连续的frame”。
那么frame里面放的是什么东西呢?翻翻任意一本讲编译器的书,都说里面有这个方法(函数)调用传的参数,返回地址,和局部变量。在jvm里,还有一个叫做operand stack的东东。
我们现在用的计算机,都是寄存器架构的(register machine). JVM 和 .NET Virutal Machine, 都是stack machine. 这两者有什么区别呢?计算一个简单的表达式:1+2,用opcode和operand表达就是add 1, 2 在register machine里面,先把1放在第一个寄存器里面,2放在第二个寄存器里面,运算时从那两个寄存器里取值,结果放在第三个寄存器里面。在jvm里,则是先把1 push到operand stack里,再把2 push 到 operand stack 里,运算时pop 两下把1和2取出来,结果3再push回 operand stack 里面。
运行1+2+3+a, 除了operand stack 之外,还要取局部变量a的值,那段英文的最后一句话指的就是这个。
最后说一句,Java Stack,frame,operand stack,都是内存里面的一块地方。
分享到:
相关推荐
用java实现的栈Stack类,不继承任何集合类,用对象数组实现
JAVA小程序 CharStack.java 包括push() pop() isEmpty() peek() isFull() RepOk()方程
使用java实现stack的源码与测试案例。
一起学习java stack 的信息,如何查看
自己写了一个Java的Stack类,并与原有的Stack比较
很好的一个示例程序,是一次作业,用explicit的方法来描绘一个java stack
栈是先进先出的原则,该类实现了栈的移入移除
每一个frame空间可以称之为StackFrame。 Method Frame 当调用一个新方法的时候,就会在JVM Stack上分配一个frame空间;当方法退出时,相应的frame空间也会JVM Stack上进行清除掉(出栈操作)。在frame空间当中,有...
Stack Frames Stack Frames Stack Frames Stack Frames
C语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 STACKC语言头文件 ...
java数据结构 ArrayList、Stack、Map,为提高效率,未做边界判断(由开发人员保证逻辑上不会出现越界),实现了添加和查询的功能,无修改删除功能
继承的作用以及如何不用继承实现应该具有的功能
还有好多,不一一列举了:获取上面这份 Java 8~12 系列新特性干货文章,请在微信搜索关注微信公众号 "Java技术栈" 在后台回复 "java" 即可获取
隐藏堆栈帧从 通过对任何节点模块隐藏堆栈帧来消除堆栈跟踪噪音。 为什么? 我一直在使用 ,每当发生错误时,就会发生这种情况。 AssertionError: expected 6 to equal 3 at Object.<anonymous> (/Users/dtinth/...
使用Java Stack实现的Web聊天机器人概念证明 该项目是标准的Maven项目,因此您可以将其导入到您选择的IDE中。 您需要安装Java 11+和Node.js 10+。 要从命令行运行,请使用./mvnw spring-boot:run并在浏览器中打开 ...
Whenever a method is invoked a new stack frame is added to the stack and corresponding frame is removed when its execution is completed. Native method stack: holds the state of each native method ...
JAVA Stack是栈。它的特性是:先进后出(FILO, First In Last Out)。
说明可能有些人还不明白:学 Java 到底该学什么?现在市面上都流行什么技术?...目录分类点击快速跳转分类文章:Java8StreamJava9Java10Java11Java12进阶后端技术SpringSpring MVCSpring BootSpring CloudMyBatisD
用java写的stack小程序
主要介绍了java 实现 stack详解的相关资料,需要的朋友可以参考下