Thread.currentThread()如何工作? [英] How does Thread.currentThread() work?
问题描述
Thread.currentThread()
是一个 static 方法,它提供对当前正在执行的Thread的引用(基本上是对'this'线程的引用)。
Thread.currentThread()
is a static method which provides reference to currently executing Thread (basically a reference to 'this' thread).
在Java中无法访问静态方法中的非静态成员(尤其是 this
),所以 currentThread()
是一种本机方法。
Accessing non-static members (especially this
) inside a static method is not possible in Java, so currentThread()
is a native method.
currentThread()
方法如何在幕后工作?
How does currentThread()
method work behind the scenes?
推荐答案
(basically a reference to 'this' thread)
错了。此处涉及此
引用。
您正在混合线程本机资源,意味着执行线程;和 Thread
,这是一个Java类。线程代码不会在 Thread
实例中运行,该实例只是Java的线程控制的句柄。很像文件
实例不是文件。
You are mixing up a thread as a native resource, meaning the thread of execution; and Thread
, which is a Java class. Thread code does not run "within" the Thread
instance, that instance is just your handle into Java's thread control. Much like a File
instance is not a file.
所以, Thread.currentThread()
是一种方法,可以检索负责执行线程的线程
的实例。该方法被调用。除非您正在探索特定JVM实现的细节,否则Java究竟是如何实现这一细节的,除非您正在探索特定JVM实现的细节。
So, Thread.currentThread()
is a way for you to retrieve the instance of Thread
in charge of the thread-of-execution inside which the method is called. How exactly Java does this is an implementation detail which should not be your concern unless you are exploring the details of a particular JVM implementation.
这篇关于Thread.currentThread()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!