From 4b77e378b747ad8ef40f8cf676f80f706a6a7c12 Mon Sep 17 00:00:00 2001 From: baljeet20 Date: Thu, 31 Aug 2017 18:33:42 +0530 Subject: [PATCH] BAEL-1071 Added new example (#2532) * BAEL-1071 Runnable vs Callable in Java * BAEL-1071 Runnable vs Callable in Java * BAEL-1071 Runnable vs Callable in Java --- .../concurrent/runnable/EventLoggingTask.java | 17 +++++++++++++ .../concurrent/runnable/TaskRunner.java | 25 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/EventLoggingTask.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/TaskRunner.java diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/EventLoggingTask.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/EventLoggingTask.java new file mode 100644 index 0000000000..d65f79202e --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/EventLoggingTask.java @@ -0,0 +1,17 @@ +package com.baeldung.concurrent.runnable; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventLoggingTask implements Runnable{ + private Logger logger = LoggerFactory.getLogger(EventLoggingTask.class); + + @Override + public void run() { + + String messge="Message read from the event queue"; + logger.info("Message read from event queue is "+messge); + + } +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/TaskRunner.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/TaskRunner.java new file mode 100644 index 0000000000..8fd98e77b8 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/runnable/TaskRunner.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.runnable; + + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +public class TaskRunner { + + private static ExecutorService executorService; + + public static void main(String[] args) { + executeTask(); + } + + private static void executeTask() { + executorService= Executors.newSingleThreadExecutor(); + + EventLoggingTask task = new EventLoggingTask(); + + Future future = executorService.submit(task); + + executorService.shutdown(); + } +}