Skip to content
Carlyle-Lee edited this page May 11, 2020 · 7 revisions
TM

TM is TaskManager for short. It encapsulated some API of TaskManger, so that there is no need to call TaskManager.getInstance each time. For more advanced apis you may need to refer to TaskManager.

public static void triggerEvent(Object group, int event, Object data)
Trigger an event, any task which is watching the group event , will be notified and with data been dilivered. Task will be executed if there is no other dependences. 触发一个局部事件。依赖这个事件的任务将会收到这个事件和数据。若无其他依赖条件,任务将被触发执行。

// 支持多次任务执行,需要手动调用 unregister
        new EventTask(){

            @Override
            public void onEvent(int eventId, Object msg) {
                log("on event " + msg);
            }
        }.registerGroupedEvents(this, 1)
                .postUI();

        // 模拟多次调用
        new TickTask(){
            @Override
            public void onTick(int loopTime) {
                TM.triggerEvent(TriggerEventTest.this, 1, new Integer(320));
            }
        }.setMaxLoopTime(10)
                .setIntervalWithFixedDelay(100)
                .postAsync();

public static void triggerEvent(int event)
Trigger an event, any task which is watching this global event , will be notified.
触发一个全局事件。依赖这个事件的任务将会收到这个事件。

// task run only once
  new Task(){

            @Override
            public void doTask() {
                // do sth
            }
        }.dependOn(R.id.event_vv)
                .postAsync();
                
     // Trigger task to run
     TM.triggerEvent(R.id.event_vv);

public static void triggerEvent(int event, Object data) Trigger event with data.

public static void needTaskSync(int taskId)
Declair that the task needs to be finished , when this statement is returned.
申明需要taskId 对应的任务执行完成。如果任务没有执行,则任务在当前线程执行。如果任务已执行完成,则立即返回。如果任务正在执行,则等待任务执行完成。如果在等待超时后,任务还没有执行完成,则任务在当前线程直接执行。

@Unsafe : not fully tested.
public static void waitForTaskWithTimeout(int taskId, int timeOutMillions)
This api will wait for task to finish if task is running. But will not execute in current thread when timeout or when task is not started.

public static void executeParallel(int timeout, Task... tasks)
Tasks will be enwrapped as ParallelTask to run; This statement will return only if all task are finished or timeout reached.
任务将被封装为并发任务执行。方法将会在所有任务全部执行完成后或者超时后才返回。如果参数中只有一个Task,请不要使用此API。

public static void executeAsyncNow(Runnable runnable)
Execute a runnable on background thread right now. This api will guarantee the runnable to be executed ASAP.
API 保证此runnable 将立即执行。

public static void cancelTaskById(int taskId)
Cancel a task by taskId;

public static void cancelTaskByToken(Object token)
Cancel a task by token. Any task with such token will be cancled.

Clone this wiki locally