& Cache buffer understand the concept of objects & pool

A), buffer

作用:缓解程序上下层之间的性能差异。

1)、当上层组件的性能优于下层组件时加入缓冲机制可以减少上层组件对下

       层组件的等待时间。

2)、上层组件不需要等待下层组件接收全部数据,即可返回操作,加快了上

        层组件的处理速度。

缓冲的应用:

JDK的I/O组件提供了缓冲功能。

例:当对文件进行写操作时需要从内存中读取数据写入到硬盘,此时,文件从

    内存中的读取速度比写入硬盘的速度快,此时就出现了上层组件的性能优

    于下层组件的情况,此时可以加入缓冲机制BufferInputStream()先将数据

    写入缓冲区,再将数据写道硬盘中。

B) cache

定义:是一块为提高系统性能的内存空间。

作用:暂存数据处理结果,并提供下次访问使用。

应用场景:

数据处理或数据获取可能会非常耗时,当数据的请求量很大时,频繁的数据处

理会耗尽cpu资源。

简单缓存的实现:直接使用HashMap

弊端:

1)、不知道应该何时清理无效数据。

2)、如何防止数据过多导致内存溢出。

基于java的缓存框架:
  1. EHCache: Hibernate default data caching solution.
  2. OSCache: OpenSymphony by the design, can cache any object, even a cache
    of the JSP page, or Http request.
  3. JBossCache: a JBoss developed, can be used for inter-cluster JBoss frame buffer data sharing.

    Cache application: It is recommended adding cache frequently-used functions and heavy loads in order to improve the performance of its frequent calls can be constructed based on key parameters passed.

    Dynamic proxy caching solution:

     1).在方法中加入缓存的弊端:缓存组件和业务层代码紧密耦合,依赖性强。

    2) use dynamic caching proxy to achieve the benefits: In the business layer, without paying attention to the operation of the cache, the code is completely independent and isolated, to a new function method, add the cache will not affect the implementation of the original method.

Three), object reuse pool

Pooling of: if a class is requested frequently used, then the instances of this class is stored in a tank, in use, directly from the pool.

Pooling can be an array, a linked list or any collection class.

Pooling of applications:

1), the thread pool:

2), a database connection pool

Data connections are widely used cell module:

1), c3p0 connection pool 2), ProXool connection pool

c3p0 achieve connection pool:

Connection con = pool.getConnection()

con class type: com.mchange.V2.c3p0.impl.NewProxyConnection

1), via a data connection pool object calls getConnection objects database connection, a Connection object NewProxyConnection proxy object obtained at this time.

2), connected to the actual objects in the database in an internal Connection to connect the acquired class object

    Object re = f.get()

    re的类类型:com.msql.jdbc.JDBC4Connection

 Field  f =  con.getClass().getDeclaredFiled("inner") //通过反射获取内部类对象
 Object re = f.get() //获取实际的数据库连接

Conclusion: When you call con.close () does not really connected to close the database, but the database connection into the pool to save the database connection when the user invokes getConnection second time from the connection pool to obtain a connection object.

Four), Jakarta commons Pool target cell module

Jakarta commons Poll target cell module provided by the Apache.

It defines two interfaces:

ObjectPool (object pooling Interface)

public interface ObjectPool{
    //从对象池中取一个对象
    T boorowObject();
    //将对象返回给对象池
    void returnObject(T boorowed);
}

PoolableObjectFactory: object pool management facility

public interface PoolableObjectFactory{
    //创建对象,若对象池没有对象,在borrowObject()时会自动调用
    T MakeObject();
    //在创建对象前使用,激活对象
    void activateObject(T Object);
    //在对象返回对象池时使用
    void passivateObject(T Object);
    //判断对象是否可用
    void validateObject(T Object);
    //pool.close()对象从对象池中销毁使用
    void destoryObject(T Object);
}

Built-in defines three objects pool

1)、StackObjectPool

使用java.util.Stack来保存对象,可以指定初始化大小,当空间不够时,可以自

     动增长,无可用对象时,可以自动创建新对象。

2)、GenericObjectPool

通用对象池,可以设定对象池的容量,可设定无可用对象时,对象池的表现行

    为(等待或创建新实例),可以设置对象有效性检查

3), softReferenceObjectPool

使用ArrayList保存对象,不保存对象的强引用,只保存对象的弱引用,对对象

数量没有限制,当没有对象时会自动创建对象,当内存紧张时,Jvm自动回收

具有弱引用对象。

Guess you like

Origin www.cnblogs.com/Auge/p/11641421.html