Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- public class TLCTest {
- private final static long INITIAL_DELAY = 24;
- private final static long DELAY = 16;
- private final static long EXPIRY = 17;
- private final static TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
- private final static long LONG_DELAY = 1;
- private final static long SHORT_DELAY = 1;
- private final static long POLL_DELAY = 3;
- private final static long LOOP = 1000;
- private final static TimeLimitedCacheMap map = new TimeLimitedCacheMap(INITIAL_DELAY, DELAY, EXPIRY, TIME_UNIT);
- /**
- * @param args
- */
- public static void main(String[] args) {
- Thread t1 = createThread("A", "AA", LONG_DELAY);
- Thread t2 = createThread("B", "BB", SHORT_DELAY);
- Thread t3 = createStateLogger(POLL_DELAY);
- t1.start();
- t2.start();
- t3.start();
- createAndStartMultiple(100, SHORT_DELAY);
- }
- private static Thread createThread(final String key, final String value, final long periodicity) {
- Runnable r = new Runnable() {
- long i=0;
- long max=LOOP;
- @Override
- public void run() {
- while(i<max){
- map.put(key+i, value);
- try {
- Thread.sleep(periodicity);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- i++;
- }
- }
- };
- Thread t = new Thread(r);
- t.setPriority(8);
- return t;
- }
- private static void createAndStartMultiple(int num, long delay) {
- Thread[] threads = new Thread[num];
- Thread t;
- for (int i = 0; i < num; i++) {
- t = createThread(i+"_n", i+"_ns", delay);
- threads[i] = t;
- }
- for (Thread thread : threads) {
- thread.start();
- }
- }
- private static Thread createStateLogger(final long periodicity){
- Runnable r = new Runnable() {
- long i=0;
- long max=Math.max(LONG_DELAY * LOOP, INITIAL_DELAY + EXPIRY + POLL_DELAY);
- long init=System.currentTimeMillis();
- @Override
- public void run() {
- while(i<max){
- i++;
- long now=System.currentTimeMillis();
- Map neo = map.getClonedMap();
- if(neo.isEmpty()) {
- i+=5; // fast loop
- continue;
- }
- String log = (now-init)+ " , "+neo.size();
- System.out.println(log);
- try {
- Thread.sleep(periodicity);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
- Thread t = new Thread(r);
- //t.setDaemon(true);
- return t;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment