thread(this, ThreadName)对于Java中这个语句,this是什么?

就拿这个程序为例。
public class GetCurrentThread implements Runnable {
Thread th;

public GetCurrentThread(String threadName) {
th = new Thread(this,threadName); //<----DOUBT
System.out.println("get threadname "+th.getName());
th.start();
}

public void run() {
System.out.println(th.getName()+" is starting.....");
System.out.println("Current thread name : " + Thread.currentThread().getName());
}

public static void main(String args[]) {
System.out.println("Current thread name : " + Thread.currentThread().getName());
new GetCurrentThread("1st Thread");
//new GetCurrentThread("2nd Thread");
}
}


aluckdog
浏览 1084回答 2
2回答

肥皂起泡泡

Thread要的不是this,它要一个Runnable,在你的代码里,刚好this是一个Runnable,所以就这么写了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java