开发者

Populating an object pool on JAR load

I have a core JAR that provides some functionality and hence used in lots of other apps as well - desktop apps, web apps etc. Now this core JAR maintains an object pool containing objects that are computationally expensive to create. The object pool is of fixed size and hence I create all of the objects at once and populate the pool. What I want to do is that whenever the app using my JAR is loaded, the object pool is created without the application knowing about the details of such a pool. And that even though the app load time will take a hit because of the pool population, but once the开发者_开发知识库 app is past this stage, there shouldn't be any more performance hit in creation of such heavy objects later.

Is there a way to achieve this?


The only way to do something like this, outside of adding explicit hooks in the client apps to invoke your startup logic is to add static initializers to your classes to construct your pool.

A static initializer is run when your class is first loaded by the JVM (prior to any code in your class being invoked), which is not quite deterministic but is usually when a code path referencing your class is first hit.

I would recommend providing some sort of hooks into your startup logic for the client applications to invoke, this way they can precisely control this type of thing rather than having your startup logic run at an unpredictable time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