1.获取线程名字
通过Thread的getName方法获取线程对象名字
//获取主线程对象
Thread mainThread=new Thread();
System.out.println("名称"+mianThread.getName());
2.设置线程名称
通过setName(String)方法设置线程对象的名字
Thread thread=new Thread();
thread.setName("线程a");
System.out.println(thread.getName());
3.通过构造函数传入线程名字
public static void main(String[] args){
MyThread thread=new Thread("子线程");
thread.start();
}
class MyThread extends Thread{
public MyThread(String name){
super(name);
}
@Override
public void run(){
System.out.println("业务1..."+Thread.currentThread());
}
}