java多線程Thread的實(shí)現(xiàn)方法代碼詳解
之前有簡(jiǎn)單介紹過java多線程的使用,已經(jīng)Thread類和Runnable類,為了更好地理解多線程,本文就Thread進(jìn)行詳細(xì)的分析。
start()
我們先來看看API中對(duì)于該方法的介紹:
使該線程開始執(zhí)行;Java 虛擬機(jī)調(diào)用該線程的 run 方法。
結(jié)果是兩個(gè)線程并發(fā)地運(yùn)行;當(dāng)前線程(從調(diào)用返回給 start 方法)和另一個(gè)線程(執(zhí)行其 run 方法)。
多次啟動(dòng)一個(gè)線程是非法的。特別是當(dāng)線程已經(jīng)結(jié)束執(zhí)行后,不能再重新啟動(dòng)。
用start方法來啟動(dòng)線程,真正實(shí)現(xiàn)了多線程運(yùn)行,這時(shí)無需等待run方法體代碼執(zhí)行完畢而直接繼續(xù)執(zhí)行下面的代碼。通過調(diào)用Thread類的 start()方法來啟動(dòng)一個(gè)線程,這時(shí)此線程處于就緒(可運(yùn)行)狀態(tài),并沒有運(yùn)行,一旦得到cpu時(shí)間片,就開始執(zhí)行run()方法,這里方法 run()稱為線程體,它包含了要執(zhí)行的這個(gè)線程的內(nèi)容,Run方法運(yùn)行結(jié)束,此線程隨即終止。
start方法是開啟線程的方法,使用后java會(huì)創(chuàng)建一個(gè)新的線程執(zhí)行run里的方法。這是一個(gè)小demo:
for(int i=0;i<3;i++){
Thread t= new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" end");
}
});
t.start();
}
System.out.println("it is over");
執(zhí)行結(jié)果:
it is over
Thread-1 start
Thread-0 start
Thread-2 start
Thread-0 end
Thread-1 end
Thread-2 end
由于多線程是有隨機(jī)性的,所以每次的結(jié)果可能都不一樣,這一點(diǎn)也是我們需要注意的,線程的執(zhí)行順序和調(diào)用順序并不一致。
run()
run方法就是調(diào)用Thread設(shè)置的Runnable的run方法,將上面的demo進(jìn)行修改:
for(int i=0;i<3;i++){
Thread t= new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" end");
}
});
t.run();
}
System.out.println("it is over");
執(zhí)行結(jié)果:
main start
main end
main start
main end
main start
main end
it is over
run方法的直接結(jié)果和start有很大的差別,完全是按順序執(zhí)行,并沒有開啟新線程。
stop()
stop方法是強(qiáng)制停止線程的執(zhí)行,是非安全的,不要使用此方法。在調(diào)用stop時(shí), 會(huì)對(duì)鎖定的資源進(jìn)行釋放,但這種釋放是非一致的,容易引起程序問題。如果想要控制線程的停止,可以使用自定義變量來判斷或者isInterrupted()方法:
class Thread1 extends Thread {
@Override
public void run() {
//判斷線程體是否運(yùn)行
while (!isInterrupted()) {
// Do Something
}
}
}
interrupt()
interrupt的作用是通知線程,你已經(jīng)被中斷的,但具體的中斷執(zhí)行需要在線程自定義處理,甚至你可以不理會(huì)繼續(xù)執(zhí)行。具體的中孤單是會(huì)線程執(zhí)行join、wait、sleep方法時(shí),拋出InterruptedException。
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" start");
try {
for(int i=0;i<100000;i++){
System.out.println(i+"");
Thread.sleep(1);
}
} catch (InterruptedException e) {
System.out.println("the thread is interrupted");//可以在這里做資源釋放,日志記錄等
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" end");
}
});
t1.start();
Thread.sleep(100);
t1.interrupt();
執(zhí)行結(jié)果:
65 66 67 68 the thread is interrupted java.lang.InterruptedException: sleep interrupted Thread-0 end at java.lang.Thread.sleep(Native Method) at com.wk.aqi.act.Test$1.run(Test.java:23) at java.lang.Thread.run(Thread.java:745)
isInterrupted()
判斷線程是否中斷,在執(zhí)行上面的interrupt方法后,會(huì)return true。
setPriority(int newPriority)和getPriority()
設(shè)置線程的優(yōu)先級(jí)和獲取線程的優(yōu)先級(jí),cpu分配的資源給側(cè)重給priority高的線程。
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
long t = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+" start");
for(int i=0;i<1000;i++){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" t1 end "+(System.currentTimeMillis()-t));
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
long t = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+" start");
for(int i=0;i<1000;i++){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" t2 end "+(System.currentTimeMillis()-t));
}
});
t1.setPriority(10);
t2.setPriority(1);
t2.start();
t1.start();
執(zhí)行結(jié)果:
Thread-0 start Thread-1 start Thread-0 t1 end 1357 Thread-1 t2 end 1371
在優(yōu)先級(jí)一樣的情況下,t1和t2是幾乎同時(shí)完成的,在優(yōu)先級(jí)不一樣的情況,有明顯的差別。
getName()
比較簡(jiǎn)單,獲取線程的名稱。
join()和join(long millis)
jion方法的作用是等待線程執(zhí)行完成,join(long millis)可以設(shè)置最長(zhǎng)等待時(shí)間。比如主線程需要等待子線程完成,獲取子線程的結(jié)果后才能繼續(xù)往下執(zhí)行,這時(shí)候就可以使用join方法
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
long t = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+" start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" t1 end "+(System.currentTimeMillis()-t));
}
});
t1.start();
t1.join();
System.out.println("等待t1執(zhí)行完,再執(zhí)行");
執(zhí)行結(jié)果:
Thread-0 start Thread-0 t1 end 1001 等待t1執(zhí)行完,再執(zhí)行
總結(jié)
以上就是本文關(guān)于java多線程Thread的實(shí)現(xiàn)方法代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。
相關(guān)文章
SpringBoot接口限流的實(shí)現(xiàn)方法小結(jié)
在一個(gè)高并發(fā)系統(tǒng)中對(duì)流量的把控是非常重要的,當(dāng)巨大的流量直接請(qǐng)求到我們的服務(wù)器上沒多久就可能造成接口不可用,不處理的話甚至?xí)斐烧麄€(gè)應(yīng)用不可用,所以我們需要接口限流,本文給大家介紹了SpringBoot接口限流的實(shí)現(xiàn)方法,需要的朋友可以參考下2024-10-10
Java發(fā)送郵件javax.mail的實(shí)現(xiàn)方法
這篇文章主要為大家介紹了Java發(fā)送郵件javax.mail的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,代碼都有詳細(xì)的注釋,感興趣的小伙伴們可以參考一下2016-01-01
java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用過程詳解
這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤
這篇文章主要介紹了Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
從零到掌握Spring Boot Validation 接口校驗(yàn)的詳細(xì)過程
本文詳細(xì)介紹了SpringBoot的Validation接口校驗(yàn)機(jī)制,包括其核心功能、常用注解、自定義校驗(yàn)、以及實(shí)際應(yīng)用場(chǎng)景,通過注解定義數(shù)據(jù)校驗(yàn)規(guī)則,感興趣的朋友跟隨小編一起看看吧2025-02-02
restTemplate未設(shè)置連接數(shù)導(dǎo)致服務(wù)雪崩問題以及解決
面對(duì)線上問題,仔細(xì)分析原因,及時(shí)調(diào)整配置,能有效解決問題,本文詳細(xì)描述了線上遇到流量突增引發(fā)的問題,通過查看代碼和連接池信息,分析出問題的原因是連接池滿了,連接池大小配置不足以應(yīng)對(duì)大并發(fā)流量,通過調(diào)整連接池大小配置2024-10-10
Java Spring框架簡(jiǎn)介與Spring IOC詳解
Spring 框架是一個(gè)輕量級(jí)的解決方案,可以一站式地構(gòu)建企業(yè)級(jí)應(yīng)用。它是為了解決 企業(yè)應(yīng)用開發(fā)的復(fù)雜性而創(chuàng)建的。Spring 使用基本的 JavaBean 來完成以前只可能由 EJB 完成的事情。IOC 是 Inversion of Control 的縮寫,多數(shù)書籍翻譯成控制反轉(zhuǎn)2021-09-09
idea向System.getenv()添加系統(tǒng)環(huán)境變量的操作
這篇文章主要介紹了idea向System.getenv()添加系統(tǒng)環(huán)境變量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot使用Minio進(jìn)行文件存儲(chǔ)的實(shí)現(xiàn)
本文主要介紹了SpringBoot使用Minio進(jìn)行文件存儲(chǔ)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

