currentThread()
currentThread()方法可返回代碼段正在被哪個(gè)線程調(diào)用的信息庞钢。
Thread.currentThread().getName();
Example:
class MyThread1 extends Thread {
MyThread1(){
System.out.println("執(zhí)行構(gòu)造函數(shù)"+Thread.currentThread().getName());
}
@Override
public void run() {
super.run();
System.out.println("run方法"+Thread.currentThread().getName());
}
}
public class Run {
public static void main(String[] args) {
MyThread1 myThread1= new MyThread1();
myThread1.start();
System.out.println("main方法"+Thread.currentThread().getName());
}
}
Result:
執(zhí)行構(gòu)造函數(shù)main
main方法main
run方法Thread-0
從運(yùn)行結(jié)果可以看出甲锡,MyThread1的構(gòu)造函數(shù)是被main線程調(diào)用的缚忧,而run()是被名稱為Thread-0的線程調(diào)用的豺裆,run()是自動(dòng)調(diào)用的。
isAlive()
isAlive()判斷當(dāng)前線程是否處于活動(dòng)狀態(tài)
Example:
class MyThread1 extends Thread {
@Override
public void run() {
super.run();
System.out.println("run="+isAlive());
}
}
public class Run {
public static void main(String[] args) {
MyThread1 myThread1= new MyThread1();
System.out.println("begin="+myThread1.isAlive());
myThread1.start();
try {
Thread.sleep(1000);
System.out.println("end="+myThread1.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Result:
begin=false
run=true
end=false
end=false荐健,由于Thread.sleep(1000)后音半,myThread1的run方法以及執(zhí)行完畢了。所以end=false
sleep()
sleep()方法的作用是在指定的毫秒數(shù)內(nèi)讓當(dāng)前正在執(zhí)行的線程進(jìn)行休眠(暫停執(zhí)行).這個(gè)正在執(zhí)行的線程是指this.currentThread()返回的的線程
Example:
class MyThread1 extends Thread {
@Override
public void run() {
super.run();
System.out.println("MyThread1 begin"+System.currentTimeMillis());
try {
Thread.sleep(2000);
System.out.println("MyThread1 end"+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) {
MyThread1 myThread1=new MyThread1();
System.out.println("main begin"+System.currentTimeMillis());
myThread1.start();
System.out.println("main end"+System.currentTimeMillis());
}
}
Result:
main begin1497171784723
main end1497171784723
MyThread1 begin1497171784738
MyThread1 end1497171786739
由于main線程與MyThread1線程是異步執(zhí)行的玖喘,所以首先打印的信息是main begin 和 main end甩牺,并且時(shí)間是相等的,而MyThread1線程是隨后運(yùn)行的累奈,所以打印MyThread1 begin 和 MyThread1 end 贬派,注意相差2000
getId()
該方法是取得線程的唯一標(biāo)識(shí)
停止線程
停止一個(gè)線程意味著在線程處理完任務(wù)前停掉正在做的操作。
有3種方式可以終止正在運(yùn)行的線程
- 使用退出標(biāo)志,使線程正常退出澎媒,也就是run方法完成后退出
- 使用stop(強(qiáng)烈不推薦)搞乏,這種暴力停止有可能使一些請理性的工作得不到完成。而且對鎖定的對象進(jìn)行了解鎖戒努,導(dǎo)致數(shù)據(jù)得不到同步的吹请敦,出現(xiàn)數(shù)據(jù)不一致的問題。
- 使用interrupt方法
interrupt()
調(diào)用interrupt()并不會(huì)直接停止該線程储玫,只是在當(dāng)前線程中打了一個(gè)停止的標(biāo)記
Example:
class MyThread1 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i < 500000; i++) {
System.out.println(i);
}
}
}
public class Run {
public static void main(String[] args) {
MyThread1 myThread1=new MyThread1();
myThread1.start();
try {
Thread.sleep(2000);
myThread1.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Result:
...
...
499997
499998
499999
從結(jié)果可以看到侍筛,interrupt()并沒有停止該線程。
this.interrupted()和this.isInterrupted()區(qū)別
this.interrupted():測試當(dāng)前線程是否已經(jīng)中斷,這里當(dāng)前線程是指運(yùn)行this.isterrupted()方法的線程,具有將狀態(tài)標(biāo)志置清除為false
this.isInterrrupted():測試線程是否已經(jīng)中斷,不清除狀態(tài)標(biāo)志
yield()
該方法作用是放棄當(dāng)前CPU資源撒穷,將它讓給其他的任務(wù)去占用CPU執(zhí)行時(shí)間匣椰。但放棄時(shí)間不確定。
class MyThread1 extends Thread {
@Override
public void run() {
super.run();
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 50000; i++) {
// Thread.yield();
i++;
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-beginTime);
}
}
public class Run {
public static void main(String[] args) {
MyThread1 myThread1=new MyThread1();
myThread1.start();
}
}
注釋掉Thread.yield()端礼,輸出1窝爪,解開的話11弛车。
線程優(yōu)先級(setPriority())
設(shè)置線程的優(yōu)先級使用setPriorty()齐媒。一共分為1~10這10個(gè)等級蒲每。
線程優(yōu)先級特性:
- 繼承性:A線程啟動(dòng)B線程,則B線程和A線程優(yōu)先級是一樣的
- 規(guī)則性:CPU盡量將執(zhí)行資源讓給優(yōu)先級較高的線程
- 隨機(jī)性:優(yōu)先級較高的線程不一定每一次都先執(zhí)行完喻括。