1、阿姆达尔定律

  1.1 加速比=优化后的耗时/优化前的耗时

  1.2 阿姆达尔定律   s<=1/F+(1-F)/N

    其中:s为加速比,F为程序的串行化比重,n为cpu处理核数

2、调优层次(设计调优、代码调优、JVM调优、数据库调优)

  2.1 设计模式

    2.1.1 单列模式:常见应用spring注解,以及实现当前在线人数的统计

      基本要素:构造方法私有化,提供公共的获取实例的方法

public class SingleSessionUserList {

    private static final SingleSessionUserList sessionUserList=new SingleSessionUserList();

    //提供公共的方法接口
public static SingleSessionUserList getInstance() {
return sessionUserList;
} //将构造方法私有化
private SingleSessionUserList() {
}
}

    2.1.2 代理模式

      核心思想:只有在真心需要时才进行初始化,否则只返回当前对象的代理对象

      基本要素:主题接口、真实主题、代理类

/**
* IDbQuery
* @Description 主题接口
*/
public interface IDbQuery {
//比较耗时的方法
public String request();
} /**
* DbQuery
* @Description 真实主题
*/
public class DbQuery implements IDbQuery {
@Override
public String request() {
return "比较耗时的操作结果";
}
} /**
* DbQueryProxy
* @Description 代理类
*/
public class DbQueryProxy implements IDbQuery {
private DbQuery dbQuery;//真实主题
@Override
public String request() {
//真正需要使用DbQuery
if (dbQuery==null) {
dbQuery=new DbQuery();
}return dbQuery.request();
}
} /**
* ProxyTest
* @Description 代理类的测试
*/
public class ProxyTest {
@Test
public void testProxy() {
IDbQuery dbQuery=new DbQueryProxy();
dbQuery.request();
}
}

    动态代理的实现

/**
* JdkDbQueryHandler
* @Description 使用jdk反射的反射获取动态代理
*/
public class JdkDbQueryHandler implements InvocationHandler {
IDbQuery dbquery;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (dbquery == null) {
dbquery = new DbQuery();
}
//调用真实主题的方法
return method.invoke(dbquery, args);
} //获取动态代理对象
public static IDbQuery getJdkDbQueryProxy() {
IDbQuery jdkDbQueryProxy=(IDbQuery) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
new Class[] { IDbQuery.class }, new JdkDbQueryHandler());
return jdkDbQueryProxy;
}
} /**
* CglibDbQueryHandler
* @Description 使用cglib创建动态代理
*/
public class CglibDbQueryHandler implements MethodInterceptor {
private DbQuery dbQuery;
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
if (dbQuery==null) {
dbQuery=new DbQuery();
}
//调用真实主题的方法
return method.invoke(dbQuery, args);
} //cglib方法获取动态代理对象
public static IDbQuery getCglibDbQueryProxy() {
Enhancer enhancer=new Enhancer();
enhancer.setCallback(new CglibDbQueryHandler());
enhancer.setInterfaces(new Class[]{IDbQuery.class});
return (IDbQuery) enhancer.create();
}
}

    2.1.3 享元模式

      核心思想:如果系统中多次使用相同的对象,每次只需要获取对象的拷贝,不需要重新创建一个新的对象

/**
* IReportManager
* @Description 报表管理器
*/
public interface IReportManager {
//生成报表
public String createReport();
} /**
* FinancialReportManager
* @Description 财务报表生成器实现类
*/
public class FinancialReportManager implements IReportManager {
@Override
public String createReport() {
return "财务报表";
}
} /**
* EmployeeReportManager
* @Description 员工报表生成器
*/
public class EmployeeReportManager implements IReportManager {
@Override
public String createReport() {
return "员工报表";
}
} /**
* ReportManagerFactory
* @Description 报表生成器工厂
*/
public class ReportManagerFactory {
Map<String, IReportManager> financialReportManagers=new HashMap<String, IReportManager>();
Map<String, IReportManager> employeeReportManagers=new HashMap<String, IReportManager>(); public IReportManager getFinancialReportManager(String tid) {
IReportManager financialReportManager=financialReportManagers.get(tid);
if (financialReportManager==null) {
financialReportManager=new FinancialReportManager();
financialReportManagers.put(tid, financialReportManager);
}
return financialReportManager;
} public IReportManager getEmployeeReportManager(String tid) {
IReportManager employeeReportManager=employeeReportManagers.get(tid);
if (employeeReportManager==null) {
employeeReportManager=new EmployeeReportManager();
employeeReportManagers.put(tid, employeeReportManager);
}
return employeeReportManager;
}
} /**
* ReportManagerFactoryTest
* @Description 享元模式测试类
*/
public class ReportManagerFactoryTest {
@Test
public void testCreateReport() {
ReportManagerFactory factory=new ReportManagerFactory();
IReportManager reportManager1=factory.getEmployeeReportManager("a");
IReportManager reportManager2=factory.getEmployeeReportManager("a");
IReportManager reportManager3=factory.getEmployeeReportManager("a");
IReportManager reportManager4=factory.getFinancialReportManager("a");
IReportManager reportManager5=factory.getFinancialReportManager("a");
IReportManager reportManager6=factory.getFinancialReportManager("a");
System.out.println(reportManager1.createReport());
System.out.println(reportManager2.createReport());
System.out.println(reportManager3.createReport());
System.out.println(reportManager4.createReport());
System.out.println(reportManager5.createReport());
System.out.println(reportManager6.createReport());
}
}

    2.1.4 装饰模式

