java multithreading (V)

First, what is the thread pool?

Is created when the program started several threads to respond to treatment, they are called thread pool threads called worker threads inside the 
first: to reduce resource consumption. By reusing the thread has been created to reduce thread creation and destruction caused by consumption. 
Second: improving the response speed. When the mission arrives, the task may not need to wait until the thread creation can be implemented immediately. 
Third: to improve the manageability of threads.

Second, the method of creating a thread pool

1, newFixedThreadPool () method: This method returns a fixed number of thread pool threads of this method is always the same, when there is a job submission, if the thread pool idle, then immediately executed, if not, it will be suspend a task waiting in the queue idle threads to execute.

2, newSingleThreadExecutor () method: create a thread pool thread, if idle is executed, if there is no idle thread is suspended in the task queue.

3, newCachedThreadPool () method returns a can be adjusted according to the actual number of threads in the thread pool, does not limit the maximum number of threads, if the task is to create a thread, the thread is not created if no task. If there is no task to recover the thread automatically (idle time 60s) after 60s.

4, newScheduledThreadPool () method, which returns a SchededExecutorService object, but the number of threads of thread pool can be specified.

Several methods are used ThreadPoolExecutor class, which can be custom thread pool. Configuration as follows:

 

Second, a custom thread pool

 When using this class ThreadPoolExecutor custom thread pool, this construction method is more crucial for the queue what type:

When using bounded queue: If a new task needs to be performed, if the actual number of threads in the thread pool is less than corePoolSize, then create a thread priority, if more than corePoolSize, the task will be added to the queue, if the queue is full, the total thread under no greater than the number of maximumPoolSize the premise of creating a new thread, if more than maximumPoolSize, refused to execute strategy. Or other custom way.

In an unbounded queue: LinkedBlockingQueue. Compared with the bounded queue until system resources are exhausted, or unbounded task queue does not fail if the task team into existence. When a new job arrives, the system is less than the number of threads corePoolSize, the new thread to perform the task. After reaching corePoolSize, we will not continue to increase. If follow-up is still adding new tasks, and there is no idle thread resources, the task directly into the waiting queue. If the task of creating and processing speed is very different, unbounded queue will maintain rapid growth, knowing that deplete the system memory.

JDK denial strategy:

AbortPolicy: Direct thrown organization system is working properly.

CallerRunsPolicy: As long as the thread pool is not closed, the policy directly in the caller's thread, run the current task is discarded.

DiscardOldestPolicy: discard the oldest request, try to submit the current job again.

DiscardPolicy: discard task can not handle, do not give any treatment.

Custom refused strategy: to achieve RejectExecutiionHandler interface.

 

Guess you like

Origin www.cnblogs.com/dwxblogs/p/10939599.html