Thinking?
1 Why use actuators and thread pools?
2 What are actuators and threads? How to use
Actuator
Thread executor separates the creation and execution of tasks, improves the performance of threads
Thread pool
Avoids the frequent creation and destruction of threads, the reuse of thread objects, and the flexibility to control the number of concurrent items based on the project
Executorservice (java.util.concurrent)
1 executors.newcachedthreadpool () cache thread pool, flexible reclamation of idle threads if the thread pool is longer than required for processing, new thread if not recyclable
2 executors.newfixedthreadpool () fixed thread pool, which controls the maximum number of concurrent threads, and the excess threads wait in the queue
3 executors.newscheduledthreadpool () fixed thread pool to support timed and recurring task execution
4 Executors.newsinglethreadexecutor () Single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO,LIFO, priority)
Execution method The difference between submit and Execute
1 The received parameters are different. The submit parameter is callable, and the Execute parameter is runnable
2 Submit there is a return value, you can know the result of the execution of the thread, for example: 2.1
2.1 Future<string> Future = Excutorservice.submit (Task) future.get ();//Results of print thread execution
The difference between callable and runable
Callable has a return value, Runable no return value
The difference between shutdown and Shutdownnow ()
1 shutdown () method allows previously submitted tasks to be performed before terminating
The 2 Shutdownnow () method prevents the waiting task from starting and attempts to stop the task that is currently performing normally
How to properly configure thread pool size (check the nature of tasks, execution time, and dependencies)
1 Determine the nature of the task (CPU intensive?) IO-intensive? Mixed-type tasks? )
2 Determining the execution time of a task (long?) Short? Medium?)
3 Determine the dependency of the task, whether it depends on other system resources (data connection?)
Thread pool Configuration Scenario Reference
1 CPU-intensive tasks should be configured with as few threads as possible (context switch consumption) number of CPUs +1
2 IO-intensive tasks should be configured with as many threads as possible (IO operations do not consume CPU) twice times the number of CPUs +1
3 If you rely on other resources, the longer the wait, the longer the CPU idle time, the larger the number of threads should be set
4 The higher the percentage of thread wait time, the more threads are required, the higher the percentage of thread CPU time, the fewer threads required
JAVA-Concurrent programming-actuators and thread pools