Java并發(fā)編程創(chuàng)建并運(yùn)行線程的方法對比
一、創(chuàng)建并運(yùn)行線程的五種方法
第一種:繼承Thread類
這種方式是最基礎(chǔ)的一種方式,學(xué)過java的朋友都知道,不做贅述。需要注意的是:覆蓋實(shí)現(xiàn)使用的是run方法,運(yùn)行線程是start方法。
public class FirstWay extends Thread {
@Override
public void run() {
System.out.println("第一種實(shí)現(xiàn)線程的方式:繼承Thread類");
}
//模擬測試
public static void main(String[] args) {
new FirstWay().start();
}
}
第二種:實(shí)現(xiàn)Runnable接口
第二種實(shí)現(xiàn)方式仍然很基礎(chǔ),繼承Runnable接口,重寫run方法實(shí)現(xiàn)線程運(yùn)行邏輯。需要注意的:運(yùn)行線程需要套一層new Thread 。
public class SecondWay implements Runnable{
@Override
public void run() {
System.out.println("第二種實(shí)現(xiàn)線程的方式:實(shí)現(xiàn)Runnable接口");
}
//模擬測試
public static void main(String[] args) {
new Thread(new SecondWay()).start();
}
}
第三種:實(shí)現(xiàn)Callable接口
第三種方式是實(shí)現(xiàn)Callable接口,Callable接口與Runable接口都能實(shí)現(xiàn)線程。
public class ThirdWay implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("第三種實(shí)現(xiàn)線程的方式:實(shí)現(xiàn)Callable接口");
return "Callable接口帶返回值,可以拋出異常";
}
//模擬測試
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> futureTask = new FutureTask<>(new ThirdWay());
new Thread(futureTask).start();
//阻塞方法,獲取call方法返回值
System.out.println(futureTask.get()); //打?。篊allable接口帶返回值,可以拋出異常
}
}
區(qū)別如下:
- Callable接口實(shí)現(xiàn)線程方法是call, Runable接口實(shí)現(xiàn)線程方法是run
- Callable有返回值, Runable接口不能有返回值
- Callable接口方法call返回值可以設(shè)置泛型,如下例子中使用String數(shù)據(jù)類型
- Callable接口方法call方法可以拋出異常,Runable接口run方法不可以拋出異常
- Callable接口方法通過
new Thread(futureTask).start()運(yùn)行,F(xiàn)utureTask的get方法可以獲取Callable接口方法call方法的返回值 - 如果Callable接口方法call方法異常,在FutureTask的get方法調(diào)用時(shí)也會拋出同樣的異常
第四種:線程池 + execute
從JDK5版本開始,java默認(rèn)提供了線程池的支持,用線程池的方式運(yùn)行線程可以避免線程的無限擴(kuò)張導(dǎo)致應(yīng)用宕機(jī),同時(shí)也節(jié)省了線程頻繁創(chuàng)建與銷毀的資源與時(shí)間成本。
public class FourthWay implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() +
":實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池");
}
public static void main(String[] args) {
//創(chuàng)建一個(gè)固定大小的線程池
ExecutorService threadPool = Executors.newFixedThreadPool(5);
for(int i = 0;i < 10;i++){
threadPool.execute(new FourthWay());
}
}
}
線程池ExecutorService使用execute方法運(yùn)行Runnable接口run方法的線程實(shí)現(xiàn),execute方法與run方法的共同特點(diǎn)是沒有返回值。
pool-1-thread-5:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-2:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-4:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-4:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-4:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-1:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-4:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-3:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-2:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池 pool-1-thread-5:實(shí)現(xiàn)線程的方式Runnable接口,但運(yùn)行方式不一樣,使用線程池
從上面的結(jié)果中可以看出,線程池中包含五個(gè)線程。線程運(yùn)行完成之后并不銷毀,而是還回到線程池,下一次執(zhí)行時(shí)從線程池中獲取線程資源再次運(yùn)行。
第五種:線程池 + submit
下面的例子線程池ExecutorService使用submit方法運(yùn)行Callable接口call方法的線程實(shí)現(xiàn),submit方法與call方法的共同特點(diǎn)是存在返回值。
- Callable接口call方法的返回值可以由泛型定義
- ExecutorService線程池submit方法的返回值是Future
Future的get方法可以獲取call方法的返回值,同時(shí)如果call方法拋出異常,F(xiàn)uture的get方法也會拋出異常。
public class FifthWay implements Callable<String> {
@Override
public String call() throws Exception {
return Thread.currentThread().getName() + ":Callable接口帶返回值,可以拋出異常";
}
//模擬測試
public static void main(String[] args) throws ExecutionException, InterruptedException {
//保存多線程執(zhí)行結(jié)果
List<String> retList = new ArrayList<>();
//創(chuàng)建一個(gè)固定大小的線程池
ExecutorService threadPool = Executors.newFixedThreadPool(5);
for(int i = 0;i < 10;i++){
Future<String> future = threadPool.submit(new FifthWay());
retList.add(future.get());
}
//java8 語法,打印retlist
retList.forEach(System.out::println);
}
}
上文代碼中有一個(gè)小小的語法糖,retList.forEach(System.out::println);是java8提供的方法引用
pool-1-thread-1:Callable接口帶返回值,可以拋出異常 pool-1-thread-2:Callable接口帶返回值,可以拋出異常 pool-1-thread-3:Callable接口帶返回值,可以拋出異常 pool-1-thread-4:Callable接口帶返回值,可以拋出異常 pool-1-thread-5:Callable接口帶返回值,可以拋出異常 pool-1-thread-1:Callable接口帶返回值,可以拋出異常 pool-1-thread-2:Callable接口帶返回值,可以拋出異常 pool-1-thread-3:Callable接口帶返回值,可以拋出異常 pool-1-thread-4:Callable接口帶返回值,可以拋出異常 pool-1-thread-5:Callable接口帶返回值,可以拋出異常
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
分析xxljob登入功能集成OIDC的統(tǒng)一認(rèn)證
這篇文章主要為大家介紹分析xxljob登入功能集成OIDC的統(tǒng)一認(rèn)證的詳解說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
SpringMVC的簡單傳值(實(shí)現(xiàn)代碼)
下面小編就為大家?guī)硪黄猄pringMVC的簡單傳值(實(shí)現(xiàn)代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
一文帶你掌握J(rèn)ava8中函數(shù)式接口的使用和自定義
函數(shù)式接口是?Java?8?引入的一種接口,用于支持函數(shù)式編程,下面我們就來深入探討函數(shù)式接口的概念、用途以及如何創(chuàng)建和使用函數(shù)式接口吧2023-08-08
play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面
這篇文章主要介紹了play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面的相關(guān)資料,需要的朋友可以參考下2016-11-11
Java中實(shí)現(xiàn)文件預(yù)覽的功能(實(shí)例代碼)
大家都知道word,Excel,PPT實(shí)現(xiàn)在線預(yù)覽常用的方式就是先轉(zhuǎn)換成pdf,然后在進(jìn)行預(yù)覽,下面給大家介紹Java中如何實(shí)現(xiàn)文件預(yù)覽的功能,需要的朋友可以參考下2023-05-05
Java利用for循環(huán)輸出空心菱形的實(shí)例代碼
這篇文章主要介紹了Java利用for循環(huán)輸出空心菱形的實(shí)例代碼,需要的朋友可以參考下2014-02-02

