### Question:
CopyOnWriteWeakSet extends WeakSet<T> { @Override public synchronized void add(T t) { maybeWarnAboutAnonymousValue(t); weakHashStorage = new WeakHashMap<>(weakHashStorage); weakHashStorage.put(t, null); } CopyOnWriteWeakSet(); @Override synchronized Iterator<T> iterator(); @Override synchronized void add(T t); @Override synchronized void remove(T t); @Override synchronized void clear(); @Override synchronized Collection<T> values(); @Override synchronized boolean isEmpty(); @Override synchronized int size(); @Override synchronized boolean contains(T t);  }### Answer:
@Test public void testIterationWithModification_Add() throws InterruptedException { final CountDownLatch finishedLatch = new CountDownLatch(1); final CopyOnWriteWeakSet<Listener> set = new CopyOnWriteWeakSet<Listener>(); LatchListener latchListener = new LatchListener(); for (int i = 0; i < 5; i++) { Listener listener = new Listener(); listeners.add(listener); set.add(listener); } listeners.add(latchListener); set.add(latchListener); for (int i = 0; i < 5; i++) { Listener listener = new Listener(); listeners.add(listener); set.add(listener); } final AtomicReference<Throwable> exception = new AtomicReference<Throwable>(); new Thread(new Runnable() { @Override public void run() { try { for (Listener listener : set) { listener.doIt(); } } catch (Throwable t) { exception.set(t); } finishedLatch.countDown(); } }).start(); latchListener.waitForStart(); Listener listener = new Listener(); set.add(listener); latchListener.release(); finishedLatch.await(5, TimeUnit.SECONDS); assertNull("Found exception while processing set: " + exception.get(), exception.get()); }