Samples JDK
Test.java
1 package com.freemindcafe.concurrency.sample3;
2 
3 import java.util.concurrent.Callable;
4 import java.util.concurrent.CancellationException;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.FutureTask;
7 
8 import junit.framework.Assert;
9 
10 public class Test {
11 
12  @org.junit.Test
13  public void runnable_with_result_as_future_task() throws InterruptedException, ExecutionException{
14  Runnable r1 = () -> System.out.println("r1");
15 
16  FutureTask<Integer> ft = new FutureTask<Integer>(r1, 1);
17  ft.run();
18  Assert.assertEquals(1, ft.get().intValue());
19  }
20 
21  @org.junit.Test
22  public void callable_as_future_task() throws InterruptedException, ExecutionException{
23  Callable<Integer> c1 = () -> 1;
24 
25  FutureTask<Integer> ft = new FutureTask<Integer>(c1);
26  ft.run();
27  Assert.assertEquals(1, ft.get().intValue());
28  }
29 
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 = () -> {
33  try{
34  Thread.sleep(5000);
35  }
36  catch(InterruptedException ex){
37  //some cleanup
38  throw ex;
39  }
40  return 1;
41  };
42 
43  FutureTask<Integer> ft = new FutureTask<Integer>(c1);
44  new Thread(()->ft.run()).start();
45  ft.cancel(true);
46  Assert.assertEquals(1, ft.get().intValue());
47  }
48 
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 = () -> {
52  try{
53  Thread.sleep(5000);
54  }
55  catch(InterruptedException ex){
56  //some cleanup
57  throw new RuntimeException();
58  }
59  return 1;
60  };
61 
62  FutureTask<Integer> ft = new FutureTask<Integer>(c1);
63  new Thread(()->ft.run()).start();
64  ft.cancel(true);
65  Assert.assertEquals(1, ft.get().intValue());
66  }
67 
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 = () -> {
71  try{
72  Thread.sleep(5000);
73  }
74  catch(InterruptedException ex){
75  //some cleanup
76  }
77  return 1;
78  };
79 
80  FutureTask<Integer> ft = new FutureTask<Integer>(c1);
81  new Thread(()->ft.run()).start();
82  ft.cancel(true);
83  Assert.assertEquals(1, ft.get().intValue());
84  }
85 
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 = () -> {
89  try{
90  Thread.sleep(5000);
91  }
92  catch(InterruptedException ex){
93  //some cleanup
94  }
95  return 1;
96  };
97 
98  FutureTask<Integer> ft = new FutureTask<Integer>(c1);
99  new Thread(()->ft.run()).start();
100  ft.cancel(false);
101  Assert.assertEquals(1, ft.get().intValue());
102  }
103 
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();
108  };
109 
110  FutureTask<Integer> ft = new FutureTask<Integer>(c1);
111  new Thread(()->ft.run()).start();
112  Assert.assertEquals(1, ft.get().intValue());
113  }
114 
115  @org.junit.Test
116  public void if_task_throws_any_exceptions_then_calling_get_throws_execution_exception_(){
117  Callable<Integer> c1 = () -> {
118  throw new RuntimeException();
119  };
120 
121  FutureTask<Integer> ft = new FutureTask<Integer>(c1);
122  new Thread(()->ft.run()).start();
123  try{
124  ft.get();
125  Assert.assertTrue(false);
126  }
127  catch(InterruptedException | CancellationException ex){
128  //Empty, this will not be thrown
129  Assert.assertTrue(false);
130  }
131  catch(ExecutionException ex){
132  Throwable th = ex.getCause();
133  Assert.assertTrue(th instanceof RuntimeException);
134  }
135  }
136 
137 }