一文搞懂Java創(chuàng)建線程的五種方法
題目描述
Java創(chuàng)建線程的幾種方式
Java使用Thread類代表線程,所有線程對(duì)象都必須是Thread類或者其子類的實(shí)例。Java可以用以下5種方式來(lái)創(chuàng)建線程
- 繼承Thread類創(chuàng)建線程;
- 實(shí)現(xiàn)Runnable接口創(chuàng)建線程;
- 實(shí)現(xiàn)Callable接口,通過FutureTask包裝器來(lái)創(chuàng)建Thread線程;
- 使用ExecutorService、Callable(或者Runnable)、Future實(shí)現(xiàn)由返回結(jié)果的線程。
- 使用CompletableFuture類創(chuàng)建異步線程,且是據(jù)有返回結(jié)果的線程。 JDK8新支持的
實(shí)現(xiàn):使用這5種方式創(chuàng)建線程,體驗(yàn)其中的妙處。
解題思路
繼承Thread類創(chuàng)建線程
Thread類本質(zhì)上是實(shí)現(xiàn)了Runnable接口的一個(gè)實(shí)例,代表一個(gè)線程的實(shí)例。啟動(dòng)線程的唯一方法就是通過Thread類的start()實(shí)例方法。start()方法是一個(gè)native方法,它將啟動(dòng)一個(gè)新線程,并執(zhí)行run()方法。這種方式實(shí)現(xiàn)多線程很簡(jiǎn)單,通過自己的類直接extends Thread,并復(fù)寫run()方法,就可以啟動(dòng)新線程并執(zhí)行自己定義的run()方法。
實(shí)現(xiàn)Runnable接口創(chuàng)建線程
如果自己的類已經(jīng)extends另一個(gè)類,就無(wú)法直接extends Thread,此時(shí),可以實(shí)現(xiàn)一個(gè)Runnable接口
實(shí)現(xiàn)Callable接口,通過FutureTask包裝器來(lái)創(chuàng)建Thread線程
實(shí)現(xiàn)一個(gè)Callable接口(它是一個(gè)具有返回值的)
使用ExecutorService、Callable(或者Runnable)、Future實(shí)現(xiàn)由返回結(jié)果的線程
Executors類,提供了一系列工廠方法用于創(chuàng)建線程池,返回的線程池都實(shí)現(xiàn)了ExecutorService接口:
Executors類,提供了一系列工廠方法用于創(chuàng)建線程池,返回的線程池都實(shí)現(xiàn)了ExecutorService接口:
//創(chuàng)建固定數(shù)目線程的線程池。 public static ExecutorService newFixedThreadPool(int nThreads) ; //創(chuàng)建一個(gè)可緩存的線程池,調(diào)用execute 將重用以前構(gòu)造的線程(如果線程可用)。如果現(xiàn)有線程沒有可用的,則創(chuàng)建一個(gè)新線程并添加到池中。終止并從緩存中移除那些已有 60 秒鐘未被使用的線程。 public static ExecutorService newCachedThreadPool(); //創(chuàng)建一個(gè)單線程化的Executor。 public static ExecutorService newSingleThreadExecutor(); //創(chuàng)建一個(gè)支持定時(shí)及周期性的任務(wù)執(zhí)行的線程池,多數(shù)情況下可用來(lái)替代Timer類。 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize);
ExecutoreService提供了submit()方法,傳遞一個(gè)Callable,或Runnable,返回Future。如果Executor后臺(tái)線程池還沒有完成Callable的計(jì)算,這調(diào)用返回Future對(duì)象的get()方法,會(huì)阻塞直到計(jì)算完成。
使用CompletableFuture類創(chuàng)建異步線程,且是據(jù)有返回結(jié)果的線程
Future模式的缺點(diǎn)
Future雖然可以實(shí)現(xiàn)獲取異步執(zhí)行結(jié)果的需求,但是它沒有提供通知的機(jī)制,我們無(wú)法得知Future什么時(shí)候完成。
要么使用阻塞,在future.get()的地方等待future返回的結(jié)果,這時(shí)又變成同步操作。要么使用isDone()輪詢地判斷Future是否完成,這樣會(huì)耗費(fèi)CPU的資源。
CompletableFuture 介紹
JDK1.8新加入的一個(gè)實(shí)現(xiàn)類CompletableFuture,實(shí)現(xiàn)了Future, CompletionStage兩個(gè)接口。
CompletableFuture中4個(gè)異步執(zhí)行任務(wù)靜態(tài)方法:
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
return asyncSupplyStage(asyncPool, supplier);
}
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor) {
return asyncSupplyStage(screenExecutor(executor), supplier);
}
public static CompletableFuture<Void> runAsync(Runnable runnable) {
return asyncRunStage(asyncPool, runnable);
}
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) {
return asyncRunStage(screenExecutor(executor), runnable);
}
其中supplyAsync用于有返回值的任務(wù),runAsync則用于沒有返回值的任務(wù)。Executor參數(shù)可以手動(dòng)指定線程池,否則默認(rèn)ForkJoinPool.commonPool()系統(tǒng)級(jí)公共線程池
代碼詳解
第一種 繼承Thread類創(chuàng)建線程
package cn.xiaoxuzhu.daily;
import java.util.concurrent.CountDownLatch;
/**
* Description:繼承Thread類創(chuàng)建線程
*
* @author 小王同學(xué)
* @version 1.0
*/
public class ThreadDemo1 extends Thread {
CountDownLatch countDownLatch;
public ThreadDemo1(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + ":my thread ");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}
public static void main(String[] args) {
// 第一種:使用extends Thread方式
CountDownLatch countDownLatch1 = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
ThreadDemo1 myThread1 = new ThreadDemo1(countDownLatch1);
myThread1.start();
}
try {
countDownLatch1.await();
System.out.println("thread complete...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

第二種:實(shí)現(xiàn)Runnable接口創(chuàng)建線程
package cn.xiaoxuzhu.daily;
import java.util.concurrent.CountDownLatch;
/**
* Description: 實(shí)現(xiàn)Runnable接口創(chuàng)建線程
*
* @author 小王同學(xué)
* @version 1.0
*/
public class ThreadDemo2 implements Runnable{
CountDownLatch countDownLatch;
public ThreadDemo2(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + ":my runnable ");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}
public static void main(String[] args) {
// 第二種:使用implements Runnable方式
CountDownLatch countDownLatch2 = new CountDownLatch(2);
ThreadDemo2 myRunnable = new ThreadDemo2(countDownLatch2);
for (int i = 0; i < 2; i++) {
new Thread(myRunnable).start();
}
try {
countDownLatch2.await();
System.out.println("runnable complete...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

第三種:實(shí)現(xiàn)Callable接口,通過FutureTask包裝器來(lái)創(chuàng)建Thread線程
計(jì)算1~100的疊加
package cn.xiaoxuzhu.daily;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Description: 實(shí)現(xiàn)Callable接口,通過FutureTask包裝器來(lái)創(chuàng)建Thread線程
* 跟Runnable比,不同點(diǎn)在于它是一個(gè)具有返回值的,且會(huì)拋出異常
* //用futureTask接收結(jié)果
*
* @author 小王同學(xué)
* @version 1.0
*/
public class ThreadDemo3 implements Callable<Integer> {
public static void main(String[] args) {
ThreadDemo3 threadDemo03 = new ThreadDemo3();
//1、用futureTask接收結(jié)果
FutureTask<Integer> futureTask = new FutureTask<>(threadDemo03);
new Thread(futureTask).start();
//2、接收線程運(yùn)算后的結(jié)果
try {
//futureTask.get();這個(gè)是堵塞性的等待
Integer sum = futureTask.get();
System.out.println("sum="+sum);
System.out.println("-------------------");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i <101 ; i++) {
sum+=i;
}
return sum;
}
}

第四種:使用ExecutorService、Callable(或者Runnable)、Future實(shí)現(xiàn)返回結(jié)果的線程
package cn.xiaoxuzhu.daily;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Description: 使用ExecutorService、Callable(或者Runnable)、Future實(shí)現(xiàn)由返回結(jié)果的線程
*
* @author xiaoxuzhu
* @version 1.0
*/
public class ThreadDemo4 {
static class MyCallable implements Callable<Integer> {
private CountDownLatch countDownLatch;
public MyCallable(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
public Integer call() {
int sum = 0;
try {
for (int i = 0; i <= 100; i++) {
sum += i;
}
System.out.println("線程執(zhí)行結(jié)果:"+sum);
} finally {
countDownLatch.countDown();
}
return sum;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 第四種:使用使用線程池方式
// 接受返回參數(shù)
List<Future> resultItems2 = new ArrayList<Future>();
// 給線程池初始化5個(gè)線程
ExecutorService executorService = Executors.newFixedThreadPool(5);
CountDownLatch countDownLatch4 = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
MyCallable myCallable = new MyCallable(countDownLatch4);
Future result = executorService.submit(myCallable);
resultItems2.add(result);
}
// 等待線程池中分配的任務(wù)完成后才關(guān)閉(關(guān)閉之后不允許有新的線程加入,但是它并不會(huì)等待線程結(jié)束),
// 而executorService.shutdownNow();是立即關(guān)閉不管是否線程池中是否有其他未完成的線程。
executorService.shutdown();
try {
countDownLatch4.await();
Iterator<Future> iterator = resultItems2.iterator();
System.out.println("----------------------");
while (iterator.hasNext()) {
try {
System.out.println("線程返回結(jié)果:"+iterator.next().get());
} catch (ExecutionException e) {
e.printStackTrace();
}
}
System.out.println("callable complete...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

第五種:使用ComletetableFuture類創(chuàng)建異步線程,且是據(jù)有返回結(jié)果的線程
package cn.xiaoxuzhu.daily;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
/**
* Description: 使用CompletableFuture類創(chuàng)建異步線程,且是據(jù)有返回結(jié)果的線程。
*
* @author xiaoxuzhu
* @version 1.0
*/
public class ThreadDemo5 {
/**
* A任務(wù)B任務(wù)完成后,才執(zhí)行C任務(wù)
* 返回值的處理
* @param
*@return void
**/
@Test
public void completableFuture1(){
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("future1 finished!");
return "future1 finished!";
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("future2 finished!");
return "future2 finished!";
});
CompletableFuture<Void> future3 = CompletableFuture.allOf(future1, future2);
try {
future3.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("future1: " + future1.isDone() + " future2: " + future2.isDone());
}
/**
* 在Java8中,CompletableFuture提供了非常強(qiáng)大的Future的擴(kuò)展功能,可以幫助我們簡(jiǎn)化異步編程的復(fù)雜性,
* 并且提供了函數(shù)式編程的能力,可以通過回調(diào)的方式處理計(jì)算結(jié)果,也提供了轉(zhuǎn)換和組合 CompletableFuture 的方法
*
* 注意: 方法中有Async一般表示另起一個(gè)線程,沒有表示用當(dāng)前線程
*/
@Test
public void test01() throws Exception {
ExecutorService service = Executors.newFixedThreadPool(5);
/**
* supplyAsync用于有返回值的任務(wù),
* runAsync則用于沒有返回值的任務(wù)
* Executor參數(shù)可以手動(dòng)指定線程池,否則默認(rèn)ForkJoinPool.commonPool()系統(tǒng)級(jí)公共線程池
*/
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "xiaoxuzhu";
}, service);
CompletableFuture<Void> data = CompletableFuture.runAsync(() -> System.out.println("xiaoxuzhu"));
/**
* 計(jì)算結(jié)果完成回調(diào)
*/
future.whenComplete((x,y)-> System.out.println("有延遲3秒:執(zhí)行當(dāng)前任務(wù)的線程繼續(xù)執(zhí)行:"+x+","+y)); //執(zhí)行當(dāng)前任務(wù)的線程繼續(xù)執(zhí)行
data.whenCompleteAsync((x,y)-> System.out.println("交給線程池另起線程執(zhí)行:"+x+","+y)); // 交給線程池另起線程執(zhí)行
future.exceptionally(Throwable::toString);
//System.out.println(future.get());
/**
* thenApply,一個(gè)線程依賴另一個(gè)線程可以使用,出現(xiàn)異常不執(zhí)行
*/
//第二個(gè)線程依賴第一個(gè)的結(jié)果
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 5).thenApply(x -> x);
/**
* handle 是執(zhí)行任務(wù)完成時(shí)對(duì)結(jié)果的處理,第一個(gè)出現(xiàn)異常繼續(xù)執(zhí)行
*/
CompletableFuture<Integer> future2 = future1.handleAsync((x, y) -> x + 2);
System.out.println(future2.get());//7
/**
* thenAccept 消費(fèi)處理結(jié)果,不返回
*/
future2.thenAccept(System.out::println);
/**
* thenRun 不關(guān)心任務(wù)的處理結(jié)果。只要上面的任務(wù)執(zhí)行完成,就開始執(zhí)行
*/
future2.thenRunAsync(()-> System.out.println("繼續(xù)下一個(gè)任務(wù)"));
/**
* thenCombine 會(huì)把 兩個(gè) CompletionStage 的任務(wù)都執(zhí)行完成后,兩個(gè)任務(wù)的結(jié)果交給 thenCombine 來(lái)處理
*/
CompletableFuture<Integer> future3 = future1.thenCombine(future2, Integer::sum);
System.out.println(future3.get()); // 5+7=12
/**
* thenAcceptBoth : 當(dāng)兩個(gè)CompletionStage都執(zhí)行完成后,把結(jié)果一塊交給thenAcceptBoth來(lái)進(jìn)行消耗
*/
future1.thenAcceptBothAsync(future2,(x,y)-> System.out.println(x+","+y)); //5,7
/**
* applyToEither
* 兩個(gè)CompletionStage,誰(shuí)執(zhí)行返回的結(jié)果快,我就用那個(gè)CompletionStage的結(jié)果進(jìn)行下一步的轉(zhuǎn)化操作
*/
CompletableFuture<Integer> future4 = future1.applyToEither(future2, x -> x);
System.out.println(future4.get()); //5
/**
* acceptEither
* 兩個(gè)CompletionStage,誰(shuí)執(zhí)行返回的結(jié)果快,我就用那個(gè)CompletionStage的結(jié)果進(jìn)行下一步的消耗操作
*/
future1.acceptEither(future2, System.out::println);
/**
* runAfterEither
* 兩個(gè)CompletionStage,任何一個(gè)完成了都會(huì)執(zhí)行下一步的操作(Runnable
*/
future1.runAfterEither(future,()-> System.out.println("有一個(gè)完成了,我繼續(xù)"));
/**
* runAfterBoth
* 兩個(gè)CompletionStage,都完成了計(jì)算才會(huì)執(zhí)行下一步的操作(Runnable)
*/
future1.runAfterBoth(future,()-> System.out.println("都完成了,我繼續(xù)"));
/**
* thenCompose 方法
* thenCompose 方法允許你對(duì)多個(gè) CompletionStage 進(jìn)行流水線操作,第一個(gè)操作完成時(shí),將其結(jié)果作為參數(shù)傳遞給第二個(gè)操作
* thenApply是接受一個(gè)函數(shù),thenCompose是接受一個(gè)future實(shí)例,更適合處理流操作
*/
future1.thenComposeAsync(x->CompletableFuture.supplyAsync(()->x+1))
.thenComposeAsync(x->CompletableFuture.supplyAsync(()->x+2))
.thenCompose(x->CompletableFuture.runAsync(()-> System.out.println("流操作結(jié)果:"+x)));
TimeUnit.SECONDS.sleep(5);//主線程sleep,等待其他線程執(zhí)行
}
}


以上就是一文搞懂Java創(chuàng)建線程的五種方法的詳細(xì)內(nèi)容,更多關(guān)于Java創(chuàng)建線程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java OCR tesseract 圖像智能文字字符識(shí)別技術(shù)實(shí)例代碼
這篇文章主要介紹了Java OCR tesseract 圖像智能文字字符識(shí)別技術(shù)實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06
SpringBoot自定義注解之實(shí)現(xiàn)AOP切面日志詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot自定義注解之實(shí)現(xiàn)AOP切面統(tǒng)一打印出入?yún)⑷罩?,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
windows系統(tǒng)配置Java開發(fā)環(huán)境變量
這篇文章主要介紹了windows系統(tǒng)配置Java開發(fā)環(huán)境變量,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2021-12-12
深入淺析Mybatis與Hibernate的區(qū)別與用途
這篇文章主要介紹了Mybatis與Hibernate的區(qū)別與用途的相關(guān)資料,需要的朋友可以參考下2017-10-10
JAVA中的函數(shù)式接口Function和BiFunction詳解
這篇文章主要介紹了JAVA中的函數(shù)式接口Function和BiFunction詳解,JDK的函數(shù)式接口都加上了@FunctionalInterface注解進(jìn)行標(biāo)識(shí),但是無(wú)論是否加上該注解只要接口中只有一個(gè)抽象方法,都是函數(shù)式接口,需要的朋友可以參考下2024-01-01

