PackageMythread; Public classCountoperateextendsthread{ Publiccountoperate () {System.out.println ("Countoperate---begin"); System.out.println ("Thread.CurrentThread (). GetName () =" +Thread.CurrentThread (). GetName ());//Gets the thread name System.out.println ("Thread.CurrentThread (). isAlive () =" +Thread.CurrentThread (). isAlive ()); See if the thread survives System.out.println ("This.getname=" + This. GetName ()); System.out.println ("this.isalive () =" + This. isAlive ()); System.out.println ("Countoperate---end"); System.out.println ("Thread.CurrentThread () ==this:" + (thread.currentthread () = = This)); } @Override Public voidrun () {System.out.println ("Run---begin"); System.out.println ("Thread.CurrentThread (). Getname=" +Thread.CurrentThread (). GetName ()); System.out.println ("Thread.CurrentThread (). IsAlive ()" +Thread.CurrentThread (). isAlive ()); System.out.println ("Thread.CurrentThread () ==this:" + (thread.currentthread () = = This)); System.out.println ("This.getname () =" + This. GetName ()); System.out.println ("this.isalive () =" + This. isAlive ()); System.out.println ("Run---end"); }}
Public class Run { publicstaticvoid main (string[] args) { new countoperate (); C.start (); New Thread (c); SYSTEM.OUT.PRINTLN ("main begin T1 Isalive=" + t1.isalive ()); T1.setname ("A"); T1.start (); SYSTEM.OUT.PRINTLN ("main end T1 isalive=" + t1.isalive ());} }
The printed log is:
Countoperate---Begin
Thread.CurrentThread (). GetName () =main
Thread.CurrentThread (). IsAlive () =true
this.getname=thread-0
This.isalive () =false
Countoperate---End
Thread.CurrentThread () ==this:false
Main begin T1 Isalive=false
Main End T1 Isalive=true
Run---Begin
Thread.CurrentThread (). getname=a
Thread.CurrentThread (). IsAlive () True
Thread.CurrentThread () ==this:false
This.getname () =thread-0
This.isalive () =false
Run---End
According to the printed log, it is known that the Countoperate constructor is called the main thread, so print out
Thread.CurrentThread (). GetName () =mainthread.currentthread (). IsAlive () =true
The Countoperate sub-thread is not started at this time, so print out
This.getname=thread-0this.isalive () =false
This represents the Countoperate object instance, so
Thread.CurrentThread () ==this:false
Here is more confusing is "this.getname () = Thread-0", what is this Thread-0???
By looking at the thread source code discovery, in the thread class's construction method, the name assignment is automatically assigned to the value:
Then proceed to:
thread T1 = new Thread (c);
System.out.println ("Main begin T1 Isalive=" + t1.isalive ());
t1.setname ("A");
T1.start ();
log print:
thread.currentthread (). Getname=a
thread.currentthread (). IsAlive () True
Thread.CurrentThread () ==this:false
this.getname () =thread-0
Span style= "COLOR: #ff0000" >this.isalive () =false
indicates that this and Thread.CurrentThread () point to not the same thread instance
That is, this refers to the thread instance created by new Countoperate (), not the instance that the new thread created, which is T1. The
View source code can be known.
In effect, the new thread will bind the object applied by thread to a Pravite variable target,
to be exact, when the Run method is executed, this.getname () actually returns Target.getname (), and Thread.CurrentThread (). GetName () is actually t1.getname ().
So we modify the code in the following main as:
Public class Run { publicstaticvoid main (string[] args) { new countoperate (); C.start (); }}
The printed log is:
Countoperate---Beginthread.currentthread (). GetName ()=Mainthread.currentthread (). IsAlive ()=true This. getname=thread-0 This. isAlive () =falsecountoperate---End Thread.CurrentThread ()== This:falseRun---Beginthread.currentthread (). GetName=thread-0Thread.CurrentThread (). IsAlive ()trueThread.CurrentThread ()== This:true This. GetName () =thread-0 This. isAlive () =trueRun---end
It's the same as the results we expected.
The difference between the Java multi-thread This and the Thread.CurrentThread ()