Basically I want to create a concurrent LinkedHashSet
which returns proper size too.
I am mainly concerned about adding and removing iterations. Suggestions relating to implementation of set modification during iteration are also welcomed.
import java.util.Collection;
import java.util.LinkedHashSet;
//TODO still not sure
public class ConcurrentLinkedHashSet<E> extends LinkedHashSet<E>
{
private static final long serialVersionUID = 1L;
private boolean isUpdating = false;
@Override
public int size()
{
while (isUpdating)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
return (Integer) null;
}
}
return super.size();
}
@Override
public synchronized boolean addAll(Collection<? extends E> arg0)
{
while (isUpdating)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
return false;
}
}
isUpdating = true;
boolean result = false;
result = super.addAll(arg0);
isUpdating = false;
notify();
return result;
}
@Override
public synchronized boolean add(E arg0)
{
while (isUpdating)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
return false;
}
}
isUpdating = true;
boolean result = super.add(arg0);
isUpdating = false;
notify();
return result;
}
@Override
public boolean remove(Object arg0)
{
while (isUpdating)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
return false;
}
}
isUpdating = true;
boolean result = super.remove(arg0);
isUpdating = false;
notify();
return result;
}
@Override
public boolean removeAll(Collection<?> arg0)
{
while (isUpdating)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
return false;
}
}
isUpdating = true;
boolean result = super.removeAll(arg0);
isUpdating = false;
notify();
return result;
}
}
EDIT : Updated the code based on reviews and is available on github