Precautions for high concurrency in java SE Program

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.