[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[整理]Java线程:Thread.currentThread()与this的关系

上一篇:[转帖]禁止 Symantec Protection Agent 运行的两个方法。
下一篇:[整理]Eclipse RCP plugin开发,取得插件目录下的配置文件

添加日期:2011/6/22 11:22:11 快速返回   返回列表 阅读8464次
Thread.currentThread() 返回当前线程对象,也就是执行这句代码这个线程。

this,也是当前线程对象~~~~

如果线程是start()方法调用起来的,那么二者是相等的。
不过,run()方法有可能被直接调用,这种情况下就不相等了。


new Thread("aaa"){
    public void run(){
        System.out.println(Thread.currentThread() == this);
        
        new Thread("bbb"){
            public void run(){
                System.out.println(Thread.currentThread() == this);
            }
        }.start();

        new Thread("ccc"){
            public void run(){
                System.out.println(Thread.currentThread() == this);
            }
        }.run();
    }
}.start();



结果是:
true
true
false

第3个ccc线程是用run()方法调用的,那么就相当于new了一个普通类,调用了它的一个方法,仅此而已。
Thread.currentThread()返回的是aaa线程,this是ccc对象,所以不相等。
 

评论 COMMENTS
guest315596331
2012/7/27 17:21:37
guest795085658
2016/2/25 15:50:59
厉害
guest1005380073
2016/3/3 23:11:44
谢谢老司机
guest398476612
2016/7/11 21:04:30
guest412092479
2016/11/22 17:57:26
diao

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved