The thread pool likened decoration company

 

To operate a decoration company to be a metaphor. The company waiting in office customers to submit requests decoration; the company has a fixed number of regular workers in order to maintain the operation; the peak season when more business, new client requests will be ranked on such orders after a month to tell the user to start renovation; when too much schedule, to avoid user wait too long, the company will be through some channels (such as the job market, acquaintances, etc.) hire temporary workers (Note that after the recruitment of temporary workers filled in the schedule); if temporary workers too busy, the company will decide not to receive new clients, refused direct orders.

Thread Pool is a program of "decoration company," do it all kinds of dirty work. The above procedure corresponds to the thread pool:

// Java线程池的完整构造函数
public ThreadPoolExecutor( int corePoolSize, // 正式工数量 int maximumPoolSize, // 工人数量上限,包括正式工和临时工 long keepAliveTime, TimeUnit unit, // 临时工游手好闲的最长时间,超过这个时间将被解雇 BlockingQueue<Runnable> workQueue, // 排期队列 ThreadFactory threadFactory, // 招人渠道 RejectedExecutionHandler handler) // 拒单方式

to sum up

ExecutorsProvides a convenient method for constructing the thread pool for us, we should put an end to the server program easy to use these methods, but direct use of the thread pool ThreadPoolExecutorconstruction method, the number of threads to avoid OOM and unbounded number of threads queues may lead to restrictions due to improper consumption do other issues. ExecutorCompletionServiceIt provides an efficient way to perform all the tasks waiting for the end, if you want to set the timeout time to wait, you can CountDownLatchcomplete.

Guess you like

Origin www.cnblogs.com/findbetterme/p/11519202.html