ThreadGroup类getName()方法
- getName() 方法在 java.lang 包中可用。
- getName() 方法用于返回该线程组的名称。
- getName() 方法是一个非静态方法,它只能通过类对象访问,如果我们尝试使用类名访问方法,那么我们将得到一个错误。
- getName() 方法在返回该线程组的名称时不抛出异常。
用法:
Public final String getName();
参数:
- 它不接受任何参数。
返回值:
这个方法的返回类型是String
– 返回该线程组的名称,名称为 "String" 类型。
例:
// Java program to demonstrate the example
// of String getName() method of ThreadGroup
public class GetName implements Runnable {
public static void main(String[] args) {
GetName gn = new GetName();
gn.getNamee();
}
public void getNamee() {
try {
// Create two thread group and the named are base
// and derived
ThreadGroup base = new ThreadGroup("Base ThreadGroup");
ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");
// create two threads
Thread th1 = new Thread(base, this);
Thread th2 = new Thread(derived, this);
//By using getName() method is to retrieve the
// name of the thread th1
System.out.println(th1.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th1
th1.start();
// By using getName() method is to retrieve the
// name of the thread th2
System.out.println(th2.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th2
th2.start();
System.out.println();
// By using getName() method is to return the
// name of the base and derived thread group
System.out.println("base.getName():" + " " + base.getName());
System.out.println("derived.getName():" + " " + derived.getName());
// By using join() method is to wait the current
// thread till complete execution of another
// thread
th1.join();
th2.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// Override run()
public void run() {
for (int k = 0; k < 100; ++k)
++k;
System.out.println(Thread.currentThread().getName() + " " + "ends.....");
}
}
输出
Thread-0 begins..... Thread-1 begins..... Thread-0 ends..... Thread-1 ends..... base.getName():Base ThreadGroup derived.getName():Derived ThreadGroup
相关用法
- Java ThreadGroup getMaxPriority()用法及代码示例
- Java ThreadGroup getParent()用法及代码示例
- Java ThreadGroup enumerate()用法及代码示例
- Java ThreadGroup parentOf()用法及代码示例
- Java ThreadGroup interrupt()用法及代码示例
- Java ThreadGroup suspend()用法及代码示例
- Java ThreadGroup setDaemon()用法及代码示例
- Java ThreadGroup isDaemon()用法及代码示例
- Java ThreadGroup checkAccess()用法及代码示例
- Java ThreadGroup resume()用法及代码示例
- Java ThreadGroup isDestroyed()用法及代码示例
- Java ThreadGroup toString()用法及代码示例
- Java ThreadGroup destroy()用法及代码示例
- Java ThreadGroup list()用法及代码示例
- Java ThreadGroup stop()用法及代码示例
- Java ThreadGroup activeCount()用法及代码示例
- Java ThreadGroup setMaxPriority()用法及代码示例
- Java ThreadGroup activeGroupCount()用法及代码示例
- Java Thread toString()用法及代码示例
- Java Thread interrupted()用法及代码示例
注:本文由纯净天空筛选整理自Preeti Jain大神的英文原创作品 Java ThreadGroup getName() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。