1 package com.freemindcafe.concurrency.sample3;
3 import java.util.concurrent.Callable;
4 import java.util.concurrent.CancellationException;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.FutureTask;
8 import junit.framework.Assert;
13 public void runnable_with_result_as_future_task()
throws InterruptedException, ExecutionException{
14 Runnable r1 = () -> System.out.println(
"r1");
16 FutureTask<Integer> ft =
new FutureTask<Integer>(r1, 1);
18 Assert.assertEquals(1, ft.get().intValue());
22 public void callable_as_future_task()
throws InterruptedException, ExecutionException{
23 Callable<Integer> c1 = () -> 1;
25 FutureTask<Integer> ft =
new FutureTask<Integer>(c1);
27 Assert.assertEquals(1, ft.get().intValue());
30 @
org.junit.Test(expected=CancellationException.class)
31 public void calling_get_on_cancelled_task_throws_cancellation_exception()
throws InterruptedException, ExecutionException{
32 Callable<Integer> c1 = () -> {
36 catch(InterruptedException ex){
43 FutureTask<Integer> ft =
new FutureTask<Integer>(c1);
44 new Thread(()->ft.run()).start();
46 Assert.assertEquals(1, ft.get().intValue());
49 @
org.junit.Test(expected=CancellationException.class)
50 public void calling_get_on_cancelled_task_throws_cancellation_exception_even_if_task_thorows_other_exception()
throws InterruptedException, ExecutionException{
51 Callable<Integer> c1 = () -> {
55 catch(InterruptedException ex){
57 throw new RuntimeException();
62 FutureTask<Integer> ft =
new FutureTask<Integer>(c1);
63 new Thread(()->ft.run()).start();
65 Assert.assertEquals(1, ft.get().intValue());
68 @
org.junit.Test(expected=CancellationException.class)
69 public void calling_get_on_cancelled_task_throws_cancellation_exception_even_if_task_does_not_cancel()
throws InterruptedException, ExecutionException{
70 Callable<Integer> c1 = () -> {
74 catch(InterruptedException ex){
80 FutureTask<Integer> ft =
new FutureTask<Integer>(c1);
81 new Thread(()->ft.run()).start();
83 Assert.assertEquals(1, ft.get().intValue());
86 @
org.junit.Test(expected=CancellationException.class)
87 public void calling_get_on_cancelled_task_throws_cancellation_exception_even_if_cancel_was_non_interruptible()
throws InterruptedException, ExecutionException{
88 Callable<Integer> c1 = () -> {
92 catch(InterruptedException ex){
98 FutureTask<Integer> ft =
new FutureTask<Integer>(c1);
99 new Thread(()->ft.run()).start();
101 Assert.assertEquals(1, ft.get().intValue());
104 @
org.junit.Test(expected=ExecutionException.class)
105 public void if_task_throws_any_exceptions_then_calling_get_throws_execution_exception()
throws InterruptedException, ExecutionException{
106 Callable<Integer> c1 = () -> {
107 throw new RuntimeException();
110 FutureTask<Integer> ft =
new FutureTask<Integer>(c1);
111 new Thread(()->ft.run()).start();
112 Assert.assertEquals(1, ft.get().intValue());
116 public void if_task_throws_any_exceptions_then_calling_get_throws_execution_exception_(){
117 Callable<Integer> c1 = () -> {
118 throw new RuntimeException();
121 FutureTask<Integer> ft =
new FutureTask<Integer>(c1);
122 new Thread(()->ft.run()).start();
125 Assert.assertTrue(
false);
127 catch(InterruptedException | CancellationException ex){
129 Assert.assertTrue(
false);
131 catch(ExecutionException ex){
132 Throwable th = ex.getCause();
133 Assert.assertTrue(th instanceof RuntimeException);