赞
踩
优先级 | 详细描述 |
---|---|
HIGH | 最高任务优先级,比默认优先级、低优先级的任务有更高的几率得到执行 |
DEFAULT | 默认任务优先级, 比低优先级的任务有更高的几率得到执行 |
LOW | 低任务优先级,比高优先级、默认优先级的任务有更低的几率得到执行 |
TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
String dispatcherName = "parallelTaskDispatcher";
TaskDispatcher parallelTaskDispatcher = createParallelTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
String dispatcherName = "serialTaskDispatcher";
TaskDispatcher serialTaskDispatcher = createSerialTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
TaskDispatcher uiTaskDispatcher = getUITaskDispatcher();
TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
globalTaskDispatcher.syncDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "sync task1 run");
}
});
HiLog.info(LABEL_LOG, "after sync task1");
globalTaskDispatcher.syncDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "sync task2 run");
}
});
HiLog.info(LABEL_LOG, "after sync task2");
globalTaskDispatcher.syncDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "sync task3 run");
}
});
HiLog.info(LABEL_LOG, "after sync task3");
// 执行结果如下:
// sync task1 run
// after sync task1
// sync task2 run
// after sync task2
// sync task3 run
// after sync task3
TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "async task1 run");
}
});
HiLog.info(LABEL_LOG, "after async task1");
// 执行结果可能如下:
// after async task1
// async task1 run
final long callTime = System.currentTimeMillis();
final long delayTime = 50L;
TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
Revocable revocable = globalTaskDispatcher.delayDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "delayDispatch task1 run");
final long actualDelay = System.currentTimeMillis() - callTime;
HiLog.info(LABEL_LOG, "actualDelayTime >= delayTime: %{public}b", (actualDelay >= delayTime));
}
}, delayTime);
HiLog.info(LABEL_LOG, "after delayDispatch task1");
// 执行结果可能如下:
// after delayDispatch task1
// delayDispatch task1 run
// actualDelayTime >= delayTime : true
String dispatcherName = "parallelTaskDispatcher";
TaskDispatcher dispatcher = createParallelTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
// 创建任务组。
Group group = dispatcher.createDispatchGroup();
// 将任务1加入任务组,返回一个用于取消任务的接口。
dispatcher.asyncGroupDispatch(group, new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "download task1 is running");
}
});
// 将与任务1相关联的任务2加入任务组。
dispatcher.asyncGroupDispatch(group, new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "download task2 is running");
}
});
// 在任务组中的所有任务执行完成后执行指定任务。
dispatcher.groupDispatchNotify(group, new Runnable() {
@Override
public void run() {
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
TaskDispatcher dispatcher = getUITaskDispatcher();
Revocable revocable = dispatcher.delayDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "delay dispatch");
}
}, 10);
boolean revoked = revocable.revoke();
HiLog.info(LABEL_LOG, "%{public}b", revoked);
// 一种可能的结果如下 :
// true
String dispatcherName = "parallelTaskDispatcher";
TaskDispatcher dispatcher = createParallelTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
// 创建任务组。
Group group = dispatcher.createDispatchGroup();
// 将任务加入任务组,返回一个用于取消任务的接口。
dispatcher.asyncGroupDispatch(group, new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "task1 is running"); // 1
}
});
dispatcher.asyncGroupDispatch(group, new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "task2 is running"); // 2
}
});
dispatcher.syncDispatchBarrier(new Runnable() {
@Override
public void run() {
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, new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "task1 is running"); // 1
}
});
dispatcher.asyncGroupDispatch(group, new Runnable() {
@Override
public void run() {
HiLog.info(LABEL_LOG, "task2 is running"); // 2
}
});
dispatcher.asyncDispatchBarrier(new Runnable() {
@Override
public void run() {
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
final int total = 10;
final CountDownLatch latch = new CountDownLatch(total);
final List<Long> indexList = new ArrayList<>(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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。