Skip to content

ParallelTask

Carlyle-Lee edited this page Jun 3, 2020 · 4 revisions

####并发任务类,可以添加多个子任务。使用execute 提交任务。execute 方法将会在子任务全部执行完成后返回,或者在设置的等待超时时间到达后,返回。 使用场景:将一个耗时的任务,并行化执行,加快任务执行的速度。

public ParallelTask setTimeOut(int timeOut)
Set how much time current thread is going to wait for other thread to join;

public ParallelTask addSubTask(Runnable run)
Add a sub task to run;

public ParallelTask addSubTask(Task task)
Add a sub task to run;

public void execute()
Start parallel task running. Current thread will wait all child tasks to finish with some time out by setting. (<=0 , will wait until all tasks finished.)

new ParalleTask()
        .addSubTask(new Runnable() {
            @Override
 public void run() {
                appendEventBusIndexStub0();
            }
        })
        .addSubTask(new Runnable() {
            @Override
 public void run() {
                appendEventBusIndexStub1();
            }
        })
        .addSubTask(new Runnable() {
            @Override
 public void run() {
                appendEventBusIndexStub2();
            }
        })
        .addSubTask(new Runnable() {
            @Override
 public void run() {
                appendEventBusIndexStub3();
            }
        })
        .execute();
Clone this wiki locally