Skip to content
Carlyle-Lee edited this page Jun 7, 2020 · 6 revisions

Task

public Task(String name)
Construct a new task with a specified task name;

public Task(String name, int taskId)
Construct a new task with a specified task name and an Id. Its better to use a resource id here.

public Task dependOn(int ... ids)
Set this task to run after all tasks of the specified ids are finished;
为任务设置依赖关系。当参数指定的任务id对应的任务全部执行完成后,当前任务才可以执行。id 最好在xml 中申明,或者使用id = genNewEventId 生成。不可使用自定义的id。多个任务id 为与依赖。需要等待参数中的全部的id对应的任务完成后,当前任务才能执行。

public Task orDependOn(int ...ids)
Same as 'dependOn', this task need to wait until all task (specified by the param ids ) to be finished . Each statement of "dependOn" makes 'Or logic'.
与”dependOn“方法类似,参数内部的ids 仍然是”与依赖“,但是多条依赖语句构成”或依赖“。

Example : this task will be executed :

  1. after task_1 & task_2 finished.
  2. or after task_3 & task_4 finished;
  3. or after 2000ms;
  4. Task will run in background thread. Different statement makes "Or Logic".
    不同的语句之间构成‘或’逻辑。
new Task(){
            @Override
            public void doTask() {
                // your task
            }
        }.dependOn(R.id.task_1, R.id.task_2) // 或者任务在 task1 与 task2 完成后执行
         .orDependOn(R.id.task_3,R.id.task_4)// 或者任务在 task3 与 task4 完成后执行
         .orDelay(2000)// 或者 任务在2000ms 后执行。
         .postAsync();//提交任务到子线程执行

public Task delayAfter(int delayTime, int ...ids)
Same as "dependOn", this task will be executed later in "delayTime" after all tasks (specified by parameteres) are finished . 与"dependOn" 方法类似,当前任务将灰再参数指定的任务执行完成后,并等待一段事件后再执行。

public Task dependOn(Task ... tasks)

private void registerTask(Task task1, Task task2) {

        new Task() {

            @Override
            public void doTask() {
                //do sth
            }
        }.dependOn(task1, task2)
                .postAsync();

    }

public Task enableIdleRun()
Task will run when tm thread pool is not so busy while there is no other restrictions.
当任务处于可执行状态后(所有依赖的任务或者其他限制条件都已经满足),任务将在TM线程池比较空闲的时候执行。

public void executeSync(int ...ids)
Task will be executed right after all task finished. (Will be executed in the thread where the last dependent task is finished.) If it has no dependencies , task will be executed right now, in current thread.
任务将会再所有依赖任务中,最后一个完成的依赖任务的线程中立即执行。如果没有依赖任务,则会在当前线程中立即执行。

public void executeSyncUI(int ...ids)
Same as "executeSync", if current running thread is not UI thread, this task will be posted to UI thread to run.
与“executeSync” 一致, 如果当前线程不是UI 线程,那么任务将会被抛到UI线程执行。

public void executeSerial(String goupName) All tasks with the same group name , will be executed in FIFO order.
所有的相同group name 的任务,将会按照FIFO顺序执行。可用于替代HandlerThread 保证任务顺序执行。另,参考TM.cancelTaskByToken 移除任务。

TM.cancelTaskByToken("pingback");

 new Task() {

            @Override
            public void doTask() {
                //do sth
            }
        }.setToken("pingback")
                .executeSerial("pingback");

public Task bind(Context context) If "context " is instance of Activity, this task wiil be bound to Activity lifecycle. When activity is finished , task will be canceled & task record will be removed;
如果context 是Activity 实例,任务将会与Activity 生命周期绑定。当Activity 销毁的时候,如果任务没有执行,那么任务将被取消。任务记录也会被自动删除。

public void post()
Post task to run , dont care with thread is going to run.
提交任务,不关心任务执行的线程。

public void postUI()
Post task to run on UI thread.
将任务提交到主线程执行。

public void postAsync()
Post task to run on background thread.
将任务提交到子线程去执行。

Clone this wiki locally