使用多線程用new Thread():
1.多任務(wù)情況下,避免線程頻繁的創(chuàng)建銷毀窝爪;
2.多個(gè)線程頻繁的創(chuàng)建會(huì)占用大量的資源弛车,并且在資源競(jìng)爭(zhēng)的時(shí)候出現(xiàn)問(wèn)題,缺乏統(tǒng)一的管理蒲每,容易造成線程卡頓纷跛;
3.多個(gè)線程頻繁銷毀,會(huì)頻繁調(diào)用GC機(jī)制邀杏,降低性能并且耗時(shí)忽舟;
線程池的作用:
1.對(duì)線程統(tǒng)一管理,避免資源競(jìng)爭(zhēng)造成卡頓淮阐、死機(jī)等問(wèn)題叮阅;
2.對(duì)線程服用,不會(huì)在線程結(jié)束后立即銷毀泣特,等待其他任務(wù)浩姥。避免了頻繁創(chuàng)建、銷毀和調(diào)用GC機(jī)制
public class ThreadPool {
ThreadPoolExecutor mThreadPoolExecutor;
private int corePoolSize;
private int maximumPoolSize;
private long keepAliveTime;
private static ThreadPool mThreadPool = null;
public static ThreadPool getInstance() {
if (mThreadPool == null) {
synchronized (ThreadPool.class){
mThreadPool = new ThreadPool(5, 10, 5 * 1000);
}
}
return mThreadPool;
}
private ThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.keepAliveTime = keepAliveTime;
}
private ThreadPoolExecutor initExecutor() {
if (mThreadPoolExecutor == null) {
synchronized (ThreadPool.class) {
if (mThreadPoolExecutor == null) {
TimeUnit unit = TimeUnit.MILLISECONDS;
ThreadFactory threadFactory = Executors.defaultThreadFactory();
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
mThreadPoolExecutor = new ThreadPoolExecutor(
corePoolSize,//核心線程數(shù)
maximumPoolSize,//最大線程數(shù)
keepAliveTime,//保持時(shí)間
unit,//保持時(shí)間對(duì)應(yīng)的單位
workQueue,
threadFactory,//線程工廠
handler);//異常捕獲器
}
}
}
return mThreadPoolExecutor;
}
/**
* 執(zhí)行任務(wù)
*/
public void executeTask(Runnable r) {
initExecutor();
mThreadPoolExecutor.execute(r);
}
/**
* 提交任務(wù)
*/
public Future<?> commitTask(Runnable r) {
initExecutor();
return mThreadPoolExecutor.submit(r);
}
/**
* 刪除任務(wù)
*/
public void removeTask(Runnable r) {
initExecutor();
mThreadPoolExecutor.remove(r);
}
}
FutureTask
FutureTask是實(shí)現(xiàn)了future的Runable状您,F(xiàn)utureTask比Runable多了一個(gè)執(zhí)行的返回值
private void test(){
AsyncTask<Boolean,String,Integer> asyncTask = new AsyncTask<Boolean, String, Integer>() {
@Override
protected Integer doInBackground(Boolean... booleans) {
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected void onCancelled(Integer integer) {
super.onCancelled(integer);
}
@Override
protected void onCancelled() {
super.onCancelled();
}
};
asyncTask.execute();
asyncTask.getStatus();
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return false;
}
};
FutureTask futureTask = new FutureTask(callable );
futureTask.cancel(true);
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
2,10,300,
TimeUnit.MICROSECONDS,new LinkedBlockingDeque<Runnable>(),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
Future<Boolean> future= poolExecutor.submit(callable);
future.cancel(true);
}