/** * Test usage from multiple threads. * * @throws InterruptedException if interrupted */ public final void testInstancesUsedFromMultipleThreads() throws InterruptedException { final Set<Touchable> set = Collections.synchronizedSet(new HashSet<Touchable>()); final List<Touchable> list = Collections.synchronizedList(new ArrayList<Touchable>()); final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized( new ConstructorInjection.ConstructorInjector( Touchable.class, SimpleTouchable.class, null)); final Touchable touchable = (Touchable) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); final Thread[] threads = { new Thread(new Runner(touchable, list, set), "junit-1"), new Thread(new Runner(touchable, list, set), "junit-2"), new Thread(new Runner(touchable, list, set), "junit-3"), }; for (int i = threads.length; i-- > 0; ) { threads[i].start(); } Thread.sleep(300); for (int i = threads.length; i-- > 0; ) { synchronized (threads[i]) { threads[i].notifyAll(); } } Thread.sleep(300); for (int i = threads.length; i-- > 0; ) { threads[i].interrupt(); } Thread.sleep(300); assertEquals(6, list.size()); assertEquals(3, set.size()); }