2018-09-17 10:07:32 +03:00
|
|
|
package ru.gravit.launcher.managers;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
|
|
import ru.gravit.launcher.NeedGarbageCollection;
|
|
|
|
|
|
|
|
public class GarbageManager {
|
|
|
|
static class Entry {
|
|
|
|
NeedGarbageCollection invoke;
|
|
|
|
long timer;
|
|
|
|
|
|
|
|
public Entry(NeedGarbageCollection invoke, long timer) {
|
|
|
|
this.invoke = invoke;
|
|
|
|
this.timer = timer;
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 17:33:00 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
private static final Timer timer = new Timer("GarbageTimer");
|
|
|
|
|
|
|
|
private static final ArrayList<Entry> NEED_GARBARE_COLLECTION = new ArrayList<>();
|
|
|
|
|
|
|
|
public static void gc() {
|
|
|
|
for (Entry gc : NEED_GARBARE_COLLECTION)
|
2018-09-22 17:33:00 +03:00
|
|
|
gc.invoke.garbageCollection();
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerNeedGC(NeedGarbageCollection gc) {
|
|
|
|
NEED_GARBARE_COLLECTION.add(new Entry(gc, 0L));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerNeedGC(NeedGarbageCollection gc, long time) {
|
|
|
|
TimerTask task = new TimerTask() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
gc.garbageCollection();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
timer.schedule(task, time);
|
|
|
|
NEED_GARBARE_COLLECTION.add(new Entry(gc, time));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void unregisterNeedGC(NeedGarbageCollection gc) {
|
|
|
|
NEED_GARBARE_COLLECTION.removeIf(e -> e.invoke == gc);
|
|
|
|
}
|
|
|
|
}
|