public class DisposingPolicy {
-
+
public static final boolean DEBUG = false;
-
+
public static final int MAX_QUEUE_LENGTH = 8;
public static final long DISPOSE_TIME = 30000L; // ms
public static final long MIN_DELAY = 200L; // ms
-
+
+ private final int maxQueueLength;
private ArrayDeque<Runnable> disposerQueue = new ArrayDeque<Runnable>(MAX_QUEUE_LENGTH);
private TObjectLongHashMap<Runnable> disposeTime =
new TObjectLongHashMap<Runnable>(MAX_QUEUE_LENGTH);
private Runnable currentlyScheduled = null;
-
- private Runnable disposeOne = new Runnable() {
- @Override
- public void run() {
- if(!disposerQueue.isEmpty()) {
- Runnable runnable = disposerQueue.removeFirst();
- disposeTime.remove(runnable);
- currentlyScheduled = null;
- runnable.run();
- if(DEBUG)
- System.out.println("Executed disposer " + runnable);
- if(!disposerQueue.isEmpty())
- scheduleDispose();
- }
+
+ private Runnable disposeOne = () -> {
+ if(!disposerQueue.isEmpty()) {
+ Runnable runnable = disposerQueue.removeFirst();
+ disposeTime.remove(runnable);
+ currentlyScheduled = null;
+ runnable.run();
+ if(DEBUG)
+ System.out.println("Executed disposer " + runnable);
+ if(!disposerQueue.isEmpty())
+ scheduleDispose();
}
};
-
+
+ public DisposingPolicy() {
+ this(MAX_QUEUE_LENGTH);
+ }
+
+ public DisposingPolicy(int maxQueueLength) {
+ this.maxQueueLength = maxQueueLength;
+ }
+
private void scheduleDispose() {
currentlyScheduled = disposerQueue.peekFirst();
long delay = Math.max(
if(DEBUG)
System.out.println("Scheduled disposer " + currentlyScheduled + " in " + delay + " ms");
}
-
+
/**
* Runs the disposer either after DISPOSE_TIME or when there are
- * more than MAX_QUEUE_LENGTH disposers active and this is first
+ * more than {@link #maxQueueLength} disposers active and this is first
* of them (by activation order). This method must be called from
* UI thread.
*/
System.out.println("Added disposer " + disposer);
if(disposeTime.contains(disposer))
return;
- if(disposerQueue.size() >= MAX_QUEUE_LENGTH)
+ if(disposerQueue.size() >= maxQueueLength)
disposeOne.run();
disposerQueue.addLast(disposer);
disposeTime.put(disposer, System.currentTimeMillis()+DISPOSE_TIME);
if(disposerQueue.size() == 1)
scheduleDispose();
}
-
+
/**
* Cancels a disposer added before. This method must be called from
* UI thread.
scheduleDispose();
}
}
-
+
}