JavaScript的Promise.all()
Promise是JavaScript異步編程的一種解決方案,在ES6中引入。
通過Promise.all()可以實現(xiàn)對一組異步請求的統(tǒng)一處理,等待所有異步執(zhí)行完成之后調(diào)用回調(diào)函數(shù)。
其實,這種并發(fā)執(zhí)行同步等待的需求在Java并發(fā)編程中也很常見,那么,是否可以通過Java也來實現(xiàn)這樣一個Promise類呢?
使用Java實現(xiàn)Promise.all()
使用工具
CountDownLatch:Java并發(fā)工具包中有CountDownLatch類允許一個或多個線程等待其他線程的一系列操作完成。
ThreadPoolExecutor:通過線程池實現(xiàn)多線程的并發(fā)執(zhí)行
實現(xiàn)
public class Promise { private static ExecutorService executorService = Executors.newScheduledThreadPool(16); private Promise() { throw new AssertionError(); } /** * 實現(xiàn)并發(fā)同時地對某個action并發(fā)執(zhí)行并返回執(zhí)行結(jié)果 * 實現(xiàn)思路: * 并發(fā)創(chuàng)建所有執(zhí)行的線程,并通過鎖(start)阻塞等待著 * 在創(chuàng)建所有執(zhí)行的線程后(ready)開始計時,并解鎖然所有的線程啟動 * 通過另外一個鎖(done)記錄執(zhí)行完的線程 * 主線程只需關心3點 * - 所有線程是否準備好 * - 準備好的話開始計時并解鎖開始執(zhí)行 * - 等待執(zhí)行完畢 * * @param callableList 要并發(fā)執(zhí)行的列表 * @return list 執(zhí)行結(jié)果,list.item為null的話表示執(zhí)行異常 * @throws InterruptedException 異常 */ public static <T> List<T> all(final List<Callable<T>> callableList) throws InterruptedException { final List<T> result = new ArrayList<>(); int length = callableList.size(); final CountDownLatch ready = new CountDownLatch(length); final CountDownLatch start = new CountDownLatch(1); final CountDownLatch done = new CountDownLatch(length); for (final Callable<T> callable : callableList) { executorService.execute(new Runnable() { @Override public void run() { ready.countDown(); try { start.await(); T t = callable.call(); result.add(t); } catch (Exception e) { // interrupt when exception Thread.currentThread().interrupt(); // set null mean exception result.add(null); e.printStackTrace(); } finally { done.countDown(); } } }); } ready.await(); long startnano = System.nanoTime(); start.countDown(); done.await(); long cause = System.nanoTime() - startnano; System.out.println(String.format("Promise all done,cause time millSecond: %s", cause / 1000000)); return result; }}
效果
測試
public void promiseAllTest() throws Exception{ List<Callable<String>> callables = new ArrayList<>(); for (int i = 0; i < 10; i++) { int finalI = i; callables.add(new Callable<String>() { @Override public String call() throws Exception { int millis = new Random().nextInt(10000); Thread.sleep(millis); System.out.println(String.format("thread%s sleep %s millis" ,finalI,millis)); return "Thread" + finalI; } }); } List<String> result = Promise.all(callables); System.out.println(result); System.out.println("done...");}
測試結(jié)果
thread1 sleep 732 millis
thread2 sleep 758 millis
thread7 sleep 976 millis
thread8 sleep 1397 millis
thread5 sleep 1513 millis
thread0 sleep 2221 millis
thread3 sleep 4885 millis
thread6 sleep 5221 millis
thread4 sleep 7101 millis
thread9 sleep 7634 millis
Promise all done,cause time millSecond: 7638
[Thread1, Thread2, Thread7, Thread8, Thread5, Thread0, Thread3, Thread6, Thread4, Thread9]
done...
總結(jié)
本文只是通過原生Java實現(xiàn)簡單版本的Promise.all(),可用于簡單的并發(fā)編程,但是對于實際高并發(fā)應用還需要優(yōu)化,如對線程池的優(yōu)化,還有中斷的處理等。
參考
《Effective Java》第二版第十章第69條:并發(fā)工具優(yōu)先于wait和notify
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點
疑難解答
圖片精選