What way would I go about running a singleton object that takes a long time to initialize in multiple threads?
This is the problem. I have a singleton object that takes like 4 to 5 seconds to initialize, and I need to be able to make a method call to this object on multiple threads. Unfortunately this object is not thread safe. Currently I use spring to create the object at server start up and then just reference it within other spring objects so I don't have to pass around the reference to it. I make around 10 calls to this object but right now I have to do these one at a time and wait for them each to finish before making the call again.
I want to speed this up and so the best way to do this would be for me to be able to make the method calls to the singleton object at the same time and have them run in parallel. Unfortunately since this object is a singleton and takes up a lot of memory I think I would need to run this object on separate jvms and then have each jvm receive the method call and run the code and return it.
My question is how do I go about doing this? I think I need to use a distributed system but I am not familiar with them and so I am not sure if this is the best way or if there is an easier way. Are there any frameworks or tools that are made for this?
Thanks in advance.
Re: What way would I go about running a singleton object that takes a long time to initialize in multiple threads?
Why not synchronize it? Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)
That should postpone the other threads until whatever thread is using the object releases it.