void () { StringBuffer buf = new StringBuffer(); StringBuilder expected = new StringBuilder(); for(int i = 0; i < 100; i++) { expected.append("1"); expected.append("2"); CompositePrintable.invokeInAlarm(() -> buf.append("1"), false); CompositePrintable.invokeInAlarm(() -> buf.append("2"), false); } Semaphore s = new Semaphore(); s.down(); CompositePrintable.invokeInAlarm(s::up, false); assertTrue(s.waitFor(1000)); assertEquals(expected.toString(), buf.toString()); }