線程的三個方法: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
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章