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()
Run parallel tasks. Current thread will wait for all children tasks to finish until the setting time-out is reached. (if timeOut < 0 , it 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();