Thread
類的getThreadGroup()
方法用於返回該線程所屬的線程的線程組。 如果此線程已死(已停止),則此方法返回null
。
語法
public final ThreadGroup getThreadGroup()
返回
此方法返回線程的線程組。
示例
class JavaGetThreadGroupExp implements Runnable {
public void run()
{
System.out.println("xuhuhu.com");
}
}
public class GetThreadGroup
{
public static void main(String[] args)
{
// create thread groups
ThreadGroup group = new ThreadGroup("ThreadGroup");
ThreadGroup anotherGroup = new ThreadGroup(group, "AnotherGroup");
// create threads and placed into thread group
Thread t1 = new Thread(group, new JavaGetThreadGroupExp(), "Thread-1");
Thread t2 = new Thread(anotherGroup, new JavaGetThreadGroupExp(), "Thread-2");
// Start the threads
t1.start();
t2.start();
// returns the Thread Group to which this thread belongs
System.out.println(t1.getName() +" is a member of " + t1.getThreadGroup().getName());
System.out.println(t2.getName()+ " is a member of "+ t2.getThreadGroup().getName());
}
}
執行上面示例代碼,得到以下結果:
Thread-1 is a member of ThreadGroup
Thread-2 is a member of AnotherGroup
xuhuhu.com
xuhuhu.com
上面的示例使用Thread
類的getThreadGroup()
方法獲取ThreadGroup
的對象,然後使用getName()
方法獲取線程組的名稱。
上一篇:
Java Runtime類
下一篇:無