Posts

Showing posts with the label simple

Alternative implementation for Singleton Class

In previous posts on Singleton Designing pattern I have explained one of the way of implementing and solving concurrency issue. Singleton pattern insures that only one instance of the class should be available throughout the application context. There is an alternative implementation for insuring the same functionality that is nothing but “a class with all public static methods” . A static method can be accessed directly by Class name without any class instance. It will serve same purpose as Singleton Class, means it shares only one state across the application. Here is a sample implementation import java.util.*; class SingletonMap { private static Map<String,String> map; static { map = new HashMap<String,String>(); } public static String getValue(String key){ return map.get(key); } public static void putValue(String key, String value){ map.put(key,value); } } Here the SingletonMap class with all methods declared as “static” and private stati...

Understanding Java Garbage Collector

Image
     Java’s garbage collection system reclaims objects automatically occurring transparently, behind the scenes, without any programmer intervention. It works like this: when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object is released. This recycled memory can then be used for a subsequent allocation. Although it complete transparent to developer, understanding how GC works helps developer to improve efficiency of Java Program. It is also helpful to improve performance of the program. JDK provides many runtime options to analysis GC and analysis your program behavior. Here is the simple java program to understand GC behavior 1: public class GCTest { 2: public GCTest() { 3: } 4: 5: public static void main(String[] args) { 6: int count = 10000; 7: long startTime = System.currentTimeMillis(); 8: System.out.println(" Total Count = " + count...