ArrayList<WeakReference<Runnable>> - How to tidy up best?

喜你入骨 提交于 2019-12-19 02:38:48

问题


A quick question in between: I have a simple WeakRunnableList. Is this way ok to clean it up (removing dead references), or is there a more elegant and faster solution. Full source for my WeakRunnableList:

public class WeakRunnableList
{
    private ArrayList<WeakReference<Runnable>> _items = new ArrayList<WeakReference<Runnable>>();

    public void Add(Runnable r)
    {
        _items.add(new WeakReference<Runnable>(r));
    }

    public void Execute()
    {
        ArrayList<WeakReference<Runnable>> remove = new ArrayList<WeakReference<Runnable>>();
        for (WeakReference<Runnable> item : _items)
        {
            Runnable tempCheck = item.get();
            if (tempCheck  == null)
            {
                remove.add(item);
            }
            else
            {
                tempCheck.run();
            }
        }
        _items.removeAll(remove);
    }
}

回答1:


Here is my take. WeakHashMap removes autmatically, so this should suffice. Beware of hashCode/equals semantics of Runnable though.

See also Are keySet entries of a WeakHashMap never null? WeakHashMap iteration and garbage collection

import java.util.WeakHashMap;

public class WeakRunnableList
{
    private WeakHashMap<Runnable, Void> _items = new WeakHashMap<Runnable, Void>();

    public void Add(Runnable r)
    {
        _items.put(r, null);
    }

    public void Execute()
    {
        Iterator<Runnable> iterator = _items.keySet().iterator();
        while (iterator.hasNext()) {
            Runnable runnable = iterator.next();
            if (runnable != null) {
                runnable.run();
                iterator.remove();
            }
        }
    }
}



回答2:


You have a race condition between calls to item.get(). I would place item.get() into a local variable and use that.



来源:https://stackoverflow.com/questions/8589581/arraylistweakreferencerunnable-how-to-tidy-up-best

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!