| Author |
How to avoid 2nd instance creation in java
|
Suresh Kumar Rajendran
Greenhorn
Joined: Oct 31, 2007
Posts: 19
|
|
I have to check that class XXXXX instance is running or not in server. If it is already running then if we try to create a 2nd instance of the same class some exception has to generate. How to implement this? Any suggestion welcome!!
Example:
Batch Job:
Say java instance is instantiated by triggering Batch job and java instance is running in server; in this scenario if we try initiate same job again new instance will be created. Here I don’t want to happens 2nd instance creation.
|
 |
Koen Aerts
Ranch Hand
Joined: Feb 07, 2012
Posts: 339
|
|
|
Maybe you need to look at the Singleton Pattern and modify it so that it throws an Exception instead of returning an existing reference if an instance has already been created.
|
 |
Alex Armenteros
Ranch Hand
Joined: May 05, 2010
Posts: 46
|
|
This is why Singleton pattern was created
Read singleton pattern article on wikipedia and it will solve all your doubts.
|
 |
Suresh Kumar Rajendran
Greenhorn
Joined: Oct 31, 2007
Posts: 19
|
|
|
If we use SinglePattern instance always in server even the process completed. In may case new instance have to create only after there is no exiting instance exists.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3143
|
|
Suresh Kumar Rajendran wrote:If we use SinglePattern instance always in server even the process completed.
So? Will it hurt you to have one unused object lying around? Does your object consume hundreds of MB of memory?
|
 |
Suresh Kumar Rajendran
Greenhorn
Joined: Oct 31, 2007
Posts: 19
|
|
Thanks for your reply. Memory is not an issue. If i use static every time new instance will not be created once it is created already. But i want to create new instance of the class only when there is no exist/running in the server.
|
 |
Anayonkar Shivalkar
Ranch Hand
Joined: Dec 08, 2010
Posts: 674
|
|
Suresh Kumar Rajendran wrote:If i use static every time new instance will not be created once it is created already.
But this is your requirement, right
Suresh Kumar Rajendran wrote:But i want to create new instance of the class only when there is no exist/running in the server.
Perhaps you can perform lazy instantiation in singleton class (make sure that you synchronize it).
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
Suresh Kumar Rajendran
Greenhorn
Joined: Oct 31, 2007
Posts: 19
|
|
|
Thanks for your reply. I'm not sure how to implement the lazy initialize. Can you please send me some sample code.
|
 |
Anayonkar Shivalkar
Ranch Hand
Joined: Dec 08, 2010
Posts: 674
|
|
Suresh Kumar Rajendran wrote:Can you please send me some sample code.
Perhaps this might help.
For in depth explanation of why getInstance() method has to be thread-safe (synchronized), here is an excellent article.
I hope this helps.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 1610
|
|
Suresh Kumar Rajendran wrote:Thanks for your reply. Memory is not an issue.
In which case I agree with Anayonkar: this is your requirement; and I'd suggest that it's spurious.
The only reason for using lazy instantiation is because the object in question takes either
1. A lot of space.
2. A lot of time to create - in fact, I'd go further and say that it has to take so much time that it will be visible to users.
Otherwise, the standard explicit instantiation
private static final mySingleton INSTANCE = new mySingleton();
is almost always the best way to go. I've used lazy instantiation precisely once in 11 years of writing Java.
Another point to consider is whether or not this class really needs to be a singleton. At least one of the Gang of Four has said that if they were writing their 'Patterns' book again, they would probably have left it out. You may also be interested in this article.
Winston
|
More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf
|
 |
 |
|
|
subject: How to avoid 2nd instance creation in java
|
|
|