2013年11月19日星期二

Lazy single case optimization

 This post last edited by the xtdhwl on 2013-11-14 20:16:08
In the actual project used a single case of almost all , but I think if you can optimize single case

Here is my direct knock up, pseudo code

 public class A {
  4 
  5         private static A instance;
  6 
  7         private A(){}
  8         
  9         public synchronized static A getInstance(){
 10                 if(instance == null){
 11                         instance = new A();
 12                 }
 13                 return instance;
 14         }
 15 
 16 }


Here is my opinion optimal solution , we use a single case will use synchroized synchronized block , but if instantiated once I would not be null, synchroized synchronized block a bit redundant , although there are a lot of java for synchronization optimization I think we have optimized safer
public class B{
 20 
 21         private static B instance;
 22         
 23         private B(){}
 24 
 25         public static B getInstance(){
 26                 if(instance == null){
 27                         instance = newInstance();
 28                 }
 29                 return instance;
 30         }
 31 
 32         private synchroized static B newInstance(){
 33                 if(instance == null){
 34                         instance = new B();
 35                 }
 36                 return instance;
 37         }
 38 } 


Please give evaluation
------ Solution --------------------------------- -----------
landlord of spirit is very commendable, I hope the landlord can explore further on this issue and optimization , it is proposed under Google Keyword : double-checked locking
---- - Solution --------------------------------------------
referred to the second floor is the double- checked locking double-check , in fact, in a multithreaded environment, there are other ways for a single case

such as static inner classes , and another enumeration itself is a reflection both single case can not break . . .
------ Solution ---------------------------------------- ----
Haha , the landlord can think of learners ah , the landlord on the code used in technologies such as double checking upstairs said , which helps to improve the performance of the try enumeration single case , is also a very good way to achieve the hope that the landlord learning progress, refueling
------ For reference only -------------------- -------------------
double-checked locking learn to write , and to see where a lot for multithreaded still have to consider or is playing with fire

Singleton pattern several written ( including double- checked locking wording ) http://blog.csdn.net/lg312200538/article/details/4930451
------ For reference only - --------------------------------------
landlord of spirit is very commendable, multi-directional landlord to learn.
------ For reference only -------------------------------------- -
optimal single case should be based on static inner classes that implement
public class Test {
public static Test getInstance () {
return InnerContainer.test;
}
private static class InnerContainer {
static Test test = new Test ();
}

public static void main (String [] args) {
Test intance1 = Test.getInstance ();
Test intance2 = Test.getInstance ();
System.out.println (intance1);
System.out.println (intance2);
}
}

没有评论:

发表评论