Mega Code Archive

 
Categories / Java Tutorial / Collections
 

Demonstrating the WeakHashMap

import java.util.Map; import java.util.WeakHashMap; public class MainClass {   private static Map map;   public static void main(String args[]) {     map = new WeakHashMap();     map.put("A", "B");     Runnable runner = new Runnable() {       public void run() {         while (map.containsKey("A")) {           try {             Thread.sleep(1000);             System.gc();           } catch (InterruptedException ignored) {           }           System.out.println("Has A");           System.gc();         }       }     };     Thread t = new Thread(runner);     t.start();     System.out.println("Main waiting");     try {       t.join();     } catch (InterruptedException ignored) {     }     System.gc();   } }