gpt4 book ai didi

java - Java 创建 Thread 实例时会发生什么

转载 作者:行者123 更新时间:2023-11-30 03:38:12 26 4
gpt4 key购买 nike

我有关于 Java 线程和操作系统线程的问题,我阅读了 Java Threads vs PthreadsJava Threads vs OS Threads但我没有找到解决我困惑的答案。

在调用start()之前我考虑过,没有创建操作系统线程。

但是根据https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html ,

A thread that has not yet started is in NEW state.

我错了吗,还是java doc中定义的状态不仅仅用于标记Thread实例的状态?

我的问题是:

  • 当我们创建 Thread 实例但在调用 start() 之前到底发生了什么
  • Java 中何时真正创建操作系统线程

最佳答案

Thread thread = new Thread(){
@Override
public void run() {
// code
}
};
// at this point the thread is in NEW state, all you have a simple java object,
// no actual thread is created

thread.start();
// when start() is invoked, at some unspecified point in the near future
// the thread will go into RUNNABLE state, this means an actual thread will be created.
// That can happen before start() returns.

关于java - Java 创建 Thread 实例时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27330436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com