Object Pooling in Java

后端 未结 7 1719
鱼传尺愫
鱼传尺愫 2020-12-29 08:19

What are the pro\'s and con\'s of maintaining a pool of frequently used objects and grab one from the pool instead of creating a new one. Something like string interning exc

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 08:42

    I agree with Jon Skeet's points, if you don't have a specific reason to create a pool of objects, I wouldn't bother.

    There are some situations when a pool is really helpful/necessary though. If you have a resource that is expensive to create, but can be reused (such as a database connection), it might make sense to use a pool. Also, in the case of database connections, a pool is useful for preventing your apps from opening too many concurrent connections to the database.

提交回复
热议问题