这个帖子本来觉得明白了,可后来细想,还是不明白。
一个测试当前处于哪个线程的小例子,在printMsg方法里,第二个条件t==this,可以判断出当该方法被该类的新线程调用时的情况,这我就不明白了,this本是指明当前类的实例的,怎么又成了一个线程了。运行的结果却证实了这一点。
package test;/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-1-5
* Time: 22:44:08
* To change this template use File | Settings | File Templates.
*/
public class TwoThread extends Thread{
private Thread creatorThread; public TwoThread() {
this.creatorThread = Thread.currentThread();
} public void run() {
for (int i = 0; i < 10; i++) {
printMsg(); }
}
public void printMsg(){
Thread t=Thread.currentThread();
if (t==creatorThread){
System.out.println("创建者线程");
}else if(t==this){
System.out.println("新线程");
} else{
System.out.println("不知道的线程");
}
}
public static void main(String args[]){
TwoThread tt=new TwoThread();
tt.start();
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
}
}运行结果:
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程对象和线程是独立的两个东西,任何对象都是由线程来管理,在线程里运行的。所以,我觉得t==this怎么能和当前实例是同一个对象呢,尽管当前对象是一个Thread的子类,但它也是对象,是对象就是在线程里去运行的,不能说一个线程在另一个线程里运行吧,那就乱套了。
一个测试当前处于哪个线程的小例子,在printMsg方法里,第二个条件t==this,可以判断出当该方法被该类的新线程调用时的情况,这我就不明白了,this本是指明当前类的实例的,怎么又成了一个线程了。运行的结果却证实了这一点。
package test;/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-1-5
* Time: 22:44:08
* To change this template use File | Settings | File Templates.
*/
public class TwoThread extends Thread{
private Thread creatorThread; public TwoThread() {
this.creatorThread = Thread.currentThread();
} public void run() {
for (int i = 0; i < 10; i++) {
printMsg(); }
}
public void printMsg(){
Thread t=Thread.currentThread();
if (t==creatorThread){
System.out.println("创建者线程");
}else if(t==this){
System.out.println("新线程");
} else{
System.out.println("不知道的线程");
}
}
public static void main(String args[]){
TwoThread tt=new TwoThread();
tt.start();
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
}
}运行结果:
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
创建者线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程
新线程对象和线程是独立的两个东西,任何对象都是由线程来管理,在线程里运行的。所以,我觉得t==this怎么能和当前实例是同一个对象呢,尽管当前对象是一个Thread的子类,但它也是对象,是对象就是在线程里去运行的,不能说一个线程在另一个线程里运行吧,那就乱套了。
TwoThread tt=new TwoThread();产生另一线程对象tt,创建者是main线程。
你加了Thread t=Thread.currentThread();这句就是取得了进入了当前方法的线程对象啊,有什么不对的?Java线程本身就是一个Thread对象啊。不明白的话看看这个
package test;public class TwoThread extends Thread {
private Thread creatorThread; public TwoThread() {
this.creatorThread = Thread.currentThread();
} public void run() {
System.out.println("新建的线程对象名字:"+Thread.currentThread().getName());
for (int i = 0; i < 10; i++) {
printMsg();
}
} public void printMsg() {
Thread t = Thread.currentThread();
System.out.println("当前线程对象名字:"+t.getName());
if (t == creatorThread) {
System.out.println("创建者线程");
} else if (t == this) {
System.out.println("新线程");
} else {
System.out.println("不知道的线程");
}
} public static void main(String args[]) {
System.out.println("main线程对象名字:"+Thread.currentThread().getName());
TwoThread tt = new TwoThread();
tt.start();
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
}
}运行结果:main线程对象名字:main
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
当前线程对象名字:main
创建者线程
新建的线程对象名字:Thread-0
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
当前线程对象名字:Thread-0
新线程
线程的执行也是有语句实现的,我们写这些语句都要在一个线程里执行,就像在main方法里执行语句一样,它是在主线程里执行。
TwoThread tt = new TwoThread();//这一句表明把tt.creatorThread = 主线程
而tt.start();//则是在main里有重新启动了一个线程tt,tt线程是脱离主线程来执行的
当进入printMsg里以后,如果执行的当前线程是:tt,显示的结果就是:新线程
如果执行的当前线程是:main,显示的结果就是:创建者线程
我不知道这样说楼主明白不明白?
TwoThread tt=new TwoThread();
tt.start();
for (int i = 0; i < 10; i++) {
tt.printMsg();
}你注意到这里,tt.start启动的是新构造的线程,这个线程使用构造函数public TwoThread() {
this.creatorThread = Thread.currentThread();}此时给creatorThread赋值的是这个新构建的线程,其打印;执行完run后,接着执行,这个时间执行的是main线程,即jvm执行static方法的线程,此时再打印的时候,当然不是刚刚刚刚通过构造方法创建的线程喽
和delphi的self一样
没错,这个类的实例是一个线程,但它也是个对象,对象总是被线程所管理,运行在一个线程里,如果按你说的,它是个线程对象,this就代表线程,那它运行在哪里?运行在自己里面?那就成了线程运行在线程里,所以我很糊涂。一个对象可能一会被这个线程调用,一会儿被那个线程调用。总不能被自身调用吧。
System.out.println("新建的线程对象名字:"+Thread.currentThread().getName());
for (int i = 0; i < 10; i++) {
printMsg();
}
invokeSelf();
}
public void invokeSelf(){
new TwoThread().run();
}
this.creatorThread = Thread.currentThread();这时当前进程是谁,是main,然后 tt.start();
创建一个线程,接着main继续执行就出现了
创建者线程
接着tt继续运行,就出现了
新线程,
楼主看这样解释是不是恰些当
t是一个引用,this当然是一个当前对象的引用.可以比较.
没错,一切都是对象,t代表着当前运行的线程,但this只是一个类的实例呀,尽管这里比较特殊,类就是一个线程类,但仍然是对象,是线程中运行的东西,不是线程本身吧,线程我理解是一个对象运行的环境。要不然,为什么一个对象的方法既可以被自己的线程调用,也可以被main线程或是其它线程调用呢
对象和线程是独立的两个东西,任何对象都是由线程来管理,在线程里运行的。所以,我觉得t==this怎么能和当前实例是同一个对象呢,尽管当前对象是一个Thread的子类,但它也是对象,是对象就是在线程里去运行的,不能说一个线程在另一个线程里运行吧,那就乱套了。解:
t值的是当前线程,this指的是当前实例对象
而当前实例对象正好就是一个线程(当前线程)
顺理成章的t就=this了
* @author Hill
*/
public class TwoThread3 extends Thread {
public TwoThread3() {
//this.creatorThread = Thread.currentThread();
}public void run() {
for (int i = 0; i < 10; i++) {
printMsg();
}
}public void printMsg() {
Thread t = Thread.currentThread();
//t指向当前运行的main线程对象,this指向当前TwoThread3线程对象
if (t == this) {
System.out.println("当前运行线程是TwoThread3线程");
} else {
System.out.println("不是TwoThread3线程(其实是main线程)");
}
}public static void main(String[] args) {
TwoThread3 tt = new TwoThread3();
//tt.start();
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
}
}
Thread.currentThread()返回当前正在运行的线程对象,它可能是main线程,也可能是你创建的新线程
照你的意思,TwoThread的实例对象,是运行在自己生成的线程里,自己管理自己的对象的运行,这看上去比较费解啊。好像一个人既是自己,又是自己的爸爸。
this.creatorThread = Thread.currentThread();
}
在TwoThread() 里面这个Thread.currentThread()返回的到底是tt还是main的线程?
从结果看,应该是tt线程才对,也就是tt.creatorThread==tt
不过这样看来是tt创建了自己?晕了
private Thread creatorThread; public TwoThread() {
this.creatorThread = Thread.currentThread();
} public void run() {
for (int i = 0; i < 10; i++) {
printMsg(); }
}
public void printMsg(){
Thread t=Thread.currentThread();
if (t==creatorThread){
System.out.println(this.getClass().getName() + " creatorThread 创建者线程");
}else if(t==this){
System.out.println(this.getClass().getName() + " TwoThread 新线程");
} else{
System.out.println("不知道的线程");
}
}
public static void main(String args[]){
TwoThread tt=new TwoThread();
tt.start();
try{
tt.join();
}
catch(Exception ex){
ex.printStackTrace();
}
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
}
}输出:
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread TwoThread 新线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread creatorThread 创建者线程
TwoThread tt=new TwoThread();
tt.start();
创建一个新线程,开始运行TwoThread中的 run方法,由于构造函数
public TwoThread() {
this.creatorThread = Thread.currentThread();
}
所以tt==creatorThread即为当前的线程,运行
if (t==creatorThread){
System.out.println("创建者线程");
又由于FOR循环
public void run() {
for (int i = 0; i < 10; i++) {
printMsg();
,10次后,printMsg();结束,开始接着运行主函数中的FOR循环,
public static void main(String args[]){
TwoThread tt=new TwoThread();
tt.start();
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
而此时的tt所赋的值为线程是TwoThread(因为刚才运行run方法),所以此时FOR中运行
else if(t==this){
System.out.println("新线程");
我就这点想法,不知道对不对,也不知道描述的清不清楚,我已经尽力了呀,