/**
* IPacketCreator
* @Description 内容处理接口
*/
public interface IPacketCreator {
public String handContent();
} /**
* PacketCreator
* @Description 内容处理具体实现
*/
public class PacketCreator implements IPacketCreator {
@Override
public String handContent() {
return "装饰模式";
}
} /**
* PacketDecorator
* @Description 装饰器
*/
public abstract class PacketDecorator implements IPacketCreator {
IPacketCreator packetCreator;
public PacketDecorator(IPacketCreator packetCreator) {
this.packetCreator = packetCreator;
}
} /**
* PacketHtmlDecorator
* @Description 功能详细描述
*/
public class PacketHtmlDecorator extends PacketDecorator {
public PacketHtmlDecorator(IPacketCreator packetCreator) {
super(packetCreator);
}
@Override
public String handContent() {
StringBuffer buffer=new StringBuffer("<html>");
buffer.append(packetCreator.handContent()).append("</hmtl>");
return buffer.toString();
}
} /**
* DecoratorTest
* @Description 装饰模式测试类
*/
public class DecoratorTest {
@Test
public void decoratorTest() {
String content=new PacketHtmlDecorator(new PacketCreator()).handContent();
System.out.println(content);
}
}

  2.2 使用缓冲区提高性能

    尽量使用含有缓冲区的类进行操作,如stringBuffer,BufferInputStream等

  2.3 使用缓冲技术提升性能

    1.使用oscache缓存jsp局部缓冲

     1.1 导入oscache的相关jar包

<dependency>
<groupId>opensymphony</groupId>
<artifactId>oscache</artifactId>
<version>2.4.1</version>
</dependency>

     1.2 将oscache.properties文件放入到resource目录下

     1.3 在jsp页面中导入lib文件  <%@taglib prefix="oscache" uri="http://www.opensymphony.com/oscache" %>

     1.4 将需要缓存的代码使用<oscache:cache></oscache:cache>标签进行包裹

<oscache:cache key="${param.refresh }"  refresh="${param.refresh }">
<div>登录时间<%=new Date() %></div>
</oscache:cache>

    2.使用oscache全局缓冲

     2.1 在web.xml配置oscacheFilter过滤器  

<filter>
<filter-name>oscacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>600</param-value>p
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>oscacheFilter</filter-name>
  <!--缓存的路径-->
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

  

      

