一文搞懂Java創(chuàng)建線程的五種方法
題目描述
Java創(chuàng)建線程的幾種方式
Java使用Thread類代表線程,所有線程對象都必須是Thread類或者其子類的實例。Java可以用以下5種方式來創(chuàng)建線程
- 繼承Thread類創(chuàng)建線程;
- 實現(xiàn)Runnable接口創(chuàng)建線程;
- 實現(xiàn)Callable接口,通過FutureTask包裝器來創(chuàng)建Thread線程;
- 使用ExecutorService、Callable(或者Runnable)、Future實現(xiàn)由返回結果的線程。
- 使用CompletableFuture類創(chuàng)建異步線程,且是據(jù)有返回結果的線程。 JDK8新支持的
實現(xiàn):使用這5種方式創(chuàng)建線程,體驗其中的妙處。
解題思路
繼承Thread類創(chuàng)建線程
Thread類本質上是實現(xiàn)了Runnable接口的一個實例,代表一個線程的實例。啟動線程的唯一方法就是通過Thread類的start()實例方法。start()方法是一個native方法,它將啟動一個新線程,并執(zhí)行run()方法。這種方式實現(xiàn)多線程很簡單,通過自己的類直接extends Thread,并復寫run()方法,就可以啟動新線程并執(zhí)行自己定義的run()方法。
實現(xiàn)Runnable接口創(chuàng)建線程
如果自己的類已經extends另一個類,就無法直接extends Thread,此時,可以實現(xiàn)一個Runnable接口
實現(xiàn)Callable接口,通過FutureTask包裝器來創(chuàng)建Thread線程
實現(xiàn)一個Callable接口(它是一個具有返回值的)
使用ExecutorService、Callable(或者Runnable)、Future實現(xiàn)由返回結果的線程
Executors類,提供了一系列工廠方法用于創(chuàng)建線程池,返回的線程池都實現(xiàn)了ExecutorService接口:
Executors類,提供了一系列工廠方法用于創(chuàng)建線程池,返回的線程池都實現(xiàn)了ExecutorService接口:
//創(chuàng)建固定數(shù)目線程的線程池。 public static ExecutorService newFixedThreadPool(int nThreads) ; //創(chuàng)建一個可緩存的線程池,調用execute 將重用以前構造的線程(如果線程可用)。如果現(xiàn)有線程沒有可用的,則創(chuàng)建一個新線程并添加到池中。終止并從緩存中移除那些已有 60 秒鐘未被使用的線程。 public static ExecutorService newCachedThreadPool(); //創(chuàng)建一個單線程化的Executor。 public static ExecutorService newSingleThreadExecutor(); //創(chuàng)建一個支持定時及周期性的任務執(zhí)行的線程池,多數(shù)情況下可用來替代Timer類。 public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize);
ExecutoreService提供了submit()方法,傳遞一個Callable,或Runnable,返回Future。如果Executor后臺線程池還沒有完成Callable的計算,這調用返回Future對象的get()方法,會阻塞直到計算完成。
使用CompletableFuture類創(chuàng)建異步線程,且是據(jù)有返回結果的線程
Future模式的缺點
Future雖然可以實現(xiàn)獲取異步執(zhí)行結果的需求,但是它沒有提供通知的機制,我們無法得知Future什么時候完成。
要么使用阻塞,在future.get()的地方等待future返回的結果,這時又變成同步操作。要么使用isDone()輪詢地判斷Future是否完成,這樣會耗費CPU的資源。
CompletableFuture 介紹
JDK1.8新加入的一個實現(xiàn)類CompletableFuture,實現(xiàn)了Future, CompletionStage兩個接口。
CompletableFuture中4個異步執(zhí)行任務靜態(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用于有返回值的任務,runAsync則用于沒有返回值的任務。Executor參數(shù)可以手動指定線程池,否則默認ForkJoinPool.commonPool()系統(tǒng)級公共線程池
代碼詳解
第一種 繼承Thread類創(chuàng)建線程
package cn.xiaoxuzhu.daily;
import java.util.concurrent.CountDownLatch;
/**
* Description:繼承Thread類創(chuàng)建線程
*
* @author 小王同學
* @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();
}
}
}

第二種:實現(xiàn)Runnable接口創(chuàng)建線程
package cn.xiaoxuzhu.daily;
import java.util.concurrent.CountDownLatch;
/**
* Description: 實現(xiàn)Runnable接口創(chuàng)建線程
*
* @author 小王同學
* @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();
}
}
}

第三種:實現(xiàn)Callable接口,通過FutureTask包裝器來創(chuàng)建Thread線程
計算1~100的疊加
package cn.xiaoxuzhu.daily;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Description: 實現(xiàn)Callable接口,通過FutureTask包裝器來創(chuàng)建Thread線程
* 跟Runnable比,不同點在于它是一個具有返回值的,且會拋出異常
* //用futureTask接收結果
*
* @author 小王同學
* @version 1.0
*/
public class ThreadDemo3 implements Callable<Integer> {
public static void main(String[] args) {
ThreadDemo3 threadDemo03 = new ThreadDemo3();
//1、用futureTask接收結果
FutureTask<Integer> futureTask = new FutureTask<>(threadDemo03);
new Thread(futureTask).start();
//2、接收線程運算后的結果
try {
//futureTask.get();這個是堵塞性的等待
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實現(xiàn)返回結果的線程
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實現(xiàn)由返回結果的線程
*
* @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í)行結果:"+sum);
} finally {
countDownLatch.countDown();
}
return sum;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 第四種:使用使用線程池方式
// 接受返回參數(shù)
List<Future> resultItems2 = new ArrayList<Future>();
// 給線程池初始化5個線程
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);
}
// 等待線程池中分配的任務完成后才關閉(關閉之后不允許有新的線程加入,但是它并不會等待線程結束),
// 而executorService.shutdownNow();是立即關閉不管是否線程池中是否有其他未完成的線程。
executorService.shutdown();
try {
countDownLatch4.await();
Iterator<Future> iterator = resultItems2.iterator();
System.out.println("----------------------");
while (iterator.hasNext()) {
try {
System.out.println("線程返回結果:"+iterator.next().get());
} catch (ExecutionException e) {
e.printStackTrace();
}
}
System.out.println("callable complete...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

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


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

