String dispatcherName ="parallelTaskDispatcher";TaskDispatcher dispatcher =createParallelTaskDispatcher(dispatcherName,TaskPriority.DEFAULT);// 創建任務組。Group group = dispatcher.createDispatchGroup();// 將任務1加入任務組,返回一個用于取消任務的接口。dispatcher.asyncGroupDispatch(group,newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"download task1 is running");}});// 將與任務1相關聯的任務2加入任務組。dispatcher.asyncGroupDispatch(group,newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"download task2 is running");}});// 在任務組中的所有任務執行完成后執行指定任務。dispatcher.groupDispatchNotify(group,newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"the close task is running after all tasks in the group are completed");}});?// 可能的執行結果:// download task1 is running// download task2 is running// the close task is running after all tasks in the group are completed// 另外一種可能的執行結果:// download task2 is running// download task1 is running// the close task is running after all tasks in the group are completed
String dispatcherName ="parallelTaskDispatcher";TaskDispatcher dispatcher =createParallelTaskDispatcher(dispatcherName,TaskPriority.DEFAULT);// 創建任務組。Group group = dispatcher.createDispatchGroup();// 將任務加入任務組,返回一個用于取消任務的接口。dispatcher.asyncGroupDispatch(group,newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"task1 is running");// 1}});dispatcher.asyncGroupDispatch(group,newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"task2 is running");// 2}});dispatcher.syncDispatchBarrier(newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"barrier");// 3}});HiLog.info(LABEL_LOG,"after syncDispatchBarrier");// 4?// 1和2的執行順序不定;3和4總是在1和2之后按順序執行。// 可能的執行結果:// task1 is running// task2 is running// barrier// after syncDispatchBarrier// 另外一種執行結果:// task2 is running// task1 is running// barrier// after syncDispatchBarrier
TaskDispatcher dispatcher =createParallelTaskDispatcher("dispatcherName",TaskPriority.DEFAULT);// 創建任務組Group group = dispatcher.createDispatchGroup();// 將任務加入任務組,返回一個用于取消任務的接口dispatcher.asyncGroupDispatch(group,newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"task1 is running");// 1}});dispatcher.asyncGroupDispatch(group,newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"task2 is running");// 2}});dispatcher.asyncDispatchBarrier(newRunnable(){@Overridepublicvoidrun(){HiLog.info(LABEL_LOG,"barrier");// 3}});HiLog.info(LABEL_LOG,"after asyncDispatchBarrier");// 4?// 1和2的執行順序不定,但總在3之前執行;4不需要等待1、2、3執行完成// 可能的執行結果:// task1 is running// task2 is running// after asyncDispatchBarrier// barrier
⑧ applyDispatch
執行多次任務:對指定任務執行多次。
如下代碼示例展示了如何執行多次任務:
finalint total =10;finalCountDownLatch latch =newCountDownLatch(total);finalList<Long> indexList =newArrayList<>(total);TaskDispatcher dispatcher =getGlobalTaskDispatcher(TaskPriority.DEFAULT);// 執行任務 total 次。dispatcher.applyDispatch((index)->{indexList.add(index);latch.countDown();}, total);// 設置任務超時。try{ latch.await();}catch(InterruptedException exception){HiLog.error(LABEL_LOG,"latch exception");}HiLog.info(LABEL_LOG,"list size matches, %{public}b",(total == indexList.size()));// 執行結果:// list size matches, true