Recently made a Java SE program. Use the thread pool to handle high concurrency tasks. There are query database operations during task processing, which results in very slow task handling. Then find the information on the Internet. The final solution is now to summarize the relevant considerations. First, on the thread pool considerations. For multiple tasks, the thread pool is best not to be public. The thread pool should be separated from the code for the thread pool
</pre><pre code_snippet_id= "547077" snippet_file_name= "blog_20141209_1_7940215" name= "code" class= "Java" >import Java.util.concurrent.executorservice;import Java.util.concurrent.linkedblockingqueue;import Java.util.concurrent.threadpoolexecutor;import Java.util.concurrent.timeunit;import Org.slf4j.Logger;import Org.slf4j.loggerfactory;public class Changethreadpool {private static Logger Logger = Loggerfactory.getlogger ( Changethreadpool.class);p rivate static Threadpoolexecutor executor = null; Declares a thread pool private static int coresize = 100; Number of core threads private static int maxSize = 150; Maximum number of threads private static final int keep_alive_time = 60; Life cycle public Changethreadpool () {}public synchronized void Syncinit () {executor = new Threadpoolexecutor (coresize, Maxsi Ze, keep_alive_time,timeunit.seconds, New linkedblockingqueue<runnable> ());} public static Executorservice getinstance () {if (executor = = null) {Logger.debug ("The >>>threadpool thread pool is empty! "); Changethreadpool pool = new ChangEthreadpool ();p ool.syncinit ();} return executor;}}
Second, database considerations
(1) For frequently found database operations preferably with cache, according to the actual situation must query the database, you must find
(2) The most important point is that the database CPU usage has been in the 100% situation. High concurrency causes the database CPU to persist because of frequent querying of the database. This situation is most likely caused by the absence of an index in the query statement, so you should check that all of your query statements have an index added to the Where condition. Adding an index will basically solve the problem.
Personal summary. Lest we write a similar program later and forget
Considerations for high concurrency for Java SE programs