From 080af1cb45f2a17d4e8834f3773a406e62da5f71 Mon Sep 17 00:00:00 2001 From: Mohamed Sanaulla Date: Thu, 26 Oct 2017 01:11:28 +0300 Subject: [PATCH] adding code for BAEL-1262 (#2866) * adding code for BAEL-1262 * fixing the build issue by moving the test to test folder --- .../runnable/RunnableVsThreadTest.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 core-java-concurrency/src/test/java/com/baeldung/concurrent/runnable/RunnableVsThreadTest.java 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 new file mode 100644 index 0000000000..d0f4460f51 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/runnable/RunnableVsThreadTest.java @@ -0,0 +1,110 @@ +package com.baeldung.concurrent.runnable; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.apache.commons.lang3.RandomUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RunnableVsThreadTest { + + private static Logger log = + LoggerFactory.getLogger(RunnableVsThreadTest.class); + + @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(); + + executorService.submit(new SimpleRunnable( + "SimpleRunnable executed using ExecutorService")).get(); + + executorService.submit(()-> + log.info("Lambda runnable executed!!!")).get(); + executorService.shutdown(); + } + + @Test + public void givenAThread_whenRunIt_thenResult() throws Exception{ + Thread thread = new SimpleThread( + "SimpleThread executed using Thread"); + thread.start(); + thread.join(); + + 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(); + + Future future = executorService.submit(new SimpleCallable()); + log.info("Result from callable: {}", future.get()); + + future = executorService.submit(() -> { + return RandomUtils.nextInt(0, 100); + }); + log.info("Result from callable: {}", future.get()); + + } +} + +class SimpleThread extends Thread{ + + private static final Logger log = + LoggerFactory.getLogger(SimpleThread.class); + + private String message; + + public SimpleThread(String message) { + this.message = message; + } + + @Override + public void run() { + log.info(message); + } +} + +class SimpleRunnable implements Runnable { + + private static final Logger log = + LoggerFactory.getLogger(SimpleRunnable.class); + + private String message; + + public SimpleRunnable(String message) { + this.message = message; + } + + + @Override + public void run() { + log.info(message); + } +} + +class SimpleCallable implements Callable { + + @Override + public Integer call() throws Exception { + return RandomUtils.nextInt(0, 100); + } + +} \ No newline at end of file