多线程中this、Thread.currentThread()和this.currentThread()的区别
2016-03-14 15:55
525 查看
this指的是当前对象,而Thread.currentThread()指的是当前运行的线程对象,这两者有时不一定相同。
如下列代码所示
public class CountOperate extends Thread{
public CountOperate(){
System.out.println("CountOperate---begin");
System.out.println("current name is "+Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
System.out.println("this name is "+this.getName());
System.out.println("this.isAlive()="+this.isAlive());
System.out.println("CountOperate---end");
}
}
main函数
public class Run {
public static void main(String[] args) {
// TODO Auto-generated method stub
CountOperate c = new CountOperate();
Thread t1 = new Thread(c);
}
}
输出结果
Thread.currentThread()和this.currentThread()在大部分情况下结果是相同的,都是调用Thread的静态方法currentThread(),若当前子类重写了父类的currentThread()方法,那么Thread.currentThread()和this.currentThread()返回的结果会不同。
如下列代码所示
public class CountOperate extends Thread{
public CountOperate(){
System.out.println("CountOperate---begin");
System.out.println("current name is "+Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
System.out.println("this name is "+this.getName());
System.out.println("this.isAlive()="+this.isAlive());
System.out.println("CountOperate---end");
}
}
main函数
public class Run {
public static void main(String[] args) {
// TODO Auto-generated method stub
CountOperate c = new CountOperate();
Thread t1 = new Thread(c);
}
}
输出结果
Thread.currentThread()和this.currentThread()在大部分情况下结果是相同的,都是调用Thread的静态方法currentThread(),若当前子类重写了父类的currentThread()方法,那么Thread.currentThread()和this.currentThread()返回的结果会不同。
相关文章推荐
- Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: table is not Mapped
- 进程总结
- Java JDBC
- https
- 敢问路在何方
- Java JDBC
- Java JDBC
- UVa 1225 - Digit Counting
- Bower使用教程(限window)
- 地图API
- Cobbler自动安装*nix系统
- Deep Learning Resources
- 二进制、八进制、十进制、十六进制之间的转换
- 高斯日记
- loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写
- 关于Redis中的数据类型
- 马云2016对新员工演讲
- afnetwork
- 设置UIImage的渲染模式:UIImage.renderingMode
- gulp自动化打包及静态文件自动添加版本号