码迷,mamicode.com
首页 > 其他好文 > 详细

1.3currentThread()方法

时间:2017-12-03 13:57:50      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:test   super   view   gen   xtend   bin   read   none   utf-8   

该方法可返回代码段正在被哪个线程调用的信息

 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:\it\jdk1.8\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=D:\it\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\it\jdk1.8\jre\lib\charsets.jar;D:\it\jdk1.8\jre\lib\deploy.jar;D:\it\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\it\jdk1.8\jre\lib\ext\cldrdata.jar;D:\it\jdk1.8\jre\lib\ext\dnsns.jar;D:\it\jdk1.8\jre\lib\ext\jaccess.jar;D:\it\jdk1.8\jre\lib\ext\jfxrt.jar;D:\it\jdk1.8\jre\lib\ext\localedata.jar;D:\it\jdk1.8\jre\lib\ext\nashorn.jar;D:\it\jdk1.8\jre\lib\ext\sunec.jar;D:\it\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\it\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\it\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\it\jdk1.8\jre\lib\ext\zipfs.jar;D:\it\jdk1.8\jre\lib\javaws.jar;D:\it\jdk1.8\jre\lib\jce.jar;D:\it\jdk1.8\jre\lib\jfr.jar;D:\it\jdk1.8\jre\lib\jfxswt.jar;D:\it\jdk1.8\jre\lib\jsse.jar;D:\it\jdk1.8\jre\lib\management-agent.jar;D:\it\jdk1.8\jre\lib\plugin.jar;D:\it\jdk1.8\jre\lib\resources.jar;D:\it\jdk1.8\jre\lib\rt.jar;F:\springboot\threaddemo\out\production\threaddemo;D:\it\idea\IntelliJ IDEA 2016.3.3\lib\idea_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 }
View Code

D:\it\jdk1.8\bin\java -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\it\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\it\jdk1.8\jre\lib\charsets.jar;D:\it\jdk1.8\jre\lib\deploy.jar;D:\it\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\it\jdk1.8\jre\lib\ext\cldrdata.jar;D:\it\jdk1.8\jre\lib\ext\dnsns.jar;D:\it\jdk1.8\jre\lib\ext\jaccess.jar;D:\it\jdk1.8\jre\lib\ext\jfxrt.jar;D:\it\jdk1.8\jre\lib\ext\localedata.jar;D:\it\jdk1.8\jre\lib\ext\nashorn.jar;D:\it\jdk1.8\jre\lib\ext\sunec.jar;D:\it\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\it\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\it\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\it\jdk1.8\jre\lib\ext\zipfs.jar;D:\it\jdk1.8\jre\lib\javaws.jar;D:\it\jdk1.8\jre\lib\jce.jar;D:\it\jdk1.8\jre\lib\jfr.jar;D:\it\jdk1.8\jre\lib\jfxswt.jar;D:\it\jdk1.8\jre\lib\jsse.jar;D:\it\jdk1.8\jre\lib\management-agent.jar;D:\it\jdk1.8\jre\lib\plugin.jar;D:\it\jdk1.8\jre\lib\resources.jar;D:\it\jdk1.8\jre\lib\rt.jar;F:\springboot\threaddemo\out\production\threaddemo;D:\it\idea\IntelliJ IDEA 2016.3.3\lib\idea_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 }
View Code

D:\it\jdk1.8\bin\java -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:\it\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\it\jdk1.8\jre\lib\charsets.jar;D:\it\jdk1.8\jre\lib\deploy.jar;D:\it\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\it\jdk1.8\jre\lib\ext\cldrdata.jar;D:\it\jdk1.8\jre\lib\ext\dnsns.jar;D:\it\jdk1.8\jre\lib\ext\jaccess.jar;D:\it\jdk1.8\jre\lib\ext\jfxrt.jar;D:\it\jdk1.8\jre\lib\ext\localedata.jar;D:\it\jdk1.8\jre\lib\ext\nashorn.jar;D:\it\jdk1.8\jre\lib\ext\sunec.jar;D:\it\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\it\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\it\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\it\jdk1.8\jre\lib\ext\zipfs.jar;D:\it\jdk1.8\jre\lib\javaws.jar;D:\it\jdk1.8\jre\lib\jce.jar;D:\it\jdk1.8\jre\lib\jfr.jar;D:\it\jdk1.8\jre\lib\jfxswt.jar;D:\it\jdk1.8\jre\lib\jsse.jar;D:\it\jdk1.8\jre\lib\management-agent.jar;D:\it\jdk1.8\jre\lib\plugin.jar;D:\it\jdk1.8\jre\lib\resources.jar;D:\it\jdk1.8\jre\lib\rt.jar;F:\springboot\threaddemo\out\production\threaddemo;D:\it\idea\IntelliJ IDEA 2016.3.3\lib\idea_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 }
View Code
 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:\itsoft\jdk\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_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

 

1.3currentThread()方法

标签:test   super   view   gen   xtend   bin   read   none   utf-8   

原文地址:http://www.cnblogs.com/edison20161121/p/7954603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!