该方法可返回代码段正在被哪个线程调用的信息
1 package com.cky.test; 2 3 /** 4 * Created by chenkaiyang on 2017/12/2. 5 */ 6 public class Test { 7 public static void main(String[] args){ 8 9 System.out.println(Thread.currentThread().getName()); 10 } 11 }
D:itjdk1.8injava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
esources.jar;D:itjdk1.8jrelib
t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
main
Process finished with exit code 0
结果分析:main方法被名为main的线程调用
测试下面
1 package com.cky.thread; 2 3 /** 4 * Created by chenkaiyang on 2017/12/2. 5 */ 6 public class MyFirstThread extends Thread{ 7 8 public MyFirstThread () { 9 System.out.println("构造方法的打印:"+ Thread.currentThread().getName()); 10 } 11 @Override 12 public void run() { 13 super.run(); 14 System.out.println("run方法的打印" +Thread.currentThread().getName()); 15 } 16 }
D:itjdk1.8injava -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
esources.jar;D:itjdk1.8jrelib
t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
构造方法的打印:main
run方法的打印Thread-0
Process finished with exit code 0
1 package com.cky.test; 2 3 import com.cky.thread.MyFirstThread; 4 5 /** 6 * Created by chenkaiyang on 2017/12/2. 7 */ 8 public class Test { 9 public static void main(String[] args){ 10 MyFirstThread myFirstThread = new MyFirstThread(); 11 //myFirstThread.start(); 12 myFirstThread.run(); 13 } 14 }
D:itjdk1.8injava -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
esources.jar;D:itjdk1.8jrelib
t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
构造方法的打印:main
run方法的打印main
Process finished with exit code 0
结果分析:MyFirstThread类的构造函数是被main线程调用的,而run方法是被名为Thread-0的线程调用的,run方法是自动调用的方法。
下面测试一个比较复杂的情况
1 package com.cky.thread; 2 3 /** 4 * Created by edison on 2017/11/28. 5 */ 6 public class CountOperate extends Thread { 7 8 public CountOperate() { 9 System.out.println("countbegin"); 10 //执行构造方法的线程式main线程 11 System.out.println("Thread Name="+ Thread.currentThread().getName()); 12 //当前定义这个线程的名字 13 System.out.println("this.name="+this.getName()); 14 System.out.println("runend"); 15 } 16 @Override 17 public void run() { 18 super.run(); 19 System.out.println("runbegin"); 20 //执行run方法的这个线程是A线程 21 System.out.println("Thread Name="+Thread.currentThread().getName()); 22 //当前定义这个线程的名字 23 System.out.println("this.name="+this.getName()); 24 System.out.println("runend"); 25 } 26 }
1 package com.cky.test; 2 3 import com.cky.thread.CountOperate; 4 5 /** 6 * Created by edison on 2017/11/28. 7 */ 8 public class Test13 { 9 public static void main(String[] args) { 10 CountOperate c = new CountOperate(); 11 Thread thread = new Thread(c); 12 thread.setName("A"); 13 thread.start(); 14 } 15 }
1 C:itsoftjdkinjava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib esources.jar;C:itsoftjdkjrelib t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test13 2 countbegin 3 Thread Name=main 4 this.name=Thread-0 5 runend 6 runbegin 7 Thread Name=A 8 this.name=Thread-0 9 runend 10 11 Process finished with exit code 0