当前位置: 代码迷 >> Eclipse >> 怎么获得线程外线程的名字
  详细解决方案

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

热度:77   发布时间:2016-04-23 12:25:00.0
如何获得线程外线程的名字?
我建的线程结构如下:
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();
}
}
  相关解决方案