简体   繁体   English

Java Thread:start() - 如何创建新线程?

[英]Java Thread: start() - How does it create a new thread?

我想知道java线程启动方法内部能够创建新线程的内容是什么?

It's a native method, which means that it isn't implemented in Java itself, but in C++. 它是一种本机方法,这意味着它不是用Java本身实现的,而是用C ++实现的。

Exactly what it does, outside of JVM book-keeping, depends on the platform the JVM is running on; 在JVM簿记之外,它的确如何做,取决于JVM运行的平台; on Linux, it would run something like pthread_create() , or perhaps clone() directly. 在Linux上,它会运行像pthread_create() ,或者直接运行clone() I don't know off-hand what the Windows alternatives are. 我不知道Windows的替代品是什么。

There are two main reasons why Java hides the underlying mechanism from you: Partly to make it easier for you to write Java programs without having to know the operating system they'll be running on, as an abstraction and a service to you; Java隐藏了你的底层机制有两个主要原因:部分是为了让你更容易编写Java程序,而不必知道他们将运行的操作系统,作为抽象和服务给你; but partly also because being able to call such native functionality directly would break the security model that Java imposes, so it's also to make sure that no Java program can do anything untoward without explicit permissions. 但部分原因还在于能够直接调用这些本机功能会破坏Java所强加的安全模型,因此在没有显式权限的情况下,确保没有Java程序可以做任何事情。

In the end though, I'm not sure exactly why you're asking, so I'm not sure if I'm really answering your question. 最后,我不确定你问的确切原因,所以我不确定我是否真的在回答你的问题。

A specific answer will is impossible because it varies by platform (in the long ago there were Green threads and all was clear - they ran as user level threads ). 具体的答案是不可能的,因为它因平台而异(很久以前就有绿色线程,而且一切都很明确 - 它们作为用户级线程运行 )。 Since that time they've been replaced with Native threads, as an "optimization". 从那时起,他们已被Native线程替换为“优化”。 That is, green threads were removed in favor of the idiomatic threading model of the native operating system. 也就是说,删除了绿色线程以支持本机操作系统的惯用线程模型。 Regardless, native code will eventually invoke your programmed run method. 无论如何,本机代码最终将调用您的编程run方法。

Much like anything else in Java, creating a thread via the start() method involves the JVM . 与Java中的任何其他内容非常相似,通过start()方法创建线程涉及JVM What the Java Virtual Machine does in general is to act as a mediator between your method calls and the underlying operating system. Java虚拟机通常所做的是充当方法调用和底层操作系统之间的中介 This ensures that the Java environment acts in (roughly) the same way regardless of the operating system (=> the famous Java portability). 这确保了Java环境(大致)以相同的方式运行,而不管操作系统如何(=>着名的Java可移植性)。 But what it does behind the curtain (ie in the internals of the JVM) differs and depends on the actual OS it's running on . 但它在窗帘背后的作用(即在JVM的内部) 有所不同,取决于它运行的实际操作系统

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). 例如,创建一个线程调用Unix OS上的pthread_create函数(POSIX线程调用),或Windows系统上的CreateThread函数(来自Win32 API)。 This is called by the Java Virtual Machine so you don't have to. 这是由Java虚拟机调用的,因此您不必这样做。

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. 取决于特定的JVM实现(对于不同的操作系统,请注意),start()方法可能会做其他魔术。

The principle, however, stays the same. 然而,原则保持不变。 The start() method calls something that your particular OS natively supports for creating threads. 在start()方法调用的东西 ,你的特定的操作系统本身支持创建线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM