GenericRefDemo.java

Download GenericRefDemo.java

 1: // Generic Class, SoftReference, and WeakReference Demo
 2: // This class creates a generic class Cache that uses soft references,
 3: // so when the system runs low on memory items will be deleted.  To
 4: // improve performance further, when some item has been garbage collected
 5: // the entry is deleted from the map completely.
 6: // The cache uses generics to allow a cache of anything, such as filenames
 7: // to Image objects.  (Here I only use String and Date.)
 8: //
 9: // In addition a WeakHashMap is used for the same purpose.  The output
10: // shows what happens when the garbage collector is run.
11: //
12: // Written 3/2010 by Wayne Pollock, Tampa Florida USA
13: 
14: import java.lang.ref.*;
15: import java.util.*;
16: 
17: class Cache <T>
18: {
19:    Map<String,SoftReference<T>> cache =
20:       new HashMap<String,SoftReference<T>>();
21: 
22:    public T get ( String name )
23:    {
24:       SoftReference<T> ref = cache.get( name );
25:       T item = null;
26: 
27:       if ( ref != null )
28:       {
29:          item = ref.get();
30:          if ( item == null )
31:             cache.remove( name );  // clean out cache
32:          return item;
33:       }
34:       return null;
35:    }
36: 
37:    public void put ( String name, T item )
38:    {
39:       cache.put( name, new SoftReference<T>(item) );
40:    }
41: }
42: 
43: public class GenericRefDemo
44: {
45:    public static void main ( String [] args )
46:    {
47:       Calendar cal = Calendar.getInstance();
48: 
49:       System.out.println("\n****** Cache Using Soft References ******\n\n");
50:       Cache<Date> birthday = new Cache<Date>();
51:       cal.set(1958,4,25);
52:       Date d = cal.getTime();
53:       birthday.put( "Wayne", d);
54:       cal.set(1809, 2, 12);
55:       d = cal.getTime();
56:       birthday.put( "Abe", d);
57: 
58:       System.out.println("Lincoln's Birthday: " + birthday.get("Abe") );
59:       System.out.println("Washington's Birthday: " + birthday.get("George") );
60:       System.gc();
61:       System.out.println("\n(Ran garbage collector)\n");
62:       System.out.println("Lincoln's Birthday: " + birthday.get("Abe") );
63: 
64:       System.out.println("\n\n****** Using Weak References *******\n\n");
65: 
66:       Map<String,Date> map = new WeakHashMap<String,Date>();
67:       String name = new String("Lincoln");
68:       cal.set(2009, 4, 1);
69:       d = cal.getTime();
70: 
71:       map.put(name, d);  // Try this using a String literal for name!
72:       System.out.println("Lincoln's Birthday: " + map.get("Lincoln") );
73:       System.gc();
74:       System.out.println("\n(Ran garbage collector)\n");
75:       System.out.println("Lincoln's Birthday: " + map.get("Lincoln") );
76: 
77:       name = null;
78:       System.gc();
79:       System.out.println("\n(Ran garbage collector after nulling out \"name\")");
80:       System.out.println("Lincoln's Birthday: " + map.get("Lincoln") );
81:    }
82: }