问题描述
如果在多处理器或多处理器计算机上运行,而jvm有可能绝对同时运行多个线程(不只是同时出现),那么api方法java.lang.Thread.currentThread()
返回什么?情况下,它是否只是随机返回当前线程之一?
If running on a multicore or multi processor machine where the jvm has the potential to run more than one thread absolutely simultaneously (not just apparent simultaneously), what does the api method java.lang.Thread.currentThread()
return?....in the above scenario, does it just return one of the current threads at random?
推荐答案
它返回您当前正在其中运行的线程.如果您有两个内核和两个线程A
和B
完全同时运行,并同时调用此方法,它将适当地返回A
和B
.
It returns the thread you are currently running inside. If you have two cores and two threads A
and B
running completely concurrently, calling this method at the same time, it will return A
and B
appropriately.
您的理解是正确的-此方法返回的线程始终处于运行状态-因为必须从某个线程调用该线程,并且为了返回该线程必须正在运行.不要从"所有当前正在运行的,非暂停的,非阻塞的线程"的角度来考虑这种方法.而是它的含义是:"给我一个运行我的线程的引用".
Your understanding is kind-of correct - the thread returned by this method is always running - because it must be called from some thread and in order to return, it must be running. Don't think about this method in terms of: "all currently running, non-paused, non-blocked threads". Instead its meaning is: "give me a reference to the thread that runs me".
这篇关于多核/多处理器系统上Thread.currentThread()的语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!