java怎么得到当前线程的启动线程id

Python015

java怎么得到当前线程的启动线程id,第1张

public class  线程id

{

public static void main(String[] args)

{

System.out.println("\n\t\t==========多线程怎么查看当前线程id==========\n")

init()

}//初始化!

private static void init()

{

for (int i=0i<2 i++ )

{

new Thread(new TestRunnable()).start()

}

}

}

class TestRunnable implements Runnable

{

//简单测试直接用了静态,偷懒了!

private static int i=10

public void run()

{

show()

}

synchronized void show()

{

while(i>=1)

System.out.println("当前执行的线程Id是:"+Thread.currentThread().getName()+"---->"+i--+"\n")

}

}

public class  线程id

{

public static void main(String[] args) 

{

System.out.println("\n\t\t==========多线程怎么查看当前线程id==========\n")

init()

}//初始化!

private static void init()

{

for (int i=0i<2 i++ )

{

new Thread(new TestRunnable()).start()

}

}

}

class TestRunnable implements Runnable

{

//简单测试直接用了静态,偷懒了!

private static int i=10

public void run()

{

show()

}

synchronized void show()

{

while(i>=1)

System.out.println("当前执行的线程Id是:"+Thread.currentThread().getName()+"---->"+i--+"\n")

}

}

可以的。

但是要先获取线程组,然后遍历线程组树,获取根线程组,然后再遍历所有线程,并分别判断所有的线程的id或者name,也就是说你可以通过id或者name来获取一个线程。