executor 線程池
public interface Executor {
/**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the {@code Executor} implementation.
*
* @param command the runnable task
* @throws RejectedExecutionException if this task cannot be
* accepted for execution
* @throws NullPointerException if command is null
*/
void execute(Runnable command);
}
execute方法代表執(zhí)行之景,需要傳入一個(gè)Runnable斤富,自動(dòng)執(zhí)行
public interface ExecutorService extends Executor {
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*/
void shutdown();
/**
* Attempts to stop all actively executing tasks, halts the
* processing of waiting tasks, and returns a list of the tasks
* that were awaiting execution.
*
* <p>This method does not wait for actively executing tasks to
* terminate. Use {@link #awaitTermination awaitTermination} to
* do that.
*
* <p>There are no guarantees beyond best-effort attempts to stop
* processing actively executing tasks. For example, typical
* implementations will cancel via {@link Thread#interrupt}, so any
* task that fails to respond to interrupts may never terminate.
*
* @return list of tasks that never commenced execution
*/
List<Runnable> shutdownNow();
/**
* Returns {@code true} if this executor has been shut down.
*
* @return {@code true} if this executor has been shut down
*/
boolean isShutdown();
/**
* Returns {@code true} if all tasks have completed following shut down.
* Note that {@code isTerminated} is never {@code true} unless
* either {@code shutdown} or {@code shutdownNow} was called first.
*
* @return {@code true} if all tasks have completed following shut down
*/
boolean isTerminated();
/**
* Blocks until all tasks have completed execution after a shutdown
* request, or the timeout occurs, or the current thread is
* interrupted, whichever happens first.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return {@code true} if this executor terminated and
* {@code false} if the timeout elapsed before termination
* @throws InterruptedException if interrupted while waiting
*/
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
/**
* Submits a value-returning task for execution and returns a
* Future representing the pending results of the task. The
* Future's {@code get} method will return the task's result upon
* successful completion.
*
* <p>
* If you would like to immediately block waiting
* for a task, you can use constructions of the form
* {@code result = exec.submit(aCallable).get();}
*
* <p>Note: The {@link Executors} class includes a set of methods
* that can convert some other common closure-like objects,
* for example, {@link java.security.PrivilegedAction} to
* {@link Callable} form so they can be submitted.
*
* @param task the task to submit
* @param <T> the type of the task's result
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
<T> Future<T> submit(Callable<T> task);
/**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's {@code get} method will
* return the given result upon successful completion.
*
* @param task the task to submit
* @param result the result to return
* @param <T> the type of the result
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
<T> Future<T> submit(Runnable task, T result);
/**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's {@code get} method will
* return {@code null} upon <em>successful</em> completion.
*
* @param task the task to submit
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
Future<?> submit(Runnable task);
/**
* Executes the given tasks, returning a list of Futures holding
* their status and results when all complete.
* {@link Future#isDone} is {@code true} for each
* element of the returned list.
* Note that a <em>completed</em> task could have
* terminated either normally or by throwing an exception.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param <T> the type of the values returned from the tasks
* @return a list of Futures representing the tasks, in the same
* sequential order as produced by the iterator for the
* given task list, each of which has completed
* @throws InterruptedException if interrupted while waiting, in
* which case unfinished tasks are cancelled
* @throws NullPointerException if tasks or any of its elements are {@code null}
* @throws RejectedExecutionException if any task cannot be
* scheduled for execution
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException;
/**
* Executes the given tasks, returning a list of Futures holding
* their status and results
* when all complete or the timeout expires, whichever happens first.
* {@link Future#isDone} is {@code true} for each
* element of the returned list.
* Upon return, tasks that have not completed are cancelled.
* Note that a <em>completed</em> task could have
* terminated either normally or by throwing an exception.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @param <T> the type of the values returned from the tasks
* @return a list of Futures representing the tasks, in the same
* sequential order as produced by the iterator for the
* given task list. If the operation did not time out,
* each task will have completed. If it did time out, some
* of these tasks will not have completed.
* @throws InterruptedException if interrupted while waiting, in
* which case unfinished tasks are cancelled
* @throws NullPointerException if tasks, any of its elements, or
* unit are {@code null}
* @throws RejectedExecutionException if any task cannot be scheduled
* for execution
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException;
/**
* Executes the given tasks, returning the result
* of one that has completed successfully (i.e., without throwing
* an exception), if any do. Upon normal or exceptional return,
* tasks that have not completed are cancelled.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param <T> the type of the values returned from the tasks
* @return the result returned by one of the tasks
* @throws InterruptedException if interrupted while waiting
* @throws NullPointerException if tasks or any element task
* subject to execution is {@code null}
* @throws IllegalArgumentException if tasks is empty
* @throws ExecutionException if no task successfully completes
* @throws RejectedExecutionException if tasks cannot be scheduled
* for execution
*/
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException;
/**
* Executes the given tasks, returning the result
* of one that has completed successfully (i.e., without throwing
* an exception), if any do before the given timeout elapses.
* Upon normal or exceptional return, tasks that have not
* completed are cancelled.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*
* @param tasks the collection of tasks
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @param <T> the type of the values returned from the tasks
* @return the result returned by one of the tasks
* @throws InterruptedException if interrupted while waiting
* @throws NullPointerException if tasks, or unit, or any element
* task subject to execution is {@code null}
* @throws TimeoutException if the given timeout elapses before
* any task successfully completes
* @throws ExecutionException if no task successfully completes
* @throws RejectedExecutionException if tasks cannot be scheduled
* for execution
*/
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
主要三個(gè)方法
void shutdown();
代表關(guān)閉線程池,不能再讓該線程池添加任務(wù)锻狗。但是在執(zhí)行中的任務(wù)和排隊(duì)中的任務(wù)不會(huì)受到影響满力,全部執(zhí)行完才會(huì)結(jié)束
List<Runnable> shutdownNow();
代表結(jié)束線程池。會(huì)通過Thread.interrput()來試圖結(jié)束正在執(zhí)行的任務(wù)轻纪,隊(duì)列中的任務(wù)不會(huì)執(zhí)行油额,并且將其返回。也會(huì)等待所有任務(wù)都結(jié)束才會(huì)真正結(jié)束刻帚。
<T> Future<T> submit(Callable<T> task);
代表執(zhí)行一個(gè)帶阻塞式的任務(wù)潦嘶,可以使用Callable,拿到線程的返回值
ThreadPoolExecutor
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters and default thread factory and rejected execution handler.
* It may be more convenient to use one of the {@link Executors} factory
* methods instead of this general purpose constructor.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
corePoolSize
代表默認(rèn)線程個(gè)數(shù)崇众。當(dāng)線程池被創(chuàng)建掂僵,就默認(rèn)創(chuàng)建多少個(gè);當(dāng)線程池在一個(gè)一個(gè)回收顷歌,回收都默認(rèn)個(gè)數(shù)后就不會(huì)回收了
maximumPoolSize
代表最大線程格數(shù)锰蓬。當(dāng)線程池中線程超過最大數(shù),就會(huì)進(jìn)入排隊(duì)隊(duì)列中眯漩。
keepAliveTime & unit
兩個(gè)一起使用芹扭,代表線程等待被回收的時(shí)間。當(dāng)線程執(zhí)行完赦抖,具體多少后會(huì)被回收舱卡,取決與這個(gè)時(shí)間
workQueue
代表任務(wù)隊(duì)列。當(dāng)線程池中線程超過最大數(shù)队萤,再添加任務(wù)先添加到此隊(duì)列轮锥,等待線程被回收后,線程池當(dāng)前線程數(shù)小于最大數(shù)要尔,將會(huì)取出執(zhí)行交胚。
ThreadPoolExecutor 常用實(shí)現(xiàn)
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
代表一個(gè)可以添加無數(shù)個(gè)任務(wù),默認(rèn)線程數(shù)0盈电,1分鐘會(huì)被回收的線程池蝴簇。
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
代表單線程的線程池
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
代表固定線程數(shù)的線程池,并且不會(huì)被回收匆帚,不能擴(kuò)展熬词。一般用來處理集中瞬時(shí)爆發(fā)的任務(wù),之后需要調(diào)用shutdown()來結(jié)束且回收
線程同步與安全
本質(zhì)就是資源問題
volatile
當(dāng)子線程使用了主線程的內(nèi)對象,其實(shí)子線程會(huì)把主線程的內(nèi)對象拷貝到自己的內(nèi)存塊互拾,當(dāng)子線程這個(gè)對象的值發(fā)送改變后歪今,主線程中這個(gè)對象不會(huì)發(fā)送變化,JVM這樣做是為了效率問題颜矿。為主線程的內(nèi)對象加上volatile關(guān)鍵字寄猩,會(huì)讓此對象具有同步性,這個(gè)時(shí)候在子線程修改對象值的時(shí)候骑疆,會(huì)同步到主線程內(nèi)對象上田篇,相反主線程修改也會(huì)同步到子線程。
ATomic
使用了volatile后箍铭,對象會(huì)有同步性泊柬,但是當(dāng)修改這個(gè)值是需要分幾步執(zhí)行時(shí)≌┗穑可能在修改過程中切換到另一個(gè)線程從頭開始修改兽赁,導(dǎo)致了程序的數(shù)據(jù)錯(cuò)亂。所以可以在Atomic來作為對象冷守,比如++ --刀崖,讓修改操作不僅僅具有同步性,而且具有原子性拍摇。
synchronized
同步的意思亮钦,保護(hù)代碼塊在多線程同時(shí)調(diào)用的時(shí)候,具有原子性授翻,不會(huì)出現(xiàn)數(shù)據(jù)錯(cuò)誤或悲。
monitor(鎖)
使用synchronized需要用到孙咪,可以理解為鎖堪唐。
當(dāng)為代碼塊使用synchronized (this) {}, 會(huì)使用當(dāng)前類對象的鎖。
當(dāng)其他線程同時(shí)調(diào)用時(shí)翎蹈,這個(gè)鎖還處于未解鎖淮菠,其他線程會(huì)在后面等待被執(zhí)行,直至鎖被解鎖荤堪,再執(zhí)行后面的線程任務(wù)合陵。
當(dāng)為代碼塊使用synchronized (${other monitor}) {},當(dāng)其他線程同時(shí)調(diào)用時(shí)澄阳,執(zhí)行的方法中有這個(gè)鎖才會(huì)等待執(zhí)行拥知,否則不受鎖限制。
死鎖
當(dāng)A方法碎赢,代碼塊用了A鎖低剔,A鎖中使用了B鎖;
當(dāng)B方法,代碼塊用了B鎖襟齿,B鎖中使用了A鎖姻锁;
A線程調(diào)用A方法,B線程調(diào)用B方法猜欺,且同時(shí)調(diào)用位隶。A執(zhí)行后A鎖會(huì)鎖住,B執(zhí)行后B鎖會(huì)鎖住开皿。A鎖中執(zhí)行到B鎖的時(shí)候涧黄,發(fā)現(xiàn)B鎖鎖住了,就會(huì)等待解鎖副瀑;B鎖中執(zhí)行到A鎖的時(shí)候弓熏,發(fā)現(xiàn)A鎖鎖住了,也會(huì)等待解鎖糠睡。這樣就成死鎖了挽鞠。
樂觀鎖悲觀鎖
這是操作數(shù)據(jù)庫才能的遇到的問題,一般在Android中不會(huì)出現(xiàn)
樂觀鎖的意思就是不加鎖狈孔,從數(shù)據(jù)庫拿了數(shù)據(jù)做業(yè)務(wù)邏輯修改數(shù)據(jù)信认,存入數(shù)據(jù)庫的時(shí)候,再拿數(shù)據(jù)庫最新數(shù)據(jù)和當(dāng)時(shí)拿出來的數(shù)據(jù)對比均抽,值沒有被其他人修改就提價(jià)修改嫁赏;值有差別,則再取出值重新做業(yè)務(wù)邏輯油挥。
悲觀鎖的意思就是我從數(shù)據(jù)庫開始讀取數(shù)據(jù)的時(shí)候潦蝇,我就加鎖,不允許其他人修改數(shù)據(jù)深寥。我拿到數(shù)據(jù)處理完直接更新數(shù)據(jù)庫攘乒,之后我再解鎖,讓別人操作惋鹅。
靜態(tài)鎖
一般用于單例模式
getInstance() 可能存在多線程同時(shí)調(diào)用则酝,防止多次初始化并且覆蓋操作,需要加上靜態(tài)鎖
synchronized(Class.class)