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

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

问题描述

当我说:

Thread t1 = new Thread();

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

does it create it on a heap or a stack?

推荐答案

Thread t1 = new Thread();

tl;dr 这会在堆中分配对象,即 t1.

tl;dr 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.

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

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