获取线程的基本信息
线程操作有四个常用的基本方法:
1.Thread.currentThread();
获取当前正在执行的线程;
代码示例:
public class TestCurrentThread {
public static void main(String[] args) {
// 获取当前线程
Thread thread = Thread.currentThread();
System.out.println("当前进程为:"+thread);
}
}
执行结果:
当前进程为:Thread[main,5,main]
[main,5,main]:【线程名称,线程优先级,线程组名称】
2.thread.getName();
获取线程的名称;
代码示例:
class TestGetName{
public static void main(String[] args) {
// 获取当前线程
Thread thread = Thread.currentThread();
// 获取当前进程名称
String name = thread.getName()