BAEL-1071 Runnable vs Callable in Java (#2513)
This commit is contained in:
parent
73526dd0a8
commit
904a740643
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.concurrent.callable;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
public class FactorialTask implements Callable<Integer> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<Integer> 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<Integer> future= executorService.submit(task);
|
||||||
|
Integer result=future.get().intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenException_ThenCallableDoesntThrowsItIfGetIsNotCalled(){
|
||||||
|
FactorialTask task = new FactorialTask(-5);
|
||||||
|
Future<Integer> future= executorService.submit(task);
|
||||||
|
assertEquals(false,future.isDone());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void cleanup(){
|
||||||
|
executorService.shutdown();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user