Я пытаюсь создать фоновый поток, который обновляет Runnable с заданным интервалом.
Это также не должно препятствовать тому, чтобы «родитель» собирал мусор.
Моя проблема заключается в следующем. Кажется, моя слабая ссылка действует как «сильная» ссылка. Она не мешает моей форме потока обращаться к исполняемому файлу, который я должен был сделать доступным для gc.
Почему моя слабая ссылка предотвращает gc?
Ниже приведена полная реализация
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
public final class WeakIntervalUpdater {
private final long updateFrequencyMs;
private final WeakReference updateObject;
private Thread runningThread;
/**
* Will keep a thread running fireing the updaterunnable every updateFrequencyMs.
*
* the updateRunnable is first fired after updateFrequencyMs ms after startUpdating() is called
*
* This thread will require calls to be made to stopUpdating() or that the
* updateRunnable is garbage collected to stop updateing and be eligable for
* garbage collection.
*
* This class maintains only a weak reference to the updateRunnablein order.
*
*
* @param updateFrequencyMs number of ms between each update
* @param updateRunnable the update runnable
*/
public WeakIntervalUpdater(long updateFrequencyMs, Runnable updateRunnable) {
this.updateFrequencyMs = updateFrequencyMs;
this.updateObject = new WeakReference(updateRunnable);
}
public void startUpdating() {
if (runningThread != null) {
if (runningThread.isAlive()) {
return;
}
runningThread.interrupt();
runningThread = new Thread(createThreadRunnable());
} else {
runningThread = new Thread(createThreadRunnable());
}
runningThread.setDaemon(true);
runningThread.start();
}
public void stopUpdating() {
if (runningThread != null) {
runningThread.interrupt();
runningThread = null;
}
}
Runnable createThreadRunnable() {
return new ThreadRunnable();
}
private class ThreadRunnable implements Runnable {
public void run() {
Object object;
while ((object = updateObject.get()) != null) {
System.out.println("object is now: " + object);
try {
Thread.sleep(updateFrequencyMs);
} catch (InterruptedException ex) {
System.out.println("Thread interrupted, killing thread");
return;
}
((Runnable) object).run();
object = null;
}
System.out.println("lost object reference: killing thread");
}
}
private static void printTestHelp() {
System.out.println("\n\n\n\n---------------------");
System.out.println("Commands:");
System.out.println("c : create an updater with a reference to an updateRunnable");
System.out.println("r : release reference to updateRunnable");
System.out.println("gc: run garbagecollection");
System.out.println("s : stop updater");
System.out.println("i : print object references");
System.out.println("q : quit program");
System.out.println("\nPlease enter your command");
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
WeakIntervalUpdater updater = null;
Runnable myUpdateRunnable = null;
printTestHelp();
while (!(line = br.readLine()).equals("q")) {
if (line.equals("c")) {
if (updater != null) {
updater.stopUpdating();
System.out.println("\tUpdater stopped");
}
myUpdateRunnable = new UpdateTesterRunnable();
updater = new WeakIntervalUpdater(1000, myUpdateRunnable);
updater.startUpdating();
System.out.println("\tcreated updater! updateing every 1000 ms");
} else if (line.equals("r")) {
//updater = null;
myUpdateRunnable = null;
System.out.println("\tDropped refrence to updater!");
System.out.println("\tupdateRunnable=" + myUpdateRunnable);
} else if (line.equals("gc")) {
System.gc();
Runtime.getRuntime().runFinalization();
System.out.println("\tGarbage collection running!");
} else if (line.equals("s")) {
if (updater != null) {
updater.stopUpdating();
System.out.println("\tUpdater stopped");
} else {
System.out.println("\tNo updater running");
}
} else if (line.equals("i")) {
System.out.println("\tupdater = " + updater);
System.out.println("\tupdateRunnable = " + myUpdateRunnable);
} else {
printTestHelp();
}
}
System.out.println("Goodbye");
}
private static class UpdateTesterRunnable implements Runnable {
public void run() {
System.out.println("\t\t\t(updating)");
}
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("finalize");
}
}
}