| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1111 人关注过本帖
标题:Thread t = Thread.currentThread();是什么意思??
只看楼主 加入收藏
初学者5
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2013-8-9
结帖率:75%
收藏
 问题点数:0 回复次数:2 
Thread t = Thread.currentThread();是什么意思??
public class TwoThread extends Thread {
    private Thread creatorThread;

    public TwoThread() {
        // make a note of the thread that constructed me!
        creatorThread =Thread.currentThread();
    }

    public void run() {
        for ( int i = 0; i < 10; i++ ) {
            printMsg();
        }
    }

    public void printMsg() {
        // get a reference to the thread running this
        Thread t = Thread.currentThread();

        if ( t == creatorThread ) {
            System.out.println("Creator thread");
        } else if ( t == this ) {
            System.out.println("New thread");
        } else {
            System.out.println("Mystery thread --unexpected!");
        }
    }

    public static void main(String[] args) {
        TwoThread tt = new TwoThread();
        tt.start();

        for ( int i = 0; i < 10; i++ ) {
            tt.printMsg();
        }
    }
}
程序中的creatorThread = Thread.currentThread();和Thread t = Thread.currentThread();是什么意思???
搜索更多相关主题的帖子: reference running private thread public 
2013-08-20 08:31
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 
实例化

学海无涯#¥%……&*(
2013-08-20 09:03
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
收藏
得分:0 
Thread.currentThread()可以获取当前线程的引用
可能这么说你不会太明白,这样吧,你看我增加了两句输出的代码

程序代码:
public class TwoThread extends Thread {
    private Thread creatorThread;

    public TwoThread() {
        // make a note of the thread that constructed me!
        creatorThread =Thread.currentThread();
    }

    public void run() {
        for ( int i = 0; i < 10; i++ ) {
            printMsg();
        }
    }

    public synchronized void printMsg() {
        // get a reference to the thread running this
        Thread t = Thread.currentThread();
       System.out.println(t.getId() + " | " + t.getName());//增加代码
       System.out.println(creatorThread.getId() + " | " + creatorThread.getName());//增加代码

        if ( t == creatorThread ) {
            System.out.println("Creator thread");
        } else if ( t == this ) {
            System.out.println("New thread");
        } else {
            System.out.println("Mystery thread --unexpected!");
        }
    }

    public static void main(String[] args) {
        TwoThread tt = new TwoThread();
        tt.start();

        for ( int i = 0; i < 10; i++ ) {
            tt.printMsg();
        }
    }
}

输出结果信息(会有所不同,下面是我本地运行的结果):
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
1 | main
1 | main
Creator thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread
8 | Thread-0
1 | main
New thread


这样你就会理解程序输出的结果是为什么是这样的了!

[ 本帖最后由 elan1986 于 2013-8-20 14:21 编辑 ]
2013-08-20 14:14
快速回复:Thread t = Thread.currentThread();是什么意思??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022483 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved