questzen

Testing the TimeLimitedCacheMap

Apr 13th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.39 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Map;
  4. import java.util.concurrent.TimeUnit;
  5.  
  6. public class TLCTest {
  7.     private final static long INITIAL_DELAY = 24;
  8.     private final static long DELAY = 16;
  9.     private final static long EXPIRY = 17;
  10.     private final static TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
  11.    
  12.     private final static long LONG_DELAY = 1;
  13.     private final static long SHORT_DELAY = 1;
  14.     private final static long POLL_DELAY = 3;
  15.    
  16.     private final static long LOOP = 1000;
  17.     private final static TimeLimitedCacheMap map = new TimeLimitedCacheMap(INITIAL_DELAY, DELAY, EXPIRY, TIME_UNIT);
  18.     /**
  19.      * @param args
  20.      */
  21.     public static void main(String[] args) {       
  22.         Thread t1 = createThread("A", "AA", LONG_DELAY);
  23.         Thread t2 = createThread("B", "BB", SHORT_DELAY);
  24.         Thread t3 = createStateLogger(POLL_DELAY);
  25.         t1.start();
  26.         t2.start();
  27.         t3.start();
  28.         createAndStartMultiple(100, SHORT_DELAY);
  29.     }
  30.  
  31.     private static Thread createThread(final String key, final String value, final long periodicity) {
  32.         Runnable r = new Runnable() {
  33.             long i=0;
  34.             long max=LOOP;
  35.             @Override
  36.             public void run() {
  37.                 while(i<max){
  38.                     map.put(key+i, value);
  39.                     try {
  40.                         Thread.sleep(periodicity);
  41.                     } catch (InterruptedException e) {
  42.                         e.printStackTrace();
  43.                     }
  44.                     i++;
  45.                 }
  46.             }
  47.         };
  48.         Thread t = new Thread(r);
  49.         t.setPriority(8);
  50.         return t;
  51.     }
  52.    
  53.     private static void createAndStartMultiple(int num, long delay) {
  54.         Thread[] threads = new Thread[num];
  55.         Thread t;
  56.         for (int i = 0; i < num; i++) {
  57.             t = createThread(i+"_n", i+"_ns", delay);
  58.             threads[i] = t;
  59.         }
  60.         for (Thread thread : threads) {
  61.             thread.start();
  62.         }
  63.     }
  64.    
  65.     private static Thread createStateLogger(final long periodicity){
  66.         Runnable r = new Runnable() {
  67.             long i=0;
  68.             long max=Math.max(LONG_DELAY * LOOP, INITIAL_DELAY + EXPIRY + POLL_DELAY);
  69.             long init=System.currentTimeMillis();
  70.             @Override
  71.             public void run() {
  72.                 while(i<max){
  73.                     i++;
  74.                     long now=System.currentTimeMillis();
  75.                     Map neo = map.getClonedMap();
  76.                     if(neo.isEmpty()) {
  77.                         i+=5; // fast loop
  78.                         continue;
  79.                     }
  80.                     String log = (now-init)+ " , "+neo.size();
  81.                     System.out.println(log);
  82.                     try {
  83.                         Thread.sleep(periodicity);
  84.                     } catch (InterruptedException e) {
  85.                         e.printStackTrace();
  86.                     }                  
  87.                 }
  88.             }
  89.         };
  90.         Thread t = new Thread(r);
  91.         //t.setDaemon(true);
  92.         return t;
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment