A synchronous queue does not have any internal capacity, not even a capacity of one
synchronous queue
這個隊列沒有容量,一條也沒有。
基于生產(chǎn)者-消費者模式,可實現(xiàn)同步阻塞的功能。
生產(chǎn)者生產(chǎn)數(shù)據(jù)后,如果沒有消費者進行消費,那么生產(chǎn)者線程將會阻塞,直到有消費者消費數(shù)據(jù)。
如果消費者要消費數(shù)據(jù),但是沒有生產(chǎn)者生產(chǎn)數(shù)據(jù),那么消費者線程將阻塞,直到有生產(chǎn)者生產(chǎn)數(shù)據(jù)。
應用:
Executors.newCachedThreadPool
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
緩存線程池,不會有任務在隊列里面。新的任務會直接去獲取線程,若沒有線程可用,則創(chuàng)建線程,直到Integer最大值2^31-1。若線程數(shù)已達到最大值且還有任務進來,將進行拒絕處理。