privateReferenceQueue queue=newReferenceQueue();
// Strong Reference numberpublicSoftHashMap(){this(100); }
publicSoftHashMap(inthardSize){ HARD_SIZE=hardSize; }publicObject get(Object key){
Object result=null;//We get the SoftReference represented by that key
SoftReference soft_ref=(SoftReference)hash.get(key);if(soft_ref!=null){
//From the SoftReference we get the value, which can be//null if it was not in the map, or it was removed in
//the processQueue() method defined belowresult=soft_ref.get();
if(result==null){//If the value has been garbage collected, remove the
//entry from the HashMap. hash.remove(key);
}else {//We now add this object to the beginning of the hard
//reference queue.One reference can occur more than//once, because lookups of the FIFO queue are slow, so
//we don't want to search through it each time to remove//duplicates.
// keep recent use object in memory hardCache.addFirst(result);
if(hardCache.size()>HARD_SIZE){//Remove the last entry if list longer than HARD_SIZE
hardCache.removeLast(); }
} }
returnresult; }
/**We define our own subclass of SoftReference which contains
not only the value but also the key to make it easier to findthe entry in the HashMap after it's been garbage collected.*/
privatestaticclassSoftValueextendsSoftReference{privatefinalObject key;//always make data member final
/**Did you know that an outer class can access private datamembers and methods of an inner class?I didn't know that!
I thought it was only the inner class who could access theouter class's private information.An outer class can also
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-49607-5.html
哪个项目有钱赚
美国的军事力量是强大