Semantics Thread.currentThread () for multi-core / multi-processor systems?

If you are working on a multi-core or multi-processor computer, where jvm has the ability to run more than one thread absolutely simultaneously (not only visible at the same time), what does the api method return java.lang.Thread.currentThread()? .... in the above script, does it just randomly return one of the current threads?

+2
source share
6 answers

It returns the stream that you are currently using inside. If you have two cores and two threads Aand Brunning completely simultaneously, calling this method at the same time, it will return Aand Baccordingly.

- , , - . : " , , ". : " , ".

+8

. , .

, , , threadA threadB. , threadA, currentThread(), threadA. , , threadB, currentThread(), threadB.

, . :

.

" " . " ". , .

, MSDN (!) :

Thread, .

"". , !

+3

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

, . / . JVM , single processor.

java.lang.Thread.currentThread() - Thread, , .

+3

java.lang.Thread.currentThread() , , .

, .

, :

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

( )

+1

Does the api method use java.lang.Thread.currentThread (), returning only one thread, no longer makes sense

This makes sense if you have multiple threads. It returns a stream, the current code. What you cannot get in another way.

0
source

All Articles