java 性能调优总结

1,jenkin 构建脚本(cpu, 内存)配置是否符合要求,比如1core 3G
2,tomcat线程池是否配置恰当 3,数据库是否启用第三方的数据库连接池
4,对http请求几乎不变的返回是否有缓存
5,java代码优化(日志使用模式填充,查询时能少查字段就少查,复杂处理可以使用线程分离出来)
6,logback日志是否启用异步打印,日志级别很重要,特别是在日志输出多时一定要调整级别为ERROR
7,实在特别复杂的操作抽出来用java线程池(ExecutorService)做

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExtThreadPoolDemo {
    public static class MyTask implements Runnable{
    	Subject a;
    	public MyTask(Subject ai){
    		this.a = ai;
    	}
        public void run() {
            System.out.println("Thread Name:" + Thread.currentThread().getName() + "|	" + a.toString());
 
        }
    }
    public static class Subject{
    	private int a;
    	private String b;
    	
    	public Subject(int ai, String bi){
    		this.a = ai;
    		this.b = bi;
    	}
    	@Override
    	public String toString() {
    		return b + "-" + a;
    	}
    }
 
    public static void main(String[] args) throws InterruptedException {
    	long now = System.currentTimeMillis();
    	ExtThreadPoolDemo.MyTask myTask = null;
        ExecutorService es = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(95)){
            @Override
            protected void beforeExecute(Thread t, Runnable r) {
            }
 
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
            }
 
            @Override
            protected void terminated() {
            }
        };
        for (int i = 0; i < 100 ; i++) {
        	myTask = new MyTask(new Subject(i,"hs pool^"+i));
            es.execute(myTask);
        }
        es.shutdown();
        long time1 =  (System.currentTimeMillis()  - now);
        System.out.println("time1 :"+time1);
        
        
        
        
//        now = System.currentTimeMillis();
//        for (int i = 0; i < 100 ; i++) {
//        	new MyTask(new Subject(i,"no pool ^"+i)).run();
//        }
//        long time2= System.currentTimeMillis()  - now;
//        System.out.println("time2 :"+ time2);
//        
//        System.out.println(time2-time1);
    }
}

spring boot 中使用线程池

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class GeoFenceThreadPool {
    
    public static ThreadPoolExecutor EXECUTOR;
    

    @Autowired
    public void setSpeedServiceImpl(
            @Value("${local.java.pool.corePoolSize:60}")int corePoolSize,
            @Value("${local.java.pool.maximumPoolSize:200}")int maximumPoolSize,
            @Value("${local.java.pool.keepAliveTime:1500}")long keepAliveTime,
            @Value("${local.java.pool.workQueueSize:3000}")int workQueueSize) {
        GeoFenceThreadPool.EXECUTOR = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(workQueueSize));
    }

    
    @PreDestroy
    public void shutdownPool() {
        log.info("GeoFenceThreadPool shutdown");
        EXECUTOR.shutdown();
    }

}

常用的java命令

转载自:

https://blog.csdn.net/wm5920/article/details/84986611

https://www.cnblogs.com/sxdcgaq8080/p/11089841.html


查看java进程 : jps
监控jvm,每5秒打印一次: jstat -gc 24019 5000
查看进程运行时间:ps -eo pid,tty,user,comm,lstart,etime | grep 24019
说明 S0C:第一个幸存区的大小 S1C:第二个幸存区的大小 S0U:第一个幸存区的使用大小 S1U:第二个幸存区的使用大小 EC:伊甸园区的大小 EU:伊甸园区的使用大小 OC:老年代大小 OU:老年代使用大小 MC:方法区大小 MU:方法区使用大小 CCSC:压缩类空间大小 CCSU:压缩类空间使用大小 YGC:年轻代垃圾回收次数 YGCT:年轻代垃圾回收消耗时间 FGC:老年代垃圾回收次数 FGCT:老年代垃圾回收消耗时间 GCT:垃圾回收消耗总时间

原文地址:https://www.cnblogs.com/bigjor/p/14049577.html