| 
1 | 1 | package CallableAndFuture_13;  | 
2 | 2 | 
 
  | 
 | 3 | +import java.util.Random;  | 
 | 4 | +import java.util.concurrent.*;  | 
 | 5 | + | 
3 | 6 | /**  | 
4 |  | - * Callable and Future in Java to get results from your threads and to allow  | 
 | 7 | + * {@link java.util.concurrent.Callable} and  | 
 | 8 | + * {@link java.util.concurrent.Future}  | 
 | 9 | + * in Java to get results from your threads and to allow  | 
5 | 10 |  * your threads to throw exceptions. Plus, Future allows you to control your  | 
6 | 11 |  * threads, checking to see if they’re running or not, waiting for results and  | 
7 |  | - * even interrupting them or descheduling them.  | 
8 |  | - *  | 
9 |  | - * Runnable is default abstraction for creating a task in Java. It has a single  | 
10 |  | - * method run() that accepts no arguments and returns no value, nor it can throw  | 
 | 12 | + * even interrupting them or de-scheduling them.  | 
 | 13 | + * <p>  | 
 | 14 | + * {@link java.lang.Runnable}  | 
 | 15 | + * is the default abstraction for creating a task in Java. It has a single  | 
 | 16 | + * method {@link Runnable#run()}  | 
 | 17 | + * that accepts no arguments and returns no value, nor it can throw  | 
11 | 18 |  * any checked exception. To overcome these limitations, Java 5 introduced a new  | 
12 |  | - * task abstraction through Callable interface.  | 
13 |  | - *  | 
14 |  | - * Codes with minor comments are from http://www.caveofprogramming.com/youtube/  | 
 | 19 | + * task abstraction through {@link java.util.concurrent.Callable} interface.  | 
 | 20 | + * <br><br>  | 
 | 21 | + * Codes with minor comments are from  | 
 | 22 | + * <a href="http://www.caveofprogramming.com/youtube/">  | 
 | 23 | + * <em>http://www.caveofprogramming.com/youtube/</em>  | 
 | 24 | + * </a>  | 
 | 25 | + * <br>  | 
15 | 26 |  * also freely available at  | 
16 |  | - * https://www.udemy.com/java-multithreading/?couponCode=FREE  | 
 | 27 | + * <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">  | 
 | 28 | + * <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>  | 
 | 29 | + * </a>  | 
17 | 30 |  *  | 
18 | 31 |  * @author Z.B. Celik <[email protected]>  | 
19 | 32 |  */  | 
20 |  | -import java.io.IOException;  | 
21 |  | -import java.util.Random;  | 
22 |  | -import java.util.concurrent.Callable;  | 
23 |  | -import java.util.concurrent.ExecutionException;  | 
24 |  | -import java.util.concurrent.ExecutorService;  | 
25 |  | -import java.util.concurrent.Executors;  | 
26 |  | -import java.util.concurrent.Future;  | 
27 |  | -import java.util.concurrent.TimeUnit;  | 
28 |  | - | 
29 | 33 | public class App {  | 
30 | 34 | 
 
  | 
31 | 35 |     public static void main(String[] args) throws InterruptedException {  | 
32 | 36 |         ExecutorService executor = Executors.newCachedThreadPool();  | 
 | 37 | + | 
33 | 38 |         //anonymous call of Callable  | 
34 | 39 |         Future<Integer> future = executor.submit(new Callable<Integer>() {  | 
35 | 40 | 
 
  | 
36 | 41 |             @Override  | 
37 | 42 |             //return value is Integer  | 
38 |  | -            public Integer call() throws Exception {  | 
 | 43 | +            public Integer call() throws TimeoutException {  | 
39 | 44 |                 Random random = new Random();  | 
40 | 45 |                 int duration = random.nextInt(4000);  | 
41 | 46 |                 if (duration > 2000) {  | 
42 |  | -                    throw new IOException("Sleeping for too long.");  | 
 | 47 | +                    throw new TimeoutException ("Sleeping for too long.");  | 
43 | 48 |                 }  | 
44 |  | -                System.out.println("Starting ...");  | 
45 | 49 | 
 
  | 
 | 50 | +                System.out.println("Starting ...");  | 
46 | 51 |                 try {  | 
47 | 52 |                     Thread.sleep(duration);  | 
48 |  | -                } catch (InterruptedException e) {  | 
49 |  | -                    e.printStackTrace();  | 
50 |  | -                }  | 
 | 53 | +                } catch (InterruptedException ignored) {}  | 
51 | 54 |                 System.out.println("Finished.");  | 
52 | 55 |                 return duration;  | 
53 | 56 |             }  | 
54 | 57 |         });  | 
55 | 58 | 
 
  | 
56 | 59 |         executor.shutdown();  | 
57 |  | -        executor.awaitTermination(1, TimeUnit.DAYS);  | 
 | 60 | +//        executor.awaitTermination(1, TimeUnit.DAYS);  | 
58 | 61 |         try {  | 
59 | 62 |             //get returned value from call()  | 
60 | 63 |             System.out.println("Result is: " + future.get());  | 
61 |  | -        } catch (InterruptedException e) {  | 
62 |  | -            e.printStackTrace();  | 
 | 64 | + | 
 | 65 | +        } catch (InterruptedException ignored) {  | 
 | 66 | + | 
63 | 67 |         } catch (ExecutionException e) {  | 
64 |  | -            IOException ex = (IOException) e.getCause();  | 
 | 68 | +            TimeoutException ex = (TimeoutException) e.getCause();  | 
65 | 69 |             System.out.println(ex.getMessage());  | 
66 | 70 |         }  | 
67 | 71 |     }  | 
 | 
0 commit comments