reduces resource consumption by avoiding resource consumption that frequently creates and destroys threads;
Improve Speed: When a new task arrives, you do not have to create a new thread every time you can execute it immediately;
improves the manageability of threads: the thread pool distributes, tuned, and monitored threads uniformly. Unrestricted creation of threads is not allowed. The realization principle of line Cheng code when the thread pool receives a new commit task, how the thread pool handles the new task, which mainly learns the thread pool's processing flow for the new task. The number of currently running threads is less than corepoolsize, creating a new thread to perform the task, which requires acquiring a global lock;
the number of threads running is equal to or greater than corepoolsize, the task is added to the blockingqueue;
if the blockingqueue is full, you cannot join and create a new thread to handle the task;
If the new thread causes the currently running thread to exceed the maxiumpoolsize, the task takes the appropriate measurement policy for processing; the creation of a thread pool uses Threadpoolexecutor to create a thread pool: parsing of several key parameters: Corepoolsize: Can be understood as the core thread pool size, the thread pool will usually create and start the basic thread in advance, when the number of threads is less than corepoolsize, the commit new task creates a thread, even if other threads in the thread pool are idle; Maximumpoolsize: The maximum number of threads that the thread pool allows to create, which is the upper bound of the number of threads, no more. When you are new to a task, if the number of threads in the current thread pool is greater than corepoolsize and less than Maximumpoolsize will add the task to the task queue for execution, and if the task queue is infinite, then the maximumpoolsize parameter has no practical meaning. KeepAliveTime: The pool is free to stay alive after a thread of work. Time can be increased for tasks that are more numerous and have a short execution time. Increase thread pool efficiency. Timeunit: The unit of thread activity holding time, days, HOURS, minutes, etc. Blockingqueue: Task queue, which holds blocking queues for tasks waiting to be performed, has the following blocking queues: Arrayblockingqueue: Based on Array, FIFO first-in first out principle;
Linkedblockingqueue: Based on the list, FIFO advanced first out principle, high throughput;
Synchronousqueue: Each insert operation must wait for another removal operation to complete, otherwise the insert operation is blocked and the throughput is higher than the linkedblockingqueue;
Priorityblockingqueue: A priority wireless blocking queue; Threadfactory: The thread factory that sets a more meaningful name for each created thread; Rejectedexecutionhandler: When a task queue is full , the thread pool is also saturated, and a rejection policy must be used to handle the newly committed task. The usual policy approach is as follows: AbortPolicy: Throw the exception directly, the default is this strategy;
Callerrunspolicy: Only the caller is running the task on the thread;
Discardoldestpolicy: Discards the most recent task in the queue and executes the current task;
Discardpolicy: Do not Dispose, discard; You can also submit a task to the thread pool by implementing the Rejectedexecutionhandler interface customization policy you can submit a task to the thread pool using execute () and submit () two methods; Closes the thread pool call thread pool by invoking the shutdown and Shutdownnow methods of the thread pool. The principle is to traverse the worker thread in the thread pool and invoke the interrupt method to interrupt each. The Shutdown method sets the state of the thread pool to shutdown and then interrupts the task thread that is not executing; shutdownnow sets the thread pool to stop and attempts to interrupt all threads.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.