|
1 | 1 | package CallableAndFuture_13; |
2 | 2 |
|
3 | | -import java.util.concurrent.Callable; |
4 | | -import java.util.concurrent.ExecutorService; |
5 | | -import java.util.concurrent.Future; |
6 | | -import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 3 | +import java.util.concurrent.*; |
7 | 4 |
|
8 | 5 | /** |
9 | | - * Source:http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html |
10 | | - * Till Java 1.4, threads could be implemented by either implementing Runnable |
11 | | - * or extending Thread. This was quite simple, but had a serious limitation - |
| 6 | + * Source: |
| 7 | + * <a href="http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html"> |
| 8 | + * http://java-x.blogspot.com.tr/2006/11/java-5-concurrency-callable-and-future.html |
| 9 | + * </a> |
| 10 | + * <p> |
| 11 | + * Till Java 1.4, threads could be implemented by either implementing |
| 12 | + * {@link java.lang.Runnable} or extending {@link java.lang.Thread}. |
| 13 | + * This was quite simple, but had a serious limitation; |
12 | 14 | * They have a run method that cannot return values. In order to side-step this, |
13 | 15 | * most programmers use side-effects (writing to a file etc.) to mimic returning |
14 | | - * values to the invoker of the thread. Java 5 introduces the Callable |
15 | | - * interface, that allows users to return values from a thread |
16 | | - * |
17 | | - * Runnable vs Callable<T> |
18 | | - * Runnable Introduced in Java 1.0 Callable<T> Introduced in Java 1.5 as part of |
19 | | - * java.util.concurrent library |
20 | | - * |
21 | | - * Runnable cannot be parametrized Callable is a parametrized type whose type |
22 | | - * parameter indicates the return type of its run method Classes implementing |
23 | | - * |
| 16 | + * values to the invoker of the thread. Java 5 introduces the |
| 17 | + * {@link java.util.concurrent.Callable} interface, that allows users to |
| 18 | + * return values from a thread. |
| 19 | + * </p> |
| 20 | + * <p> |
| 21 | + * {@link java.lang.Runnable} vs {@link java.util.concurrent.Callable} : |
| 22 | + * <ul> |
| 23 | + * <li> |
| 24 | + * Runnable Introduced in Java 1.0. Callable<T> Introduced in Java 1.5 as |
| 25 | + * part of |
| 26 | + * {@link java.util.concurrent} library. |
| 27 | + * </li> |
| 28 | + * <li> |
| 29 | + * Runnable cannot be parametrized .Callable is a parametrized type whose type |
| 30 | + * parameter indicates the return type of its run method Classes implementing. |
| 31 | + * </li> |
| 32 | + * <li> |
24 | 33 | * Runnable needs to implement run() method, classes implementing Callable needs |
25 | | - * to implement call() method |
26 | | - * |
27 | | - * Runnable.run() returns no value, Callable.call() returns a value of Type T |
28 | | - * |
| 34 | + * to implement call() method. |
| 35 | + * </li> |
| 36 | + * <li> |
| 37 | + * Runnable.run() returns no value, Callable.call() returns a value of Type T. |
| 38 | + * </li> |
| 39 | + * <li> |
29 | 40 | * Runnable can not throw checked exceptions, Callable can throw checked |
30 | | - * exceptions |
| 41 | + * exceptions. |
| 42 | + * </li> |
| 43 | + * </ul> |
| 44 | + * </p> |
31 | 45 | * |
32 | 46 | * @author Z.B. Celik <[email protected]> |
33 | 47 | */ |
@@ -58,20 +72,17 @@ public void setMyName(int myName) { |
58 | 72 |
|
59 | 73 | public class CallableTester { |
60 | 74 |
|
61 | | - public static void main(String[] args) { |
| 75 | + public static void main(String[] args) throws InterruptedException { |
62 | 76 |
|
63 | 77 | Callable<Integer> callable = new CallableImpl(2); |
64 | 78 | ExecutorService executor = new ScheduledThreadPoolExecutor(1); |
65 | 79 | Future<Integer> future = executor.submit(callable); |
66 | 80 |
|
67 | 81 | try { |
68 | 82 | System.out.println("Future value: " + future.get()); |
69 | | - } catch (Exception e) { |
70 | | - e.printStackTrace(); |
71 | | - } finally { |
72 | | - executor.shutdown(); |
73 | | - executor.isTerminated(); |
74 | | - } |
| 83 | + } catch (Exception ignored) {} |
| 84 | + executor.shutdown(); |
| 85 | + executor.awaitTermination(1, TimeUnit.HOURS); |
75 | 86 | } |
76 | 87 |
|
77 | 88 | } |
0 commit comments