Java thread pool-related issues

Thread Pool deny policy

  • AbortPolicy : Direct thrown prevent the system from 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 can not handle the task, not given any treatment.
  • If you want to customize the interface to reject strategy can achieve RejectedExecutionHandler

How to Use a good thread pool

The number of thread size settings

Compute-intensive

  • As the name suggests is the application requires a lot of computing resources CPU, multi-core CPU era, we have to let each CPU core are involved in the calculation, the CPU performance fully utilized, this is not considered a waste of server configuration, if you configure a very good server also running on a single-threaded program that will be just as big a waste
  • For compute-intensive applications, to work entirely on the number of core CPU, so in order to make it fully play out the advantages, avoid excessive thread context switching, the ideal solution is: the number of threads = number +1 CPU core , also can be set to CPU core number * 2 , but it depends on the JDK version, and CPU configurations (server's CPU has Hyper-Threading)

IO-intensive

  • It is well understood, and most of all we are doing to develop WEB applications, involving large amounts of network traffic, not only that, with the database, interaction between the cache and also involves IO, IO once occurred, in a thread will wait state, when the IO end, the data is ready, the thread will continue
  • Therefore, can be found here for IO intensive applications, we can set the number of multi-threaded some of the thread pool, so that we can make at this time to wait for IO, the thread can do other things, to improve the efficiency of concurrent processing. Then the amount of data that is not thread pool can easily set it? Of course not, be sure to remember that thread context switching at a price
  • Currently summed up a formula for IO intensive applications: threads = CPU cores / (l blockage factor) This blockage factor is generally 0.8 to 0.9, 0.8 or 0.9 may be taken. Apply the formula for a dual-core CPU, it's the ideal number of threads is 20, of course, is not absolute, it is necessary according to the actual situation to adjust and real business: Final int the poolSize = (int) (cpuCore / (1 - 0.9) )

Thread pool configuration parameters

  • Use the thread pool when they are not selected no upper limit of Configuration Items. First, we do not to use no upper thread pool and set the unbounded queue ! For example, set the queue unbounded set newCachedThreadPool because of some unforeseen circumstances, the system thread pool will be abnormal, leading to surge threads situation or task queue continues to expand, leading to memory exhaustion system crashes and exceptions. We recommend using the first principles of a custom thread pool to avoid this problem, which is the standard in the thread pool
  • Set a reasonable number of threads, and thread idle time to recover , to be set according to the specific task execution cycles and time, avoid frequent recovery and creation , although we aim to use the thread pool is to improve system performance and throughput, but also consider under the stability of the system , otherwise unexpected problems arise will be very troublesome
  • According to the actual scene, choose suitable for their rejection policy . To compensate, not to mess with automatic compensation mechanism JDK support ! Try using custom tactics refused to reveal all the details were

Hook your use of the embedded behavior

  • Use Hook, leaving the thread pool to perform the track: ThreadPoolExecutor provides a hook method protected type can be overridden, allowing the user the task before execution and after execution to do something.
  • We can use it to achieve such initialization ThreadLocal, gather statistics, such as logging and other operations. BeforeExecute such as Hook and afterExecute.
  • There is also a Hook can be used when the task is finished executing logic allows the user to insert, as rerminated. If the hook method execution fails, the work of the internal thread will fail or be interrupted

Close the thread pool 

  • When the content of the thread pool is not cited and the number of worker threads is 0, the thread pool will be terminated. We can also call the shutdown manually terminate the thread pool . If we forget to call shutdown, in order to allow the thread resources are released, we can also use keepAliveTime and allowCoreThreadTimeOut to achieve their goals! Of course, the prudent approach is to use a virtual machine Runtime.getRuntime (). AddShutdownHook method, hand to call the close method of the thread pool

 

Published 53 original articles · won praise 1 · views 1006

Guess you like

Origin blog.csdn.net/CHYabc123456hh/article/details/104739551