ThreadUtil工具类
public class ThreadUtil{
private static ThreadUtil _insatnce;
private static final byte[] wirteLock =
new byte[
0];
private ThreadPoolExecutor _pool;
private HashMap<String,Thread> threads =
new HashMap<>();
public interface Task{ void doTask(){} }
public void init(){get_pool();}
public void startThread(
final String key ,
final Task mTask){
Thread mThread =
new Thread(
new Runnable(){
@override
void run(){
mTask.doTask();
}
})
threads.put(key,mThread);
mThread.start();
}
public void finishThread(
final String key){
Thread mThread = threads.get(key);
mThread.interrput();
threads.remove(key);
}
public void startThreadInPool(
final Task mTask){
if(_pool !=
null && !_pool.isTerminated()){
_pool.submit(
new Runnable(){
});
}
}
public bool
isMainThread(){
return Looper.myLooper() == Looper.getMainLooper();
}
private ThreadUtil(){}
public ThreadUtil
getInstance(){
synchronized(wirteLock){
if( _instance ==
null ){
_instance =
new ThreadUtil();
}
}
return _instance;
}
public ThreadPoolExecutor
get_pool(){
synchronized(wirteLock){
if(_pool ==
null || _pool.isTerminated){
_pool =
new ThreadPoolExecutor(
10,
50,
3,TimeUnit.MINUTES,
new ArrayBlockingQueue<Runnable>(
20),
new RejectedExecutionHandler(){
@Override
rejectedExecution(...){
handler.postDelayed(
new Runnable(){
void run(){
if(!threadPoolexecutor.isShutdown){
threadPoolexecutor.execute(runnable);
}}
});
}
});
}
}
}
}