diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/runnable/RunnableVsThreadTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/runnable/RunnableVsThreadTest.java index d0f4460f51..4bd4848905 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/runnable/RunnableVsThreadTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/runnable/RunnableVsThreadTest.java @@ -8,7 +8,9 @@ import java.util.concurrent.Future; import org.apache.commons.lang3.RandomUtils; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,22 +20,34 @@ public class RunnableVsThreadTest { private static Logger log = LoggerFactory.getLogger(RunnableVsThreadTest.class); + private static ExecutorService executorService; + + @BeforeClass + public static void setup() { + executorService = Executors.newCachedThreadPool(); + } + @Test public void givenARunnable_whenRunIt_thenResult() throws Exception{ Thread thread = new Thread(new SimpleRunnable( "SimpleRunnable executed using Thread")); thread.start(); thread.join(); - - ExecutorService executorService = - Executors.newCachedThreadPool(); + } + + @Test + public void givenARunnable_whenSubmitToES_thenResult() throws Exception{ executorService.submit(new SimpleRunnable( "SimpleRunnable executed using ExecutorService")).get(); + } + + @Test + public void givenARunnableLambda_whenSubmitToES_thenResult() + throws Exception{ executorService.submit(()-> log.info("Lambda runnable executed!!!")).get(); - executorService.shutdown(); } @Test @@ -42,26 +56,39 @@ public class RunnableVsThreadTest { "SimpleThread executed using Thread"); thread.start(); thread.join(); + } + + @Test + public void givenAThread_whenSubmitToES_thenResult() throws Exception{ - ExecutorService executorService = - Executors.newCachedThreadPool(); executorService.submit(new SimpleThread( "SimpleThread executed using ExecutorService")).get(); } @Test - public void givenACallable_whenRunIt_thenResult() throws Exception { - ExecutorService executorService = - Executors.newCachedThreadPool(); + public void givenACallable_whenSubmitToES_thenResult() throws Exception { - Future future = executorService.submit(new SimpleCallable()); + Future future = executorService.submit( + new SimpleCallable()); log.info("Result from callable: {}", future.get()); + } + + @Test + public void givenACallableAsLambda_whenSubmitToES_thenResult() + throws Exception { - future = executorService.submit(() -> { + Future future = executorService.submit(() -> { return RandomUtils.nextInt(0, 100); - }); + }); + log.info("Result from callable: {}", future.get()); - + } + + @AfterClass + public static void tearDown() { + if ( executorService != null && !executorService.isShutdown()) { + executorService.shutdown(); + } } } @@ -107,4 +134,5 @@ class SimpleCallable implements Callable { return RandomUtils.nextInt(0, 100); } -} \ No newline at end of file +} +