如何创建对象池以能够借用和返回对象

如何创建对象池以能够借用和返回对象

本文介绍了如何创建对象池以能够借用和返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否可以创建对象池?这样我就可以从池中取出一个对象,一旦完成工作,就可以将其放入池中.

I wanted to know that, is it possible to create a pool of objects? So that I can take an object from the pool and once I'm done with the work, I can put it into the pool.

推荐答案

可能是的.如果新对象的构建很昂贵(例如建立数据库连接),或者由于其他原因GC带宽太高(在Android领域通常会出现问题),则可以在许多情况下看到性能提高.

It is possible yes. You can see performance improvements in many situations if the construction of a new object is expensive (like establishing a database connection) or if for other reasons the GC bandwidth is too high (often a problem in Android-land).

以下是一些可用于实现池的资源.您也许可以立即使用Apache的ObjectPool.

Here are some resources that you could use to implement your pool. You may be able to use Apache's ObjectPool right out of the box.

  • Apache Commons ObjectPool
  • Does this basic Java object pool work?
  • Build your own ObjectPool in Java to boost app speed
  • Object Pool Design Pattern in Java
  • Google search: java object pool

这篇关于如何创建对象池以能够借用和返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:28