org.xutils.common.task.TaskControllerImpl
package org.xutils.common.task;
import android.os.Looper;
import org.xutils.common.Callback;
import org.xutils.common.TaskController;
import org.xutils.common.util.LogUtil;
import org.xutils.x;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author 注释者:王教成
* @version 注释版:1.0.0
* 异步任务的管理类,任务管理接口实现
*/
public final class TaskControllerImpl implements TaskController {
private TaskControllerImpl() {
}//构造器
private static volatile TaskController instance;//声明共享任务管理接口实例
/**
* 注册实例
*/
public static void registerInstance() {
if (instance == null) {
synchronized (TaskController.class) {
if (instance == null) {
instance = new TaskControllerImpl();//如果实例非空,则锁定接口,创建新实例
}
}
}
x.Ext.setTaskController(instance);//设置任务管理器
}
/**
* 在UI线程执行runnable,如果已在UI线程,则直接执行
* @param runnable 线程对象
*/
@Override
public void autoPost(Runnable runnable) {
if (runnable == null) return;//runnable为空直接返回
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
runnable.run();//如果当前线程与循环器主循环器线程相同,执行线程的线程体
} else {
TaskProxy.sHandler.post(runnable);//否则执行Handler的post方法
}
}
/**
* 在UI线程执行runnable,post到msg queue
* @param runnable 线程对象
*/
@Override
public void post(Runnable runnable) {
if (runnable == null) return;//runnable为空直接返回
TaskProxy.sHandler.post(runnable);//执行Handler的post方法
}
/**
* 在UI线程执行runnable
* @param runnable 线程对象
* @param delayMillis 延迟时间(单位毫秒)
*/
@Override
public void postDelayed(Runnable runnable, long delayMillis) {
if (runnable == null) return;////runnable为空直接返回
TaskProxy.sHandler.postDelayed(runnable, delayMillis);//执行Handler的postDelayed方法
}
/**
* 在后台线程执行runnable
* @param runnable 线程对象
*/
@Override
public void run(Runnable runnable) {
if (!TaskProxy.sDefaultExecutor.isBusy()) {
TaskProxy.sDefaultExecutor.execute(runnable);//不过执行器不忙则执行
} else {
new Thread(runnable).start();//否则开启新线程
}
}
/**
* 移除post或postDelayed提交的,未执行的runnable
* @param runnable 线程对象
*/
@Override
public void removeCallbacks(Runnable runnable) {
TaskProxy.sHandler.removeCallbacks(runnable);//执行Handler的removeCallbacks方法
}
/**
* 开始一个异步任务
* @param task 异步任务
* @param <T> 泛型
* @return 返回一个异步任务
*/
@Override
public <T> AbsTask<T> start(AbsTask<T> task) {
TaskProxy<T> proxy = null;
if (task instanceof TaskProxy) {
proxy = (TaskProxy<T>) task;//非空强转赋值
} else {
proxy = new TaskProxy<T>(task);//否则新建
}
try {
proxy.doBackground();//执行背景方法
} catch (Throwable ex) {
LogUtil.e(ex.getMessage(), ex);//记录日志
}
return proxy;//返回异步任务
}
/**
* 同步执行一个任务
* @param task 异步任务
* @param <T> 泛型
* @return 返回泛型
* @throws Throwable 抛出异常
*/
@Override
public <T> T startSync(AbsTask<T> task) throws Throwable {
T result = null;
try {
task.onWaiting();
task.onStarted();
result = task.doBackground();
task.onSuccess(result);
} catch (Callback.CancelledException cex) {
task.onCancelled(cex);
} catch (Throwable ex) {
task.onError(ex, false);
throw ex;
} finally {
task.onFinished();
}
return result;
}
/**
* 批量执行异步任务
* @param groupCallback 组回调
* @param tasks 批量异步任务
* @param <T> 泛型
* @return 返回回调可取消接口
*/
@SuppressWarnings("unchecked")
@Override
public <T extends AbsTask<?>> Callback.Cancelable startTasks(
final Callback.GroupCallback<T> groupCallback, final T... tasks) {
if (tasks == null) {
throw new IllegalArgumentException("task must not be null");//异步任务代理对象为空,抛出非法参数异常
}
final Runnable callIfOnAllFinished = new Runnable() {
private final int total = tasks.length;//定义异步任务代理对象组数量
private final AtomicInteger count = new AtomicInteger(0);//创建原子int
@Override
public void run() {
if (count.incrementAndGet() == total) {
if (groupCallback != null) {
groupCallback.onAllFinished();//前值加一与对象组数量相等,组回调非空,组回调全部完成
}
}
}
};//创建全部完成调用线程
for (final T task : tasks) {
start(new TaskProxy(task) {
@Override
protected void onSuccess(Object result) {
super.onSuccess(result);
post(new Runnable() {
@Override
public void run() {
if (groupCallback != null) {
groupCallback.onSuccess(task);
}
}
});
}
@Override
protected void onCancelled(final Callback.CancelledException cex) {
super.onCancelled(cex);
post(new Runnable() {
@Override
public void run() {
if (groupCallback != null) {
groupCallback.onCancelled(task, cex);
}
}
});
}
@Override
protected void onError(final Throwable ex, final boolean isCallbackError) {
super.onError(ex, isCallbackError);
post(new Runnable() {
@Override
public void run() {
if (groupCallback != null) {
groupCallback.onError(task, ex, isCallbackError);
}
}
});
}
@Override
protected void onFinished() {
super.onFinished();
post(new Runnable() {
@Override
public void run() {
if (groupCallback != null) {
groupCallback.onFinished(task);
}
callIfOnAllFinished.run();
}
});
}
});//迭代启动所有任务
}
return new Callback.Cancelable() {
@Override
public void cancel() {
for (T task : tasks) {
task.cancel();//迭代取消
}
}
@Override
public boolean isCancelled() {
boolean isCancelled = true;
for (T task : tasks) {
if (!task.isCancelled()) {
isCancelled = false;//迭代标记false
}
}
return isCancelled;
}
};
}
}