当前位置: 代码迷 >> Java相关 >> Thread t = Thread.currentThread();是什么意思??
  详细解决方案

Thread t = Thread.currentThread();是什么意思??

热度:651   发布时间:2013-08-20 08:31:21.0
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  

----------------解决方案--------------------------------------------------------
实例化
----------------解决方案--------------------------------------------------------
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 编辑 ]
----------------解决方案--------------------------------------------------------
  相关解决方案