java - How to make Weakrefernce in Concurrent HashMap -


in program have scenario, mutiple thread operate (put , get) on single map. thread using put key/value in map , same time thread retrieving key/value map same or different key. if make map synchronize wil big performance issue, decided move concurrenthashmap. during scenario, have complexity after period of time(not known application) few of key/value pairs not needed in application, need remove same(garbage collection) free memory. though programm doesn't have idea key/values not needed thought use weakrefernce. so, after ceratin period of time if key(string) not reachable automatically garbage collected. know process same in weakhashmap doesn't know process in concurrenthashmap. so, can tell me how weakrefernce in concurrenthashmap. or else there other way achieve above scenario?

looking through weakhashmap code seems there no straight forward way of doing of following form should work: (this code has not been tested think @ least on correct path, implements put , get, other operations can likewise implemented)

public class mymap<k, v> {      private class mykey<k> extends weakreference<k> {                   private final int hashcode;          private mykey(k k, referencequeue<k> q) {             super(k, q);             hash = k.hashcode();         }          private mykey(k k) {             super(k);             hash = k.hashcode();         }          @override         public int hashcode() {             return hashcode;         }          @override         public boolean equals(object o) {             if (!(o instanceof mykey)) {                 return false;             }              k otherkey = ((mykey<k>) o).get();             k mykey = get();             if (otherkey != null && key != null) {                 return otherkey.equals(mykey);             }              return == o;         }     }      private final map<mykey<k>, v> map = new concurrenthashmap<mykey<k>, v>();     private final referencequeue<k> queue = new referencequeue<k>();      public v put(k key, v val) {         expungestaleentries();         return map.put(new mykey<k>(key, queue), val);         }      public v get(k key) {         expungestaleentries();         return map.get(new mykey<k>(key));     }      private void expungestaleentries() {         mykey<k> key = null;         while ((key = (mykey<k>) queue.poll()) != null) {             map.remove(key);         }     } } 

potential incorrectness of above code aside, caveat weakhashmap works calling own expungestaleentries() method every time call things put(), get(), or size(). problem in current scenario referencequeue.poll() actual polling inside of synchronized block cleaning can slow down (however not lock if queue empty not undo of speed-improvement work concurrenthashmap doing).


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -