兩者之間的一個簡單問題:我有一個簡單的WeakRunnableList。 是這種方式可以清理它(刪除死亡引用),還是有一個更優雅和更快的解決方案。我的WeakRunnableList的完整源代碼:ArrayList <WeakReference <Runnable>> - 如何整理最佳?
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);
}
}
感謝您的評論。其實只是糾正它,會改變後:-) – 2011-12-21 12:25:02