Thread Object在哪里创建?堆栈还是堆? [英] Where is Thread Object created? Stack or Heap?

查看:284
本文介绍了Thread Object在哪里创建?堆栈还是堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我说的话:

Thread t1 = new Thread();

它是在堆还是堆栈上创建?

does it create it on a heap or a stack?

推荐答案

Thread t1 = new Thread();

这样会在堆中分配对象ie t1。

This allocates object i.e. t1 in heap.

随着每个新线程的存在,它获得自己的pc寄存器(程序计数器)和Java堆栈。如果线程正在执行Java方法(非本地方法),则pc寄存器的值指示要执行的下一个指令。线程的Java堆栈存储线程的Java(非本地)方法调用的状态。 Java方法调用的状态包括其局部变量,它被调用的参数,其返回值(如果有),和中间计算。本地方法调用的状态以依赖于实现的方式存储在本地方法堆栈中,以及可能在寄存器或其他实现相关的存储器区域中。

As each new thread comes into existence, it gets its own pc register (program counter) and Java stack. If the thread is executing a Java method (not a native method), the value of the pc register indicates the next instruction to execute. A thread's Java stack stores the state of Java (not native) method invocations for the thread. The state of a Java method invocation includes its local variables, the parameters with which it was invoked, its return value (if any), and intermediate calculations. The state of native method invocations is stored in an implementation-dependent way in native method stacks, as well as possibly in registers or other implementation-dependent memory areas.

Java堆栈由堆栈帧(或帧)组成。堆栈帧包含一个Java方法调用的状态。当线程调用方法时,Java虚拟机将一个新帧推送到该线程的Java栈上。当方法完成时,虚拟机弹出并丢弃该方法的框架。

The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java virtual machine pushes a new frame onto that thread's Java stack. When the method completes, the virtual machine pops and discards the frame for that method.

Java虚拟机没有寄存器来保存中间数据值。指令集使用Java堆栈存储中间数据值。

The Java virtual machine has no registers to hold intermediate data values. The instruction set uses the Java stack for storage of intermediate data values.

该图显示了三个线程正在执行的虚拟机实例的快照。在快照的瞬间,线程1和线程2正在执行Java方法。线程三正在执行本地方法。它还显示了Java虚拟机为每个线程创建的内存区域,这些区域对于拥有线程是私有的。没有线程可以访问另一个线程的pc寄存器或Java堆栈。

Figure shows a snapshot of a virtual machine instance in which three threads are executing. At the instant of the snapshot, threads one and two are executing Java methods. Thread three is executing a native method. It also shows of the memory areas the Java virtual machine creates for each thread, these areas are private to the owning thread. No thread can access the pc register or Java stack of another thread.

这篇关于Thread Object在哪里创建?堆栈还是堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