Java ExecutorServic線(xiàn)程池異步實(shí)現(xiàn)流程
相信大家都在項(xiàng)目中遇到過(guò)這樣的情況,前臺(tái)需要快速的顯示,后臺(tái)還需要做一個(gè)很大的邏輯。比如:前臺(tái)點(diǎn)擊數(shù)據(jù)導(dǎo)入按鈕,按鈕后的服務(wù)端執(zhí)行邏輯A,和邏輯B(執(zhí)行大量的表數(shù)據(jù)之間的copy功能),而這時(shí)前臺(tái)不能一直等著,要返回給前臺(tái),告訴正在處理中就行了。這里就需要用到異步了。
點(diǎn)擊按鈕 -> 邏輯A ->邏輯B(異步) -> 方法結(jié)束。
到底,項(xiàng)目需求明確了,就引入了ExecutorServic線(xiàn)程池。
Java通過(guò)Executors提供四種線(xiàn)程池,分別為:
newCachedThreadPool創(chuàng)建一個(gè)可緩存線(xiàn)程池,如果線(xiàn)程池長(zhǎng)度超過(guò)處理需要,可靈活回收空閑線(xiàn)程,若無(wú)可回收,則新建線(xiàn)程。 newFixedThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線(xiàn)程池,可控制線(xiàn)程最大并發(fā)數(shù),超出的線(xiàn)程會(huì)在隊(duì)列中等待。 newScheduledThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線(xiàn)程池,支持定時(shí)及周期性任務(wù)執(zhí)行。 newSingleThreadExecutor 創(chuàng)建一個(gè)單線(xiàn)程化的線(xiàn)程池,它只會(huì)用唯一的工作線(xiàn)程來(lái)執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行。import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @author szy * @version 創(chuàng)建時(shí)間: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//增加睡眠時(shí)間,便于查看結(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ù)的大量導(dǎo)入或者導(dǎo)出');System.out.println('task2='+sum);System.out.println('task2導(dǎo)入或者導(dǎo)出完成');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 starttask3 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ù)的大量導(dǎo)入或者導(dǎo)出執(zhí)行完畢,看異步結(jié)果task2=49995000task2導(dǎo)入或者導(dǎo)出完成
可以看出,task1 和task3先執(zhí)行了,并且方法在沒(méi)有等待task2的情況下,直接結(jié)束了。
異步的task2另開(kāi)了一個(gè)線(xiàn)程,自己在執(zhí)行。和主線(xiàn)程已經(jīng)無(wú)關(guān)了。
不過(guò),這種在eclipse中以deubug模式是看不出來(lái)的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. 得到XML文檔大小的方法3. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法4. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))5. JavaScrip簡(jiǎn)單數(shù)據(jù)類(lèi)型隱式轉(zhuǎn)換的實(shí)現(xiàn)6. ASP常用日期格式化函數(shù) FormatDate()7. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式8. XML入門(mén)的常見(jiàn)問(wèn)題(二)9. 如何在jsp界面中插入圖片10. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
