Java中的WeakReference在Thread内部

4

我正在尝试创建一个后台线程,以在给定时间间隔内更新Runnable。

它还不应阻止"父级"被垃圾回收。

我的问题如下。我的WeakReference似乎充当了"强"引用,它没有阻止我的线程访问我应该为gc提供的runnable。

为什么我的WeakReference会阻止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");
    }
    }
}

考虑一下清理你的缩进,这段代码非常难以阅读。 - Bjarke Freund-Hansen
3个回答

1
除了将ThreadRunnable设置为静态之外,您还需要在执行Thread.sleep()之前将对象设置为null。除非清除该引用,否则垃圾收集器无法回收该对象。 只需将Thread.sleep()代码移动到object = null;赋值下面,这样垃圾收集器就有机会了。
public void run() {
    Object object;
    while ((object = updateObject.get()) != null) {
        System.out.println("object is now: " + object);
        ((Runnable) object).run();
        object = null;
        try {
            Thread.sleep(updateFrequencyMs);
        } catch (InterruptedException ex) {
            System.out.println("Thread interrupted, killing thread");
            return;
        }
    }
    System.out.println("lost object reference: killing thread");
}

我尝试使用原始代码,只将sleep移动到将object设置为null之后,然后测试了代码,它对我进行了垃圾回收。没有必要使ThreadRunnable静态(那不是被垃圾回收的Runnable)。我同意Ron的评论,你不能依赖编译器/JVM将对象置为空,但在jdk1.6.0_14中使用它对我有效。我将使用新的run方法代码编辑我的原始答案。 - Richm

0

不确定是否是这个原因,但是ThreadRunnable是一个非静态内部类,它在生成的构造函数中传递了对包含类的引用。您可以尝试将其更改为静态内部类,并手动在构造函数中传递必要的参数,如下所示(未经测试):

static class ThreadRunnable implements Runnable {

    private WeakReference updateObject;

    ThreadRunnable(WeakReference updateObject) {
        this.updateObject = updateObject;
    }

     public void run() { ... }
}

0

我建议你不要指望在主函数中将局部变量置空以使可运行对象可被垃圾回收。你可以将一些或所有的if-then块拆分到它们自己的方法中,并将可运行对象仅作为这些方法中的局部变量。


这会使测试无法运行,我尝试将线程设置为非守护线程,并在主方法中执行以下操作:new WeakIntervalUpdater(1000, new UpdateTesterRunnable()).startUpdating();但是该线程永远不会退出。 - Incognito
我设法黑进去以测试我的理论,但对象仍未被垃圾回收。很有趣的难题。 - Ron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接