Advantages of using thread pools
- Reduce resource consumption: Reuse threads to reduce resource consumption by thread creation and destruction.
- Improved responsiveness: When tasks arrive, they can be executed immediately without waiting for threads to be created.
- Improve thread manageability: Threads are scarce resources. Unlimited creation of threads consumes system resources and reduces the stability of the system. Thread pools can be used for uniform allocation, tuning and monitoring.
Thread pool parameters
2. Maximum number of threads 3. Thread retention time exceeding the number of core threads 4. Unit of thread holding time 5. Queue holding waiting threads 6. Thread creation factory 7. Thread saturation policy.
How a three-thread pool works
When a new task arrives, first determine whether the worker thread in the thread has reached the core number of threads, if not, directly create a thread to execute the task. If the core thread count is full and the queue is not full, the task is added to the wait queue. If the wait queue is full and the maximum number of threads has not been reached, the thread is created and the task is executed. If the waiting queue is full and the maximum number of threads has been hit, the saturation policy is triggered
Fourth, what are the saturation strategies
• ThreadPoolExecutor. AbortPolicy: throw RejectedExecutionException to reject the processing of a new task. • ThreadPoolExecutor. CallerRunsPolicy: call own thread running tasks, which is directly in the call the execute method of the thread run (run) rejected task, if the executable program has been closed, will be discarded the task. Therefore, this strategy will slow down the speed of new task submission and affect the overall performance of the program. You can choose this strategy if your application can withstand the delay and you require that any task request be executed. Executes the task on the current calling thread. Delay in handling. • ThreadPoolExecutor. DiscardPolicy: does not handle the new task, discarded directly. • ThreadPoolExecutor. DiscardOldestPolicy: this policy will discard the first requests pending tasks. The default refused to strategy is to raise RejectedExecutionException AbortPolicy.
V. Some Contrasts
Runnable does not return a result or throw an exception. Callable can. 2.execute() Vs submit(); Execute () commits a task that does not require a return value. Submit () needs to return the worth task. Returns the Future
object. You can call future.get() to get the result, and the get() method blocks the current thread. The get (long timeout, timeUnit) method can block for a certain amount of time and then return, but the thread may not have finished and will not get the result. The state of the thread pool is shutdown. No new tasks are accepted, but it waits for the tasks in the queue to complete. ShutdownNow: ShutdownNow closes the thread pool. The thread pool status changes to STOP, which terminates the running task, stops processing queued tasks, and returns the waiting List. 4.isTerminated() Vs isShutDown(); Isshutdown returns true after the call to shutdown; IsTerminated (), which does not return true until all tasks complete when shutdown is called.
Six common thread pools and problems
1.FixedThreadPool fixes the thread pool. The number of core threads is equal to the maximum number of threads, and the number of unbounded queue Integer.max_value tasks may be more than OOM. The number of core threads and the maximum number of threads is 1. Unbounded queue tasks may be more OOM. Core number of threads 0 Maximum number of threads Integer.MAX_VALUE Synchronous Queue Too many threads
Size Settings for the number of core threads
Computation-intensive: number of CPUs +1. I/O intensive: 2 times the number of CPUs.