Precautions for high concurrency in java SE Program
Recently I made a java SE program. Use the thread pool to process highly concurrent tasks. Database query operations are performed during task processing, resulting in slow task processing. So I searched for information online. The final solution is to summarize the relevant precautions. I. Notes about thread pool. For multiple tasks, the thread pool should not be public. Separate the thread pool from the code about the thread pool.
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); private static ThreadPoolExecutor executor = null; // declare 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, maxSize, KEEP_ALIVE_TIME, TimeUnit. SECONDS, new parameter blockingqueue
();} Public static ExecutorService getInstance () {if (executor = null) {logger. debug (">>> threadPool thread pool is empty! "); ChangeThreadPool pool = new ChangeThreadPool (); pool. syncInit ();} return executor ;}}
Ii. Database considerations
(1) it is best to add a cache for operations that frequently query the database. If you need to query the database based on the actual situation, you must query the database.
(2) The most important thing is that the cpu usage of the database has been 100%. Because of high concurrency, frequent database queries result in high database cpu usage. This is probably because no index is added to the query statement. Therefore, check whether the where condition of all your query statements has been indexed. After adding an index, you can solve the problem.
Summary. So that you do not forget to write similar programs in the future.