鸿蒙的线程是有TaskDispatcher类来分发的,TaskDispatcher有四种GlobalTaskDispatcher、ParallelTaskDispatcher、SerialTaskDispatcher、SpecTaskDispatcher。
创建对象:
private String parallelName = "parallelTaskDispatcher";
private String serialName = "serialTaskDispatcher";
private TaskDispatcher globalTaskDispatcher;//全局并发任务分发器
private TaskDispatcher parallelTaskDispatcher;//并发任务分发器
private TaskDispatcher serialTaskDispatcher;//串行任务分发器
private TaskDispatcher uiTaskDispatcher;//专有任务分发器
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
parallelTaskDispatcher = createParallelTaskDispatcher(parallelName, TaskPriority.DEFAULT);
serialTaskDispatcher = createSerialTaskDispatcher(serialName, TaskPriority.DEFAULT);
uiTaskDispatcher = getUITaskDispatcher();
}
测试同步任务代码:
private void testSyncDispatch() {
globalTaskDispatcher.syncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "sync task1 run");
}
});
HiLog.info(label, "after sync task1");
parallelTaskDispatcher.syncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "sync task2 run");
}
});
HiLog.info(label, "after sync task2");
serialTaskDispatcher.syncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "sync task3 run");
}
});
HiLog.info(label, "after sync task3");
uiTaskDispatcher.syncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "sync task4 run");
}
});
HiLog.info(label, "after sync task4");
}
输出日志:
01-24 09:14:31.488 4741-27498/com.example.dispatcherdemo I 00201/MY_TAG: sync task1 run
01-24 09:14:31.489 4741-4741/com.example.dispatcherdemo I 00201/MY_TAG: after sync task1
01-24 09:14:36.490 4741-27783/com.example.dispatcherdemo I 00201/MY_TAG: sync task2 run
01-24 09:14:36.491 4741-4741/com.example.dispatcherdemo I 00201/MY_TAG: after sync task2
01-24 09:14:41.492 4741-28065/com.example.dispatcherdemo I 00201/MY_TAG: sync task3 run
01-24 09:14:41.492 4741-4741/com.example.dispatcherdemo I 00201/MY_TAG: after sync task3
syncDispatch是派发同步任务,可以看出无论哪种分发器,只要使用syncDispatch来派发任务,都是同步执行的,有点协程的味道。最后一个uiTaskDispatcher没有执行是因为发生了死锁,官网说以下情况使用syncDispatch会导致死锁:
如果对syncDispatch使用不当, 将会导致死锁。如下情形可能导致死锁发生:
- 在专有线程上,利用该专有任务分发器进行syncDispatch。
- 在被某个串行任务分发器(dispatcher_a)派发的任务中,再次利用同一个串行任务分发器(dispatcher_a)对象派发任务。
- 在被某个串行任务分发器(dispatcher_a)派发的任务中,经过数次派发任务,最终又利用该(dispatcher_a)串行任务分发器派发任务。例如:dispatcher_a派发的任务使用dispatcher_b进行任务的派发,在dispatcher_b派发的任务中又利用dispatcher_a进行派发任务。
- 串行任务分发器(dispatcher_a)派发的任务中利用串行任务分发器(dispatcher_b)进行同步派发任务,同时dispatcher_b派发的任务中利用串行任务分发器(dispatcher_a)进行同步派发任务。在特定的线程执行顺序下将导致死锁
测试异步任务代码:
private void testAsyncDispatch() {
globalTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "async task1 run");
}
});
HiLog.info(label, "after async task1");
parallelTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "sync task2 run");
}
});
HiLog.info(label, "after sync task2");
serialTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "sync task3 run");
}
});
HiLog.info(label, "after sync task3");
uiTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "sync task4 run");
}
});
HiLog.info(label, "after sync task4");
}
日志输出:
01-24 09:24:37.406 9317-9317/com.example.dispatcherdemo I 00201/MY_TAG: after async task1
01-24 09:24:37.407 9317-9317/com.example.dispatcherdemo I 00201/MY_TAG: after sync task2
01-24 09:24:37.407 9317-9317/com.example.dispatcherdemo I 00201/MY_TAG: after sync task3
01-24 09:24:37.408 9317-9317/com.example.dispatcherdemo I 00201/MY_TAG: after sync task4
01-24 09:24:42.407 9317-30393/com.example.dispatcherdemo I 00201/MY_TAG: async task1 run
01-24 09:24:42.407 9317-30394/com.example.dispatcherdemo I 00201/MY_TAG: sync task2 run
01-24 09:24:42.409 9317-9317/com.example.dispatcherdemo I 00201/MY_TAG: sync task4 run
01-24 09:24:42.409 9317-30395/com.example.dispatcherdemo I 00201/MY_TAG: sync task3 run
无论哪种分发器,只要使用了asyncDispatch分发任务,都是异步执行,既然这样,为什么还要给分发器分类呢。
先测试一下Paralle和Serial分发异步任务
private void testSerialDispatch() {
serialTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "serialTaskDispatcher sync task1 run");
}
});
HiLog.info(label, "serialTaskDispatcher after sync task1");
serialTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "serialTaskDispatcher sync task2 run");
}
});
HiLog.info(label, "serialTaskDispatcher after sync task2");
}
private void testParalleDispatch() {
parallelTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "parallelTaskDispatcher sync task1 run");
}
});
HiLog.info(label, "parallelTaskDispatcher after sync task1");
parallelTaskDispatcher.asyncDispatch(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "parallelTaskDispatcher sync task2 run");
}
});
HiLog.info(label, "parallelTaskDispatcher after sync task2");
}
日志:
01-24 09:36:52.844 15707-15707/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher after sync task1
01-24 09:36:52.845 15707-15707/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher after sync task2
01-24 09:36:53.845 15707-8432/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher sync task2 run
01-24 09:36:57.845 15707-8431/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher sync task1 run
01-24 09:37:00.949 15707-15707/com.example.dispatcherdemo I 00201/MY_TAG: serialTaskDispatcher after sync task1
01-24 09:37:00.950 15707-15707/com.example.dispatcherdemo I 00201/MY_TAG: serialTaskDispatcher after sync task2
01-24 09:37:05.950 15707-8894/com.example.dispatcherdemo I 00201/MY_TAG: serialTaskDispatcher sync task1 run
01-24 09:37:06.951 15707-9210/com.example.dispatcherdemo I 00201/MY_TAG: serialTaskDispatcher sync task2 run
原来,使用使用syncDispatch和asyncDispatch分发任务,同步异步是针对于主线程的,而ParallelTaskDispatcher、SerialTaskDispatcher的区别是自己派发出去的任务是串行还是并行。
常规操作延时执行:
private void testDelayDispatch() {
globalTaskDispatcher.delayDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(label, "delayDispatch task1 run");
}
}, 50000);
HiLog.info(label, "after delayDispatch task1");
}
鸿蒙还可以将线程分组,将几个线程放在一个group中,并且可以添加这一组线程执行完的回调:
private void testGroupDispatch() {
// 创建任务组。
Group group = parallelTaskDispatcher.createDispatchGroup();
// 将任务1加入任务组,返回一个用于取消任务的接口。
parallelTaskDispatcher.asyncGroupDispatch(group, new Runnable(){
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "download task1 is running");
}
});
// 将与任务1相关联的任务2加入任务组。
parallelTaskDispatcher.asyncGroupDispatch(group, new Runnable(){
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "download task2 is running");
}
});
// 在任务组中的所有任务执行完成后执行指定任务。
parallelTaskDispatcher.groupDispatchNotify(group, new Runnable(){
@Override
public void run() {
HiLog.info(label, "the close task is running after all tasks in the group are completed");
}
});
}
日志:
01-24 09:49:25.346 4247-8522/com.example.dispatcherdemo I 00201/MY_TAG: download task1 is running
01-24 09:49:25.348 4247-8523/com.example.dispatcherdemo I 00201/MY_TAG: download task2 is running
01-24 09:49:25.349 4247-8587/com.example.dispatcherdemo I 00201/MY_TAG: the close task is running after all tasks in the group are completed
对于分组任务还可以设置屏障任务:可以使用asyncDispatchBarrier和syncDispatchBarrier来派发异步或同步的屏障线程,这种线程必须是在Group线程执行完成后,才执行的
private void testBarrierDispatch() {
// 创建任务组。
Group group = parallelTaskDispatcher.createDispatchGroup();
// 将任务加入任务组,返回一个用于取消任务的接口。
parallelTaskDispatcher.asyncGroupDispatch(group, new Runnable(){
@Override
public void run() {
HiLog.info(label, "parallelTaskDispatcher task1 is running"); // 1
}
});
parallelTaskDispatcher.asyncGroupDispatch(group, new Runnable(){
@Override
public void run() {
HiLog.info(label, "parallelTaskDispatcher task2 is running"); // 2
}
});
parallelTaskDispatcher.syncDispatchBarrier(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "parallelTaskDispatcher barrier is running"); // 3
}
});
HiLog.info(label, "parallelTaskDispatcher after syncDispatchBarrier"); // 4
TaskDispatcher parallelTaskDispatcher2 = createParallelTaskDispatcher("dispatcherName", TaskPriority.DEFAULT);
// 创建任务组。
Group group2 = parallelTaskDispatcher2.createDispatchGroup();
// 将任务加入任务组,返回一个用于取消任务的接口。
parallelTaskDispatcher2.asyncGroupDispatch(group2, new Runnable(){
@Override
public void run() {
HiLog.info(label, "parallelTaskDispatcher2 task1 is running"); // 1
}
});
parallelTaskDispatcher2.asyncGroupDispatch(group2, new Runnable(){
@Override
public void run() {
HiLog.info(label, "parallelTaskDispatcher2 task2 is running"); // 2
}
});
parallelTaskDispatcher2.asyncDispatchBarrier(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HiLog.info(label, "parallelTaskDispatcher2 barrier is running"); // 3
}
});
HiLog.info(label, "parallelTaskDispatcher2 after asyncDispatchBarrier"); // 4
}
日志:
01-24 10:08:15.493 30657-9662/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher task1 is running
01-24 10:08:15.493 30657-9663/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher task2 is running
01-24 10:08:16.494 30657-9664/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher barrier is running
01-24 10:08:16.495 30657-30657/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher after syncDispatchBarrier
01-24 10:08:16.496 30657-9727/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher2 task1 is running
01-24 10:08:16.496 30657-30657/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher2 after asyncDispatchBarrier
01-24 10:08:16.496 30657-9728/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher2 task2 is running
01-24 10:08:17.497 30657-9729/com.example.dispatcherdemo I 00201/MY_TAG: parallelTaskDispatcher2 barrier is running
取消任务:Revocable是取消一个异步任务的接口。异步任务包括通过 asyncDispatch、delayDispatch、asyncGroupDispatch 派发的任务。如果任务已经在执行中或执行完成,则会返回取消失败。
private void testRevocableDispatch() {
Revocable revocable = parallelTaskDispatcher.delayDispatch(new Runnable() {
@Override
public void run() {
HiLog.info(label, "delay dispatch");
}
}, 1000);
boolean revoked = revocable.revoke();
HiLog.info(label, "%{public}b", revoked);
}
日志:
01-24 09:53:45.649 7590-7590/com.example.dispatcherdemo I 00201/MY_TAG: true
执行多次任务:对指定任务执行多次,使用applyDispatch配合CountDownLatch使用,可以让线程重复执行指定次数:
final int total = 10;
final CountDownLatch latch = new CountDownLatch(total);
final ArrayList<Long> indexList = new ArrayList<>(total);
private void testApplyDispatch() {
// 执行任务 total 次
globalTaskDispatcher.applyDispatch((index) -> {
indexList.add(index);
HiLog.info(label, "applyDispatch index="+index);
latch.countDown();
}, total);
// 设置任务超时
try {
latch.await();
} catch (InterruptedException exception) {
HiLog.info(label, "latch exception");
}
HiLog.info(label, "list size matches, %{public}b", (total == indexList.size()));
}
日志:
01-24 10:16:31.939 29071-7870/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=0
01-24 10:16:31.940 29071-7871/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=1
01-24 10:16:31.940 29071-7873/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=3
01-24 10:16:31.941 29071-7874/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=4
01-24 10:16:31.941 29071-7875/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=5
01-24 10:16:31.941 29071-7872/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=2
01-24 10:16:31.942 29071-7877/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=7
01-24 10:16:31.943 29071-7878/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=8
01-24 10:16:31.943 29071-7879/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=9
01-24 10:16:31.943 29071-7876/com.example.dispatcherdemo I 00201/MY_TAG: applyDispatch index=6
01-24 10:16:31.943 29071-29071/com.example.dispatcherdemo I 00201/MY_TAG: list size matches, true
Demo:https://download.csdn.net/download/y280903468/14926982