threadSafetyTest(false);
}
protected void threadSafetyTest(final boolean singleFqn) throws Exception
{
final CountDown latch = new CountDown(1);
final Fqn fqn = Fqn.fromString("/a/b/c");
final List fqns = new ArrayList(30);
final Random r = new Random();
if (!singleFqn)
{
for (int i = 0; i < 30; i++)
{
Fqn f = Fqn.fromString("/a/b/c/" + i);
fqns.add(f);
loader.put(f, "k", "v");
}
}
else
{
loader.put(fqn, "k", "v");
}
final int loops = 1000;
final Set exceptions = new CopyOnWriteArraySet();
Thread remover1 = new Thread("Remover-1")
{
public void run()
{
try
{
latch.acquire();
for (int i = 0; i < loops; i++)
{
loader.remove(singleFqn ? fqn : (Fqn)fqns.get(r.nextInt(fqns.size())));
}
}
catch (Exception e)
{
exceptions.add(e);
}
}
};
remover1.start();
Thread remover2 = new Thread("Remover-2")
{
public void run()
{
try
{
latch.acquire();
for (int i = 0; i < loops; i++)
{
loader.remove(singleFqn ? fqn : (Fqn)fqns.get(r.nextInt(fqns.size())), "k");
}
}
catch (Exception e)
{
exceptions.add(e);
}
}
};
remover2.start();
Thread reader1 = new Thread("Reader-1")
{
public void run()
{
try
{
latch.acquire();
for (int i = 0; i < loops; i++)
{
loader.get(singleFqn ? fqn : (Fqn)fqns.get(r.nextInt(fqns.size())));
}
}
catch (Exception e)
{
exceptions.add(e);
}
}
};
reader1.start();
Thread reader2 = new Thread("Reader-2")
{
public void run()
{
try
{
latch.acquire();
for (int i = 0; i < loops; i++)
{
loader.getChildrenNames(singleFqn ? fqn : (Fqn)fqns.get(r.nextInt(fqns.size())));
}
}
catch (Exception e)
{
exceptions.add(e);
}
}
};
reader2.start();
Thread writer1 = new Thread("Writer-1")
{
public void run()
{
try
{
latch.acquire();
for (int i = 0; i < loops; i++)
{
loader.put(singleFqn ? fqn : (Fqn)fqns.get(r.nextInt(fqns.size())), "k", "v");
}
}
catch (Exception e)
{
exceptions.add(e);
}
}
};
writer1.start();
Thread writer2 = new Thread("Writer-2")
{
public void run()
{
try
{
latch.acquire();
for (int i = 0; i < loops; i++)
{
loader.put(singleFqn ? fqn : (Fqn)fqns.get(r.nextInt(fqns.size())), new HashMap());
}
}
catch (Exception e)
{
exceptions.add(e);
}
}
};
writer2.start();
latch.release();
reader1.join();
reader2.join();
remover1.join();
remover2.join();
writer1.join();