




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第詳解Java線程中常用操作目錄線程的常用操作守護(hù)線程(后臺(tái)線程)線程串行化線程優(yōu)先級(jí)線程中斷
線程的常用操作
設(shè)置線程名字:setName()
獲取線程名稱:getName()
線程唯一Id:getId()
//
自定義線程名稱
String
threadName
=
"threadName";
//
構(gòu)造方法方式
Thread
thread
=
new
Thread(()
-
{
System.out.println("線程名="
+
Thread.currentThread().getName());
},threadName);
//
set方法方式
//
thread.setName(threadName);
System.out.println("線程唯一Id="
+
thread.getId());
線程啟動(dòng):start()
判斷線程是否存活:isAlive()
//
線程啟動(dòng)
thread.start();
System.out.println("是否為存活線程="
+
thread.isAlive());
線程方法:run()/call()
線程啟動(dòng)后會(huì)去調(diào)用的方法。線程要做什么就在run/call方法寫,不需要直接調(diào)用,線程啟動(dòng)后自己會(huì)去調(diào)用run()/call()。如果程序沒有啟動(dòng)線程直接調(diào)用run/call,那么就不屬于多線程編程,是屬于當(dāng)前線程直接調(diào)用普通方法一樣。
獲取當(dāng)前線程對(duì)象:currentThread()
操作當(dāng)前線程的非static方法,得先拿到線程對(duì)象才可以
//
獲取當(dāng)前線程對(duì)象
Thread
currentThread
=
Thread.currentThread();
//
對(duì)當(dāng)前線程做一些操作
System.out.println(currentThread.getName());
try
{
//
sleep
靜態(tài)方法則不需要
Thread.sleep(1000);
}
catch
(InterruptedException
e)
{
e.printStackTrace();
關(guān)于線程的狀態(tài)控制(生命周期)的操作可以參考上一篇文章。
守護(hù)線程(后臺(tái)線程)
普通線程(用戶線程)的守護(hù)者,守護(hù)線程的任務(wù)是為其他的線程提供服務(wù)。如果進(jìn)程中沒有了用戶線程,那么守護(hù)線程也就沒有存在的意義,JVM也隨之結(jié)束。典型的守護(hù)線程有JVM的垃圾回收線程,操作系統(tǒng)的啟動(dòng)也會(huì)啟動(dòng)各種模塊的守護(hù)線程。
設(shè)置線程為守護(hù)線程:setDaeman()
注意:該方法必須在start()方法之前調(diào)用
public
static
void
main(String[]
args)
{
Thread
thread
=
new
Thread(()
-
{
System.out.println("線程名="+Thread.currentThread().getName());
try
{
Thread.sleep(1000);
}
catch
(InterruptedException
e)
{
e.printStackTrace();
}
//
這一句不會(huì)打印出來,因?yàn)閙ain線程(目前唯一的普通線程)等待1秒后已經(jīng)結(jié)束了
System.out.println("守護(hù)線程的狀態(tài)="
+
Thread.currentThread().getState());
});
//
守護(hù)線程
thread.setDaemon(true);
//
線程啟動(dòng)
thread.start();
System.out.println("是否為守護(hù)線程="
+
thread.isDaemon());
線程串行化
執(zhí)行join()方法的線程進(jìn)入等待喚醒狀態(tài)(WAITING),直到調(diào)用該方法的線程結(jié)束后再由等待喚醒狀態(tài)轉(zhuǎn)為可運(yùn)行狀態(tài)(RUNNABLE)。join()方法是Thread類中的方法,其底層是使用wait()方法來實(shí)現(xiàn)線程等待,待線程isAlive()為false時(shí)才
實(shí)現(xiàn)線程的串行化:一個(gè)線程調(diào)用另一個(gè)線程對(duì)象的join()來實(shí)現(xiàn)線程串行化執(zhí)行。
舉個(gè)例子:一道好菜
public
class
DemoCooking
{
public
static
void
main(String[]
args)
{
Thread
mainThread
=
Thread.currentThread();
//
1.買菜
Thread
buyThread
=
new
Thread(new
CookingThread(mainThread,"買菜"),"buyThread");
//
2.洗菜
Thread
washThread
=
new
Thread(new
CookingThread(buyThread,"洗菜"),"washThread");
//
3.切菜
Thread
cutThread
=
new
Thread(new
CookingThread(washThread,"切菜"),"cutThread");
//
4.炒菜
Thread
scrambleThread
=
new
Thread(new
CookingThread(cutThread,"炒菜"),"scrambleThread");
//
不受線程啟動(dòng)順序的影響
scrambleThread.start();
washThread.start();
cutThread.start();
buyThread.start();
//main線程先執(zhí)行完才可以開始:買菜
System.out.println("開始準(zhǔn)備……");
}
public
static
class
CookingThread
implements
Runnable{
private
final
Thread
thread;
private
final
String
job;
public
CookingThread(Thread
thread,
String
job){
this.thread
=
thread;
this.job
=
job;
}
@Override
public
void
run()
{
String
name
=
Thread.currentThread().getName()+":";
try
{
thread.join();
System.out.println(name
+
job
+
"開始");
Thread.sleep(1000);
System.out.println(name
+
job
+
"結(jié)束");
Thread.sleep(1000);
//
偷懶下
}
catch
(InterruptedException
e)
{
e.printStackTrace();
}
}
}
執(zhí)行結(jié)果:mainbuyThreadwashThreadcutThreadscrambleThread結(jié)束
開始準(zhǔn)備
buyThread:買菜開始
buyThread:買菜結(jié)束
washThread:洗菜開始
washThread:洗菜結(jié)束
cutThread:切菜開始
cutThread:切菜結(jié)束
scrambleThread:炒菜開始
scrambleThread:炒菜結(jié)束
線程優(yōu)先級(jí)
設(shè)置當(dāng)前線程的優(yōu)先級(jí),線程優(yōu)先級(jí)越高,線程可能獲得執(zhí)行的次數(shù)越多,Java線程的優(yōu)先級(jí)用整數(shù)表示,優(yōu)先級(jí)的范圍為1-10,默認(rèn)為5。
setPriority(int)方法:設(shè)置線程的優(yōu)先級(jí)。
getPriority方法:獲取線程的優(yōu)先級(jí)。
public
static
void
main(String[]
args)
{
Thread
thread
=
new
Thread(()
-
{
System.out.println("線程1");
});
thread.setPriority(10);
Thread
thread1
=
new
Thread(()
-
{
System.out.println("線程2");
});
thread1.setPriority(1);
thread.start();
thread1.start();
System.out.println("線程默認(rèn)的優(yōu)先級(jí)為="
+
Thread.currentThread().getPriority());
線程中斷
使用interrupt()方法設(shè)置線程中斷標(biāo)志=true,讓線程受到阻塞時(shí)拋出一個(gè)中斷信號(hào)。如果線程處于阻塞、等待喚醒或超時(shí)等待狀態(tài)(Object.wait,Thread.join和Thread.sleep)時(shí),那么它將接收到一個(gè)中斷異常(InterruptedException),從而提前被結(jié)束該狀態(tài)。反之,如果線程是處于可運(yùn)行(RUNNABLE)狀態(tài),那么中斷標(biāo)志將沒有作用。
案例一:線程中斷有效
public
static
void
main(String[]
args)
{
Thread
thread
=
new
Thread(()
-
{
System.out.println("線程1");
try
{
//
鬧鐘1分鐘后響
Thread.sleep(60000);
System.out.println("鬧鐘響了");
}
catch
(InterruptedException
e)
{
//
提前退出超時(shí)等待狀態(tài)
System.out.println("發(fā)生異常,提前醒了,鬧鐘沒響手動(dòng)關(guān)了");
}
System.out.println("繼續(xù)執(zhí)行該線程的后續(xù)程序……");
});
thread.setPriority(1);
thread.start();
errupt();
System.out.println("main線程將thread
終端狀態(tài)設(shè)置為
"+thread.isInterrupted());
執(zhí)行結(jié)果:
main線程將thread終端狀態(tài)設(shè)置為true
線程1
發(fā)生異常,提前醒了,鬧鐘沒響手動(dòng)關(guān)了
繼續(xù)執(zhí)行該線程的后續(xù)程序
案例二:線程中斷無效
public
static
void
main(String[]
args)
{
Thread
thread1
=
new
Thread(()
-
{
System.out.println("線
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 座位險(xiǎn)保險(xiǎn)合同協(xié)議書
- 糧食收割協(xié)議書
- 群眾自治協(xié)議書
- 男生友誼協(xié)議書
- 環(huán)保顧問協(xié)議書
- 組隊(duì)出游協(xié)議書
- 科研聯(lián)盟協(xié)議書
- 線上私教協(xié)議書
- 老師管理協(xié)議書
- 糊涂結(jié)婚協(xié)議書
- 中醫(yī)養(yǎng)生(靈源萬應(yīng)茶)
- 追索子女撫養(yǎng)費(fèi)起訴狀
- 六年級(jí)數(shù)學(xué)質(zhì)量分析PPT
- 土地平整、池塘推土、雜草灌木叢及樹木清除施工方案
- 眼鏡鏡架的整形專業(yè)培訓(xùn)2課件
- 下線儀式串詞策劃
- 通用長期供銷合同范本
- 新版《藥品管理法》解讀課件
- 《社區(qū)治理研究國內(nèi)外文獻(xiàn)綜述(1900字)》
- 2023浙江省學(xué)生藝術(shù)特長測試A級(jí)理論復(fù)習(xí)資料
- 建筑業(yè)企業(yè)資質(zhì)職稱人員相近專業(yè)認(rèn)定目錄
評(píng)論
0/150
提交評(píng)論