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. JVM运行时区域详解。

    我们知道的JVM内存区域有:堆和栈,这是一种泛的分法,也是按运行时区域的一种分法,堆是所有线程共享的一块区域,而栈是线程隔离的,每个线程互不共享. 线程不共享区域 每个线程的数据区域包括程序计数器.虚 ...

  2. 61.Merge k Sorted Lists(合并k个排序链表)

    Level:   Hard 题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and descri ...

  3. Codeforces The Child and Toy

    The Child and Toy time limit per test1 second On Children's Day, the child got a toy from Delayyy as ...

  4. 向服务器发送post请求

    /** * 通过HttpClient发送Post请求 * @param path 请求路径 * @param params 请求参数 * @param encoding 编码 * @return 请求 ...

  5. SQL数据库—<6>存储过程

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  6. 【知识强化】第六章 查找 6.3 B树和B+树

    本节课我们来学习本章的第一个难点,就是B树.那么B树它其实是一种数据结构,我们设计出这种数据结构就是为了提高我们的查找效率的,提高我们在磁盘上的查找效率.那么什么是B树呢?了解B树之前,我们先来回忆一 ...

  7. SHOW - 显示运行时参数的数值

    SYNOPSIS SHOW name SHOW ALL DESCRIPTION 描述 SHOW 将显示当前运行时参数的数值. 这些变量可以通过 SET 语句来设置,或者通过编辑 postgresql. ...

  8. ida吧

    经过IDA反编译后的代码是:int __cdecl Ompress(void *Dst, int a2, int a3, int a4)//dst( [esp+24h][ebp+4h] );a2([e ...

  9. python-django_rest_framework中的request/Response

    rest_framework中的request是被rest_framework再次封装过的,并在原request上添加了许多别的属性: (原Django中的request可用request._requ ...

  10. Tmux 中开启鼠标选择与复制

    在 tmux.conf 中加入下列设置(tmux 2.1 之前的版本): set -g mode-mouse on set -g mouse-resize-pane on set -g mouse-s ...