Java 线程:start() - 它如何创建新线程? [英] Java Thread: start() - How does it create a new thread?

查看:56
本文介绍了Java 线程:start() - 它如何创建新线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 java 线程启动方法中包含什么,它能够创建一个新线程?

I was wondering what goes inside a java thread start method that it's able to create a new thread?

推荐答案

很像 Java 中的其他任何事情,通过 start() 方法创建线程涉及 JVM.Java 虚拟机的作用通常是充当方法调用和底层操作系统之间的中介.这确保了 Java 环境以(大致)相同的方式运行,而不管操作系统如何(=> 著名的 Java 可移植性).但是它在幕后所做的事情(即在 JVM 的内部)不同并且取决于它运行的实际操作系统.

Much like anything else in Java, creating a thread via the start() method involves the JVM. What the Java Virtual Machine does in general is to act as a mediator between your method calls and the underlying operating system. This ensures that the Java environment acts in (roughly) the same way regardless of the operating system (=> the famous Java portability). But what it does behind the curtain (i.e. in the internals of the JVM) differs and depends on the actual OS it's running on.

例如,创建线程在 Unix 操作系统上调用 pthread_create 函数(POSIX 线程调用),或在 Windows 系统上调用 CreateThread 函数(这是从Win32 API).这由Java 虚拟机 调用,因此您不必这样做.

For example, creating a thread calls the pthread_create function (the POSIX thread call) on a Unix OS, or the CreateThread function on a Windows system (this is from the Win32 API). This is called by the Java Virtual Machine so you don't have to.

这些函数只是示例.根据特定的 JVM 实现(请注意,对于不同的操作系统), start() 方法可能会在背后发挥其他作用.

These functions are just examples. Depending on the particular JVM implementation (for differnent OS-es, mind you), the start() method might do other magic behind.

然而,原则保持不变.start() 方法调用某些内容,您的特定操作系统本机支持创建线程.

The principle, however, stays the same. The start() method calls something that your particular OS natively supports for creating threads.

这篇关于Java 线程:start() - 它如何创建新线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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