java “调用栈”和“线程栈”的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31145052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between "call stack" and "thread stack"
提问by Julian A.
Is there a semantic difference between the terms call stack
and thread stack
, in Java multithreading?
在 Java 多线程中,术语call stack
和之间是否存在语义差异thread stack
?
回答by Nathan Hughes
Each thread has its own call stack, "call stack" and "thread stack" are the same thing. Calling it a "thread stack" just emphasizes that the call stack is specific to the thread.
每个线程都有自己的调用栈,“调用栈”和“线程栈”是一回事。将其称为“线程堆栈”只是强调调用堆栈特定于线程。
Bill Venners calls this the Java stack:
When a new thread is launched, the Java virtual machine creates a new Java stack for the thread. As mentioned earlier, a Java stack stores a thread's state in discrete frames. The Java virtual machine only performs two operations directly on Java Stacks: it pushes and pops frames.
The method that is currently being executed by a thread is the thread's current method. The stack frame for the current method is the current frame. The class in which the current method is defined is called the current class, and the current class's constant pool is the current constant pool. As it executes a method, the Java virtual machine keeps track of the current class and current constant pool. When the virtual machine encounters instructions that operate on data stored in the stack frame, it performs those operations on the current frame.
When a thread invokes a Java method, the virtual machine creates and pushes a new frame onto the thread's Java stack. This new frame then becomes the current frame. As the method executes, it uses the frame to store parameters, local variables, intermediate computations, and other data.
当启动一个新线程时,Java 虚拟机会为该线程创建一个新的 Java 堆栈。如前所述,Java 堆栈将线程的状态存储在离散帧中。Java 虚拟机仅直接在 Java Stacks 上执行两个操作:推送和弹出帧。
线程当前正在执行的方法是线程的当前方法。当前方法的堆栈帧是当前帧。定义当前方法的类称为当前类,当前类的常量池为当前常量池。当它执行一个方法时,Java 虚拟机会跟踪当前的类和当前的常量池。当虚拟机遇到对堆栈帧中存储的数据进行操作的指令时,它会在当前帧上执行这些操作。
当线程调用 Java 方法时,虚拟机会创建一个新帧并将其推送到线程的 Java 堆栈上。这个新帧然后成为当前帧。在方法执行时,它使用框架来存储参数、局部变量、中间计算和其他数据。
回答by Olivier Poulin
A call stack
is a stack data structure
that stores information about the active subroutines of a computer program.
Acall stack
是stack data structure
存储有关计算机程序的活动子例程的信息的 a。
What you're calling a thread stack
is what i assume is the private stack of a thread.
你所说的 athread stack
是我假设的线程的私有堆栈。
These two things are essentially the same. They are both stack data structures
.
这两件事本质上是一样的。他们都是stack data structures
。
A thread's stack is used to store the location of function calls in order to allow return statements to return to the correct location
线程的栈用于存储函数调用的位置,以便让return语句返回到正确的位置
Since there usually is only one important call stack, it is what people refer to as the stack.
由于通常只有一个重要的调用栈,所以人们称之为栈。
Hereis information about the stack.
这是有关堆栈的信息。
Hereis information about Stack-based memory allocation.
以下是有关基于堆栈的内存分配的信息。
回答by Raman Shrivastava
Each thread has its own stack, each method call uses a new area of that stack. This means when a method calls itself (recursion), it will have a new set of local variables.
每个线程都有自己的堆栈,每个方法调用都使用该堆栈的一个新区域。这意味着当一个方法调用自身(递归)时,它将拥有一组新的局部变量。
回答by Mani Manu
When FileWriter
throws an IOException
, the runtime system immediately stops executing the try
block; method calls being executed are not completed. The runtime system then starts searching at the top of the method call stack for an appropriate exception handler.
In this example, when the IOException
occurs, the FileWriter
constructor is at the top of the call stack. However, the FileWriter
constructor doesn't have an appropriate exception handler, so the runtime system checks the next method — the writeList
method — in the method call stack. The writeList
method has two exception handlers: one for IOException
and one for IndexOutOfBoundsException
.
当FileWriter
抛出一个 时IOException
,运行时系统立即停止执行该try
块;正在执行的方法调用未完成。运行时系统然后开始在方法调用堆栈的顶部搜索适当的异常处理程序。
在此示例中,当IOException
发生时,FileWriter
构造函数位于调用堆栈的顶部。但是,FileWriter
构造函数没有合适的异常处理程序,因此运行时系统会检查writeList
方法调用堆栈中的下一个方法 —方法。该writeList
方法有两个异常处理程序:一个 forIOException
和一个 for IndexOutOfBoundsException
。