线程的三个方法:getName,setName,currentThread

/*
三个方法:
1、获取当前线程对象,Thread.currentName();
2、给线程起名,t.setName();
3、获取线程的名字,t.getName();
*/


public class fuck1{
public static void main(String[] args){
//如何获取当前线程对象
Thread t=Thread.currentThread();//t保存的内容地址指向的线程是“主线程对象”

//获取线程的名字
System.out.println(t.getName());//main

Thread t1=new Thread(new professor());
t1.setName("t1");
t1.start();

Thread t2=new Thread(new professor());
t2.setName("t2");
t2.start();
}

}


class professor implements Runnable{
public void run(){
Thread t=Thread.currentThread();//t保存的内容地址指向的线程是“t1线程对象”


System.out.println(t.getName());//Thread-0,Thread-1
}
}

猜你喜欢

转载自blog.csdn.net/rolic_/article/details/80730172