程序如下:
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 ( Thread.currentThread() == creatorThread ) {
System.out.println("Creator thread");
} else if ( Thread.currentThread() == 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();
}
}
}请问printMsg()中的this引用的是什么?(最好讲一下原理)

解决方案 »

  1.   

    this表示TwoThread类运行的一个实例,tt也是一个实例(this产生的线程).
      

  2.   

    当你定义用一个类定义一个对象的时候这个THIS就相当于指针,指向你所定义的此对象
      

  3.   

    请问是否可以这样理解this代表当前新创建的线程,也就是TwoThread类的对象tt. 当还有TwoThread的对象tt2时这时this代表线程tt2.这样理解是否正确?   TwoThread tt = new TwoThread();
       TwoThread tt2 = new TwoThread();
       tt.start();
       tt2.start();
          ......
          ......
      if( Thread.currentThread() == this ) 
                     System.out.println("New thread");
    其中this代表tt或tt2可以这样理解吗?
    nimifeng(学海无涯.......苦啊苦啊苦作舟....理解是美!!!)说“tt也是一个实例(this产生的线程).”这个“this产生的线程”作何解?
      

  4.   

    小弟还有一个问题,在程序中
    if ( Thread.currentThread() == creatorThread ) {
    System.out.println("Creator thread");
    } else if ( Thread.currentThread() == this ) {
    System.out.println("New thread");

    若在执行完Thread.currentThread()与creatorThread的比较后,线程马上发生线程切换,那么输出的结果就不一定能反映两个线程运行的真正情况了。
       我不知道我表达清楚了没有^_^
      

  5.   

    搂主的程序中启动了两个线程,因为执行这个程序的时候使用的是:
    java TwoThread,假定这个时候创建的线程是Thread1
    在Thread1中派生了一个线程:
    TwoThread tt = new TwoThread();
    tt.start();
    假定这个线程是Thread2。
    由于CPU始终是单线程的,所以在运行的时候也就是Thread1和Thread2进行互相切换。下面我们来分析上述的代码,首先来看new TowThread(),在这个方法中由于Thread2还没有创建,当前的Thread.currentThread()指的是Thread1,这个属性被传递给creatorThread,所以currentThread指向的Thread1。
    接着tt.start(),CPU的在Thread1和Thread2之间调度,this指的是当前正在运行的类,如果是Thread1在运行,那么指向的是Thread1,否则指向的是Thread2。
    从Java的线程调度机制来说,首先会将Thread1运行完成,所以最开始的输出是连续十次的Creator thread;然后是连续十次的New thread。
    如果我们在tt.start()后紧接着让Thread1睡眠,这个时候Thread2将运行,这个时候的this指向的是Thread2,所以结果和上面相反。
    tt.start();
    try
    {
      sleep(1000);
    }
    catch(Exception ex)
    {}
    for ( int i = 0; i < 10; i++ ) 
    {
      tt.printMsg();
    }
      

  6.   

    我的分析已经转到我在csdn的BLOG中,大家有什么问题我们可以在那里面继续探讨。
      

  7.   

    等写完后才想起来,我说的有一句话是错误的(this代表的是类实例化对象的指针):
    this是指当前线程没有错误,不过由于再次调用的时候使用的是tt.printMsg(),所以this指代的是Thread2,而不是Thread1
      

  8.   

    呵呵,bison_java(Java野牛) 你的前半部分说的是对的,但是你说"从Java的线程调度机制来说,首先会将Thread1运行完成,所以最开始的输出是连续十次的Creator thread;然后是连续十次的New thread。"时不一定的.不信你多执行几次试试!
      你还没有解决我的问题^_^
      

  9.   

    this与线程无关,它是一个指代当前正在运行的类实例化的对象(当前正在运行是指已经进入的栈)tt.printMsg(),所以this指代的是tt由于java 类的时候没有创建类来,所以没有this可以使用。你说的我的给出来的结果不一定正确,只不过是线程优先级的问题。可以简单点说:
    ClassA classA = new ClassA();
    执行任何classA.method()的时候,其中的this都是指代classA。
      

  10.   

    this 它是一个指当前正在运行的类实例化的对象,线程执行他自己的语句顺序是一致的,但语句在CPU上的实际处理顺序是不定的。所以
    if ( Thread.currentThread() == creatorThread ) {
    System.out.println("Creator thread");
    } else if ( Thread.currentThread() == this ) {
    System.out.println("New thread");
    } else {
    System.out.println("Mystery thread --unexpected!");
    }
    这段代码显示的是main线程,或tt线程。
    我不知道楼主清楚了没有。
      

  11.   

    public static void main(String[] args) {
    TwoThread tt = new TwoThread();
    // output:new thread
    // Thread.currentThread() 返回当前正在运行的TwoThread
    tt.start();for ( int i = 0; i < 10; i++ ) {
    // output:creatorThread
    // Thread.currentThread() 返回main Thread ,this指当前对象
    tt.printMsg();
    }
    }
    }
      

  12.   

    this仅仅指的是“类的实例”本身,也就是对象的本身,因此我们在考虑this的时候,不需要考虑什么线程之类的东西,只需要知道这个this指的是哪个“对象”。
    在楼主的程序中,一共产生了两个线程:一个是主线程,一个是由主线程抛出的一个子线程。当我们运行Java程序的时候,JVM会创建一个主线程运行程序,而这个主线程贯穿于整个程序的运行过程中(关于主线程的概念,其实可以看一看操作系统中有关的概念)。
    当运行到楼主的tt.start(); 时,主线程这时候抛出了一个子线程(我们暂时称这个线程为Thread_tt)。
    而在这条语句之前的TwoThread tt = new TwoThread(); 实际上并没有抛出新的线程,这时候正在运行的线程是主线程, 所以 tt中的属性 creatorThread 的指应该是主线程。
    这时候如果要考虑程序运行的结果,可以这样分析:
    1、什么时候才会运行tt.printMsg()?其实只有主线程在运行的时候才会运行main方法中的tt.printMsg(),而当线程Thread_tt在运行的时候,才会运行run方法中的printMsg()方法。
    2、有了上面的认识,那么我们自然可以得到如下的结果
       当主线程运行时,调用了tt.printMsg(), tt中的this指的是tt本身,tt的属性creatorThread 的指向主线程,Thread.currentThread()的值是主线程,因此输出Creator thread
       当Thread_tt在运行时,调用run中的printMsg方法, tt中的this指的是tt本身,tt的属性creatorThread 的指向主线程,Thread.currentThread()的值是Thread_tt-- 其实Thread_tt就是tt对象,因此Thread.currentThread()等于this,所以输出New thread。
      
       说了这么多,不知道大家能不能理解我说的,好像是比较麻烦。呵呵。
      

  13.   

    呵呵, tangqs(leon_tangqs) ( ) 信誉:100 ,我能理解你的意思,可是不好意思在我结帖了,才看到你的回复,没有分给你了^_^