Java 中的对象池替代方案
I know what is Object Pooling in Java
I was keen to know if there are any better alternatives which can be used in an distributed environment, or specifically with server sockets and database connection sockets.
Any information or leads would be helpful.
Thanks,
Ivard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,对象池在 Java 中不再是一个好主意,因为在最近的 JVM 版本中,对象创建(和 GC)非常便宜且安全(比尝试在并发环境中使对象池线程安全要安全得多......)。
仅当特定对象的创建成本非常高和/或消耗大量资源时才合理,例如 连接池化。您链接的线程包含一些有用的解决方案建议(但是,由于缺乏个人经验,我不能推荐其中任何一个)。
Object pooling in general is not a good idea in Java anymore, as in recent JVM versions object creation (and GC) is extremely cheap and safe (much safer than trying to make an object pool threadsafe in a concurrent environment...).
It is only reasonable in case the specific objects are very costly to create and/or consume lots of resources, such as connection pooling. The thread you linked contains some useful looking suggestions for solutions (however, lacking personal experience, I can't recommend any of these).
分布式环境的更好替代方案是使用像 JBoss AS 这样的应用程序服务器来处理您的池化需求
我确信(但缺乏经验)Spring 框架提供了类似的功能能力。
A better alternative for a distributed environment could be using an application server like JBoss AS to have your pooling needs handled for you.
I am sure (but lack experience with) that the Spring framework provides similar capabilities.
在某些应用程序服务器(例如 JBoss)中,使用 JCA< /a> 控制数据库连接和套接字池。
In some application server, such as JBoss, uses JCA to control database connection and socket pooling.