Một câu hỏi nhanh ở giữa: Tôi có một WeakRunnableList đơn giản. Là cách này ok để làm sạch nó lên (loại bỏ tham chiếu chết), hoặc là có một giải pháp thanh lịch hơn và nhanh hơn. Nguồn đầy đủ cho WeakRunnableList của tôi:ArrayList <WeakReference <Runnable>> - Làm thế nào để dọn dẹp tốt nhất?
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);
}
}
Cảm ơn đã nhận xét. Trên thực tế chỉ cần sửa chữa nó, sẽ thay đổi trong bài :-) –