首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

怎么获得线程外线程的名字

2012-09-07 
如何获得线程外线程的名字?我建的线程结构如下:1.class NewThread extends Thread{2.public void run() {3

如何获得线程外线程的名字?
我建的线程结构如下:
1.class NewThread extends Thread{
2. public void run() {
3. new Thread(){
4. public void run() {
5.  
6. }
7. }.start();
8. }
9.}
我想在第5行打印一下线程NewThread 的名字,怎么获得NewThread 的名字呢?请诸位大神指点!

[解决办法]
public class ThreadTest {
public static void main(String[] args) {
new NewThread().start();
System.out.println(Thread.currentThread().getName());
}
}

class NewThread extends Thread {
public void run() {
final String string=Thread.currentThread().getName();
System.out.println("NewThread:"+Thread.currentThread().getName());
new Thread() {
public void run() {
System.out.println(string);//打印出NewThread的名字
System.out.println(Thread.currentThread().getName());
}
}.start();
}
}

热点排行