• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to avoid 2nd instance creation in java  RSS feed

 
Suresh Kumar Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is why Singleton pattern was created

Read singleton pattern article on wikipedia and it will solve all your doubts.
 
Suresh Kumar Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Suresh Kumar Rajendran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I'm not sure how to implement the lazy initialize. Can you please send me some sample code.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!