Source:
@Test public void whenFewTreadsDoTheSameWork() throws Throwable { TestThread runTestThread = new TestThread(run); TestThread runTestThread2 = new TestThread(run2); TestThread isRunningTestThread = new TestThread(isRunning); TestThread isRunningTestThread2 = new TestThread(isRunning2); TestThread stopTestThread = new TestThread(stop); TestThread stopTestThread2 = new TestThread(stop2); Thread runThread = new Thread(runTestThread); Thread runThread2 = new Thread(runTestThread2); Thread isRunningThread = new Thread(isRunningTestThread); Thread isRunningThread2 = new Thread(isRunningTestThread2); Thread stopThread = new Thread(stopTestThread); Thread stopThread2 = new Thread(stopTestThread2); try { runThread.start(); runThread2.start(); while (runThread.isAlive() || runThread2.isAlive()) { } if (runTestThread.throwable != null) { throw runTestThread.throwable; } if (runTestThread2.throwable != null) { throw runTestThread2.throwable; } assertEquals("OK", runTestThread.result); assertEquals("OK", runTestThread2.result); assertTrue(service.isRunning()); isRunningThread.start(); isRunningThread2.start(); while (isRunningThread.isAlive() || isRunningThread2.isAlive()) { } if (isRunningTestThread.throwable != null) { throw isRunningTestThread.throwable; } if (isRunningTestThread2.throwable != null) { throw isRunningTestThread2.throwable; } assertEquals(true, isRunningTestThread.result); assertEquals(true, isRunningTestThread2.result); stopThread.start(); stopThread2.start(); while (stopThread.isAlive() || stopThread2.isAlive()) { } if (stopTestThread.throwable != null) { throw stopTestThread.throwable; } if (stopTestThread2.throwable != null) { throw stopTestThread2.throwable; } assertEquals("OK", stopTestThread.result); assertEquals("OK", stopTestThread2.result); assertFalse(service.isRunning()); } finally { if (service.isRunning()) { service.stop(); } } }

Target:
@Test public void whenFewTreadsDoTheSameWork() throws Throwable { TestThread<String> runTestThread = new TestThread<>(run); TestThread<String> runTestThread2 = new TestThread<>(run2); TestThread<Boolean> isRunningTestThread = new TestThread<>(isRunning); TestThread<Boolean> isRunningTestThread2 = new TestThread<>(isRunning2); TestThread<String> stopTestThread = new TestThread<>(stop); TestThread<String> stopTestThread2 = new TestThread<>(stop2); Thread runThread = new Thread(runTestThread); Thread runThread2 = new Thread(runTestThread2); Thread isRunningThread = new Thread(isRunningTestThread); Thread isRunningThread2 = new Thread(isRunningTestThread2); Thread stopThread = new Thread(stopTestThread); Thread stopThread2 = new Thread(stopTestThread2); try { runThread.start(); runThread2.start(); while (runThread.isAlive() || runThread2.isAlive()) { } if (runTestThread.throwable != null) { throw runTestThread.throwable; } if (runTestThread2.throwable != null) { throw runTestThread2.throwable; } assertEquals("OK", runTestThread.result); assertEquals("OK", runTestThread2.result); assertTrue(service.isRunning()); isRunningThread.start(); isRunningThread2.start(); while (isRunningThread.isAlive() || isRunningThread2.isAlive()) { } if (isRunningTestThread.throwable != null) { throw isRunningTestThread.throwable; } if (isRunningTestThread2.throwable != null) { throw isRunningTestThread2.throwable; } assertTrue(isRunningTestThread.result); assertTrue(isRunningTestThread2.result); stopThread.start(); stopThread2.start(); while (stopThread.isAlive() || stopThread2.isAlive()) { } if (stopTestThread.throwable != null) { throw stopTestThread.throwable; } if (stopTestThread2.throwable != null) { throw stopTestThread2.throwable; } assertEquals("OK", stopTestThread.result); assertEquals("OK", stopTestThread2.result); assertFalse(service.isRunning()); } finally { if (service.isRunning()) { service.stop(); } } }