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?
java.lang.Thread.currentThread()
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.
A
B
- , , - . : " , , ". : " , ".
. , .
, , , threadA threadB. , threadA, currentThread(), threadA. , , threadB, currentThread(), threadB.
threadA
threadB
currentThread()
, . :
.
" " . " ". , .
, MSDN (!) :
Thread, .
Thread
"". , !
jvm has the potential to run more than one thread absolutely simultaneously
, . / . JVM , single processor.
single processor
java.lang.Thread.currentThread() - Thread, , .
java.lang.Thread.currentThread() , , .
, .
, :
for (Thread t : Thread.getAllStackTraces().keySet()) { if (t.getState()==Thread.State.RUNNABLE) { // see ! } }
( )
: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
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.