标题: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-419383-1-1.html




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

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