Semantics of Thread.currentThread() on multicore/multi processor systems?

血红的双手。 提交于 2019-12-01 06:09:41

问题


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?


回答1:


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".




回答2:


It makes perfect sense. It returns a reference to the thread that is executing the calling code.

So, suppose that you have two threads, threadA and threadB. If code running in threadA calls currentThread(), then threadA will be returned. Likewise, If code running in threadB calls currentThread(), then threadB is returned.

The documentation is pretty weak in my view. It states:

Returns a reference to the currently executing thread object.

The use of "currently" here is very poor. Currently is often interpreted to mean "at this instant in time". Which is what confused you.

Rather ironically, the MSDN (!) documentation makes a better stab at it:

Gets the Thread object that is calling the current code block.

But they still fall into the trap of using "current". Oh the pitfalls of writing technical documentation!




回答3:


jvm has the potential to run more than one thread absolutely simultaneously

Yes, it is true. In a multicore / multiprocessor system it is possible to have more than one currently running thread.there are some background threads running in the JVM which you have on every system even in single processor system also.

The purpose of the method java.lang.Thread.currentThread() is to return the Thread object that is running the current code that calls the method.




回答4:


java.lang.Thread.currentThread() returns the thread in which is executed the method holding your code.

That doesn't mean at all that no other threads are running at the same time.

If you want to see the difference, you may iterate on all currently running threads with this piece of code :

for (Thread t : Thread.getAllStackTraces().keySet()) {
    if (t.getState()==Thread.State.RUNNABLE) {
          // see !
    }
}

 (or simply count them)




回答5:


It returns a reference to the currently executing thread object.

See : http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html




回答6:


does the api method java.lang.Thread.currentThread() returning just one thread no longer make sense

It only makes sense if you have multiple threads. It return the thread currently running the code. Something you can't obtain any other way.



来源:https://stackoverflow.com/questions/12216853/semantics-of-thread-currentthread-on-multicore-multi-processor-systems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!