Java ExecutorServic線程池異步實現(xiàn)流程
相信大家都在項目中遇到過這樣的情況,前臺需要快速的顯示,后臺還需要做一個很大的邏輯。比如:前臺點擊數(shù)據(jù)導入按鈕,按鈕后的服務端執(zhí)行邏輯A,和邏輯B(執(zhí)行大量的表數(shù)據(jù)之間的copy功能),而這時前臺不能一直等著,要返回給前臺,告訴正在處理中就行了。這里就需要用到異步了。
點擊按鈕 -> 邏輯A ->邏輯B(異步) -> 方法結(jié)束。
到底,項目需求明確了,就引入了ExecutorServic線程池。
Java通過Executors提供四種線程池,分別為:
- newCachedThreadPool創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
- newFixedThreadPool 創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會在隊列中等待。
- newScheduledThreadPool 創(chuàng)建一個定長線程池,支持定時及周期性任務執(zhí)行。
- newSingleThreadExecutor 創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務,保證所有任務按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author szy
* @version 創(chuàng)建時間:2018-5-20 上午10:25:06
*
*/
public class Testasync {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(task0() == true){
System.out.println("執(zhí)行完畢,看異步結(jié)果");
}
}
public static void task1(){
System.out.println("task1 is start");
}
public static void task2(){
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(new Callable(){
@Override
public Object call() throws Exception {
// TODO Auto-generated method stub
//增加睡眠時間,便于查看結(jié)果
/* try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
//異步提交
int sum = 0;
for (int i = 0; i < 10000; i++) {
sum += i;
}
System.out.println("task2執(zhí)行數(shù)據(jù)的大量導入或者導出");
System.out.println("task2="+sum);
System.out.println("task2導入或者導出完成");
return null;
}
});
}
public static void task3(){
System.out.println("task3 is start");
int j = 0;
while(true) {
if(j++ > 10) {
break;
}
System.out.println("------------task3 end-----------");
}
}
public static boolean task0(){
task1();
task2();
task3();
return true;
}
}
然后看結(jié)果:
task1 is start task3 is start ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- ------------task3 end----------- task2執(zhí)行數(shù)據(jù)的大量導入或者導出 執(zhí)行完畢,看異步結(jié)果 task2=49995000 task2導入或者導出完成
可以看出,task1 和task3先執(zhí)行了,并且方法在沒有等待task2的情況下,直接結(jié)束了。
異步的task2另開了一個線程,自己在執(zhí)行。和主線程已經(jīng)無關了。
不過,這種在eclipse中以deubug模式是看不出來的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringMVC @RequestBody Date類型的Json轉(zhuǎn)換方式
這篇文章主要介紹了SpringMVC @RequestBody Date類型的Json轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringCloud Webflux過濾器增加header傳遞方式
這篇文章主要介紹了SpringCloud Webflux過濾器增加header傳遞方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Springboot?hibernate-validator?6.x快速校驗示例代碼
這篇文章主要介紹了Springboot?hibernate-validator?6.x校驗,本文以6.2.1.Final版本為例解決了log4j版本的漏洞問題,通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-12-12
解決@JsonInclude(JsonInclude.Include.NON_NULL)不起作用問題
這篇文章主要介紹了解決@JsonInclude(JsonInclude.Include.NON_NULL)不起作用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring Cloud分布式定時器之ShedLock的實現(xiàn)
這篇文章主要介紹了Spring Cloud分布式定時器之ShedLock的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

