简体   繁体   English

Thread中的Java中的WeakReference

[英]WeakReference in Java inside Thread

i am trying to create an background thread that updates a Runnable at a given interval. 我正在尝试创建一个后台线程,以给定的间隔更新Runnable。

It should also not prevent the "parent" from beeing garbage collected. 它也不应该阻止“父母”收集垃圾。

My problem is as follows. 我的问题如下。 My WeakReference seems to act as a "strong" Reference, It doesn't stop my thread form accessing the runnable that i am supposed to have made available for gc. 我的WeakReference似乎充当了“强大的”参考,它并没有阻止我的线程形式访问我应该为gc提供的runnable。

Why is my Weakreference preventing gc? 为什么我的Weakreference会阻止gc?

Below is my full implementation 以下是我的全面实施

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");
    }
    }
}

In addition to making ThreadRunnable static you also need to set object to null before you Thread.sleep() . 除了使ThreadRunnable静态之外,还需要在Thread.sleep()之前将object设置为null。 The garbage collector cannot reclaim the object unless that reference is cleared out. 除非清除该引用,否则垃圾收集器无法回收该对象。 Just move the Thread.sleep() code down below the object = null; 只需将Thread.sleep()代码向下移动到object = null; assignment and that should give the garbage collector a chance. 分配,这应该给垃圾收集器一个机会。

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");
}

Not sure if thats the cause, but ThreadRunnable is a non-static inner class, this gets passed a reference to the containing class in its generated contructor. 不确定是否是原因,但ThreadRunnable是一个非静态内部类,这会在其生成的构造ThreadRunnable中传递对包含类的引用。 You could try making it a static inner class and pass the neccessary parameters manually in the constructor like this (untested): 你可以尝试使它成为一个静态内部类,并在构造函数中手动传递必要的参数,如下所示(未经测试):

static class ThreadRunnable implements Runnable {

    private WeakReference updateObject;

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

     public void run() { ... }
}

I advise you not to count on nulling the local variable in your main to avail the runnable to gc. 我建议你不要指望对你的main中的局部变量进行归零以利用runnable到gc。 Can you split out some or all of the if-then blocks to their own methods and make the runnable only a local var in one of those methods. 你能否将部分或全部if-then块拆分为自己的方法,并使runnable只能在其中一种方法中使用局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM