• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton Class  RSS feed

 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
i want to ask about singleton class in java.
here is the class :

public class FindHODealerDelegateImpl
{
private static FindHODealerDelegateImpl findHODealerDelegate = null;

public synchronized static FindHODealerDelegate getInstance() {
if (findHODealerDelegate == null) {
findHODealerDelegate = new FindHODealerDelegateImpl();
}
return findHODealerDelegate;
}

public List findHODealer(Map map){
//some code here
}
}

i want to ask, should findHODealer() method be synchronized or not, in case there are more than one user access the instance in once at a time.

thanks in advance.

regards,
Ricky
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ricky Martaputra:
i want to ask, should findHODealer() method be synchronized or not, in case there are more than one user access the instance in once at a time.

thanks in advance.

regards,
Ricky


Whether the findHODealer() should be synchronized or not, depends on what the method does, and depends on whether the FindHODealerDelegateImpl instance is thread safe or not.

Henry
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you henry.

the method just passes the map from UI into ejb, and return list of dealers.
i still don't get it, when do we use the synchronize word in a method while it is a singleton class, and what will be happened if we don't use the synchronize word.

regards,
Ricky
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ricky Martaputra:
thank you henry.

the method just passes the map from UI into ejb, and return list of dealers.
i still don't get it, when do we use the synchronize word in a method while it is a singleton class, and what will be happened if we don't use the synchronize word.

regards,
Ricky


The synchronized keyword is independent of whether a method belongs to a singleton class or not. It is used to protect an object from being used in multiple threads at the same time. you should read about multi-threading to understand when you should use the synchronized keyword.

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