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