Lazy mode, starving mode

Lazy mode and starving modes: single embodiment mode, only one subject only!

Starving mode: singleton class bytecode files loaded into the method area when Singleton (objects), it was new out.

Public class Singleton{
   public static int num=10;

   Private Singleton(){

}

Private static Sington sing = new Singleton();

   Public static Singleton genInstance(){

   Return sing;

}

}

Public class Test{

   Public static void main(String[] args){

     System.out.println(Singleton.num);

   }

}

Test Once running, class calls Singleton.num, to create objects new Singleton () method on the memory area;

Then the time maybe a year later, but the object is still in the process of their own area. That would create a class with objects occupy memory. When the benefit is multi-threaded, avoid high concurrency .

 

Lazy mode: When calling the method (getInstance) to get the object only when the object to which the only new.

Public class Singleton2{
   public static int num=10;

   Private Singleton2(){

   

}

Private static Sington2 sing2;

   Public static Singleton2 genInstance(){

      If(sing2==null){

      sing2= new Singleton2();

      }

   Return sing2;

}

}

 

have a test.

Public class Test{

   Public static void main(String[] args){

     System.out.println(Singleton2.num);

   }

}

It did not run with the Public static Singleton2 genInstance () method, but requires an object sing2, only to go if statement creates sing2 object. That is only going to build with, the more memory the province.

Guess you like

Origin www.cnblogs.com/aboutlanander/p/12182178.html