程序如下:
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引用的是什么?(最好讲一下原理)
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引用的是什么?(最好讲一下原理)
TwoThread tt2 = new TwoThread();
tt.start();
tt2.start();
......
......
if( Thread.currentThread() == this )
System.out.println("New thread");
其中this代表tt或tt2可以这样理解吗?
nimifeng(学海无涯.......苦啊苦啊苦作舟....理解是美!!!)说“tt也是一个实例(this产生的线程).”这个“this产生的线程”作何解?
if ( Thread.currentThread() == creatorThread ) {
System.out.println("Creator thread");
} else if ( Thread.currentThread() == this ) {
System.out.println("New thread");
}
若在执行完Thread.currentThread()与creatorThread的比较后,线程马上发生线程切换,那么输出的结果就不一定能反映两个线程运行的真正情况了。
我不知道我表达清楚了没有^_^
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();
}
this是指当前线程没有错误,不过由于再次调用的时候使用的是tt.printMsg(),所以this指代的是Thread2,而不是Thread1
你还没有解决我的问题^_^
ClassA classA = new ClassA();
执行任何classA.method()的时候,其中的this都是指代classA。
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线程。
我不知道楼主清楚了没有。
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();
}
}
}
在楼主的程序中,一共产生了两个线程:一个是主线程,一个是由主线程抛出的一个子线程。当我们运行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。
说了这么多,不知道大家能不能理解我说的,好像是比较麻烦。呵呵。