From 904a740643a36204e80ddafbeb99a49561463c3c Mon Sep 17 00:00:00 2001 From: baljeet20 Date: Tue, 29 Aug 2017 01:01:31 +0530 Subject: [PATCH] BAEL-1071 Runnable vs Callable in Java (#2513) --- .../concurrent/callable/FactorialTask.java | 30 ++++++++++++ .../callable/FactorialTaskManualTest.java | 48 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/callable/FactorialTask.java create mode 100644 core-java-concurrency/src/test/java/com/baeldung/concurrent/callable/FactorialTaskManualTest.java diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/callable/FactorialTask.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/callable/FactorialTask.java new file mode 100644 index 0000000000..8663609d2d --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/callable/FactorialTask.java @@ -0,0 +1,30 @@ +package com.baeldung.concurrent.callable; + + +import java.util.concurrent.Callable; + +public class FactorialTask implements Callable { + int number; + + public FactorialTask(int number) { + this.number = number; + } + + public Integer call() throws InvalidParamaterException { + int fact=1; + if(number < 0) + throw new InvalidParamaterException("Number must be positive"); + + for(int count=number;count>1;count--){ + fact=fact * count; + } + + return fact; + } + + private class InvalidParamaterException extends Exception { + public InvalidParamaterException(String message) { + super(message); + } + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/callable/FactorialTaskManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/callable/FactorialTaskManualTest.java new file mode 100644 index 0000000000..c55e04af94 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/callable/FactorialTaskManualTest.java @@ -0,0 +1,48 @@ +package com.baeldung.concurrent.callable; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import static junit.framework.Assert.assertEquals; + +public class FactorialTaskManualTest { + + private ExecutorService executorService; + + @Before + public void setup(){ + executorService = Executors.newSingleThreadExecutor(); + } + + @Test + public void whenTaskSubmitted_ThenFutureResultObtained() throws ExecutionException, InterruptedException { + FactorialTask task = new FactorialTask(5); + Future future= executorService.submit(task); + assertEquals(120,future.get().intValue()); + } + + @Test(expected = ExecutionException.class) + public void whenException_ThenCallableThrowsIt() throws ExecutionException, InterruptedException { + FactorialTask task = new FactorialTask(-5); + Future future= executorService.submit(task); + Integer result=future.get().intValue(); + } + + @Test + public void whenException_ThenCallableDoesntThrowsItIfGetIsNotCalled(){ + FactorialTask task = new FactorialTask(-5); + Future future= executorService.submit(task); + assertEquals(false,future.isDone()); + } + + @After + public void cleanup(){ + executorService.shutdown(); + } +}