Delete the key in the MAP according to certain conditions, mainly used for some timing deletion of Key operations
It should be more apps in imitation of httpsession
Package pack;
Import java.util.hashmap;
Import java.util.map;
/ **
*
* @Author administrator
*
* /
Public Abstract Class ExecuteTimetask Extends Thread {
Private long time = 60 * 100; // Timing time
PRIVATE MAP MAP = NULL;
/ **
* Condition is true, execute the clean operation
* @Param Key
* @Param Value
* @Return
* /
Public Abstract Boolean Condition (Object Key, Object Value);
/ * (non-javadoc)
* @see java.lang.thread # run ()
* /
Public void run () {
For (;;) {
//
Try {
Synchronized (this) {
THIS.WAIT (TIME);
}
} catch (interruptedexception e) {
E.PrintStackTrace ();
}
IF (Map == null || map.size () <1) {
CONTINUE;
}
For (java.util.Itemrator it = map.entryset (). item (); it.hasnext ();) {
Map.Entry Entry = (map.entry) it.next ();
IF (construct.getKey (), entry.getvalue ())) {
it.remove ();
}
}
}
}
/ **
* @Param Args
* /
Public static void main (String [] args) {
ExecuteTimetask T = new executetimetask () {
Public Boolean Condition (Object Key, Object Value) {
Return Key.toString (). Equals (value.toString ())? True: false;
// Return True;
}
}
Map m = new hashmap ();
M.PUT ("a", "2");
//m.put ("2 "," 2 ");
T.SETMAP (M);
T.SETTIME (1000);
T.Start ();
Long L = 0;
For (;;) {
Try {
Synchronized (thread.currentthread ()) {
Thread.currentthread (). Wait (2000);
}
} catch (interruptedexception e) {
// Todo Auto-Generated Catch Block
E.PrintStackTrace ();
}
System.err.Println ("Before -" M "-" java.lang.Runtime.Getruntime (). TotalMemory ());
M.PUT (L "", L "");
l ;
System.err.println ("After" M "--- nanotime -" system.nanotime () "----" system.currenttimemillis ());
}
/ **
* @Return the MAP
* /
Public map getmap () {
Return Map;
}
/ **
* @Param map the map to set
* /
Public void setmap (Map Map) {
THIS.MAP = Map;
}
/ **
* @Return the Time
* /
Public long gettime () {
Return Time;
}
/ **
* @Param Time The Time To Set
* /
Public void settime (long time) {
this.Time = Time;
}
}