java性能调优01的更多相关文章

  1. Java性能调优笔记

    Java性能调优笔记 调优步骤:衡量系统现状.设定调优目标.寻找性能瓶颈.性能调优.衡量是否到达目标(如果未到达目标,需重新寻找性能瓶颈).性能调优结束. 寻找性能瓶颈 性能瓶颈的表象:资源消耗过多. ...

  2. Java性能调优(一):调优的流程和程序性能分析

     https://blog.csdn.net/Oeljeklaus/article/details/80656732 Java性能调优 随着应用的数据量不断的增加,系统的反应一般会越来越慢,这个时候我 ...

  3. Java性能调优:利用JMC分析性能

    Java性能调优作为大型分布式系统提供高性能服务的必修课,其重要性不言而喻. 好的分析工具能起到事半功倍的效果,利用分析利器JMC.JFR,可以实现性能问题的准确定位. 本文主要阐述如何利用JMC分析 ...

  4. Java性能调优:利用JFR生成性能日志

    Java性能调优作为大型分布式系统提供高性能服务的必修课,其重要性不言而喻. 好的分析工具能起到事半功倍的效果,利用分析利器JMC.JFR,可以实现性能问题的准确定位. 本文主要阐述如何利用JFR生成 ...

  5. 第六章 Java性能调优工具(待续)

    Java性能调优工具 Windows工具 JDK命令行工具 JConsole工具 Visual VM多合一工具 Visual VM对QQL的支持 MAT内存分析工具 MAT对QQL的支持 JProfi ...

  6. java 性能调优和GC

    JAVA 性能调优和GC http://blog.csdn.net/gzh0222/article/details/7663181 JAVA GC调优手记 http://blog.csdn.net/f ...

  7. Java性能调优概述

    目录 Java性能调优概述 性能优化有风险和弊端,性能调优必须有明确的目标,不要为了调优而调优!!!盲目调优,风险远大于收益!!! 程序性能的主要表现点 执行速度:程序的反映是否迅速,响应时间是否足够 ...

  8. Java性能调优攻略全分享,5步搞定!(附超全技能图谱)

    对于很多研发人员来说,Java 性能调优都是很头疼的问题,为什么这么说?如今,一个简单的系统就囊括了应用程序.数据库.容器.操作系统.网络等技术,线上一旦出现性能问题,就可能要你协调多方面组件去进行优 ...

  9. Java性能调优实战,覆盖80%以上调优场景

    Java 性能调优对于每一个奋战在开发一线的技术人来说,随着系统访问量的增加.代码的臃肿,各种性能问题便会层出不穷. 日渐复杂的系统,错综复杂的性能调优,都对Java工程师的技术广度和技术深度提出了更 ...

随机推荐

  1. log4j/slf4j

    log4j的使用 引入log4j.jar包 <dependency> <groupId>log4j</groupId> <artifactId>log4 ...

  2. Flink分布式缓存Distributed Cache

    1 分布式缓存 Flink提供了一个分布式缓存,类似于hadoop,可以使用户在并行函数中很方便的读取本地文件,并把它放在taskmanager节点中,防止task重复拉取. 此缓存的工作机制如下:程 ...

  3. Unrecognized SSL message, plaintext connection--SSLSocket 代理服务器连接

    虽然java代码  URL.openconnect(proxy);已经实现了https客户端通过代理连接服务器 但个人在使用socket https代理http://www.cnblogs.com/h ...

  4. k8s进行与容器交互时报错:unable to upgrade connection: Unauthorized在k8s实现kubectl exec -it pod_ID sh出错解决

    在创建pod时,进入pod失败 kubectl exec -it nginx-deployment-d55b94fd-xcrtg sh error检查问题,一直找不到答案,通过logs发现,同样不能实 ...

  5. bzoj4843 [Neerc2016]Expect to Wait

    [Neerc2016]Expect to Wait Time Limit: 10 Sec Memory Limit: 128 MB Description ls最近开了一家图书馆,大家听说是ls开的, ...

  6. Cas 使用maven的overlay搭建开发环境 (二)

    关于cas-server的安装.部署网上教程很多.但是使用Cas,只通过部署时修改配置是无法满足产品需求的,因此需要我们改造Cas.本文讲解如何使用maven的overlay无侵入的改造Cas. 什么 ...

  7. C常量与变量

    /** * C中的常量与变量 * 常量的值在程序中是不可变化的,其在定义时必须给一个初始值 * 常量的定义方式: * 1.#define 定义宏常量 * 2.const 定义const常量 * 对于# ...

  8. 2018-10-8-如何安装-btsync

    title author date CreateTime categories 如何安装 btsync lindexi 2018-10-8 9:15:6 +0800 2018-2-13 17:23:3 ...

  9. C++的命名空间

    作用:防止类,函数,变量等之间重名,比如在代码合并的时候 假如两个头文件中均定义了类Cal,而调用程序同时包含了两个头文件,当在定义Cal c时,程序会报类型重定义的错误.这种问题可以通过命名空间来解 ...

  10. 【串线篇】实现一个RestfulCRUD

    一.概述 利用SpringMVC做一个CRUD(增删改查)符合Rest风格的: C:Create:创建 R:Retrieve:查询 U:Update:更新 D:Delete:删除 <%@tagl ...