最近的项目中有用到很多在线程中请求服务器的耗时操作,但是没有大量的高并发操作,因此不打算使用线程池,但是
每次都做请求服务器的耗时操作时,都会new Thread,这样做可能会损耗性能,也可能会产生其他原因。因此打算使用封装一个线程管理的类,统一管理线程,方便维护。
public class ThreadManager {
/** 主线程 */
public static final int THREAD_UI = 0;
/** background线程,用于一般的耗时操作 */
public static final int THREAD_BACKGROUND = 1;
/** data线程,用于数据库操作 */
public static final int THREAD_DATA = 2;
private static final int THREAD_SIZE = 3;
/** 线程信息数组 */
private static final Handler[] HANDLER_LIST = new Handler[THREAD_SIZE];
private static final String[] THREAD_NAME_LIST = {
"thread_ui",
"thread_background",
"thread_data"
};
private ThreadManager() {
HANDLER_LIST[THREAD_UI] = new Handler();
}
private static class ThreadManagerHolder {
private static ThreadManager sManager = new ThreadManager();
}
public static ThreadManager getManager() {
return ThreadManagerHolder.sManager;
}
/**
* 派发任务
*
* @param index 线程类型
*/
public void post(int index, Runnable r) {
postDelayed(index, r, 0);
}
/**
* 延迟派发任务
*
* @param index 线程类型
*/
public void postDelayed(int index, Runnable r, long delayMillis) {
Handler handler = getHandler(index);
handler.postDelayed(r, delayMillis);
}
/**
* 删除任务
*
* @param index 线程类型
*/
public void removeCallbacks(int index, Runnable r) {
Handler handler = getHandler(index);
handler.removeCallbacks(r);
}
/**
* 获取线程Handler
*
* @param index 线程类型
*/
public Handler getHandler(int index) {
if (index < 0 || index >= THREAD_SIZE) {
throw new InvalidParameterException();
}
if (HANDLER_LIST[index] == null) {
synchronized (HANDLER_LIST) {
if (HANDLER_LIST[index] == null) {
HandlerThread thread = new HandlerThread(THREAD_NAME_LIST[index]);
if (index != THREAD_UI) {
//优先级要低于主线程
thread.setPriority(Thread.MIN_PRIORITY);
}
thread.start();
Handler handler = new Handler(thread.getLooper());
HANDLER_LIST[index] = handler;
}
}
}
return HANDLER_LIST[index];
}
/**
* 判断是否运行在当前线程
*
* @param index 线程类型
* @return true yes
*/
public boolean runningOnCurrent(int index) {
return getHandler(index).getLooper() == Looper.myLooper();
}
}
使用方法
ThreadManager.getManager.post(ThreadManager.THREAD_BACKGROUND,new Runable(){
@override
public void run(){
//TODO
}
});
原文链接:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0703/8147.html
网友评论