Skip to content

Commit bfa2172

Browse files
committed
Replaced the IOException with a TimeoutException (better example :P )
JavaDoc optimized
1 parent 9192686 commit bfa2172

File tree

1 file changed

+32
-28
lines changed
  • JavaMultiThreadingCodes/src/CallableAndFuture_13

1 file changed

+32
-28
lines changed

JavaMultiThreadingCodes/src/CallableAndFuture_13/App.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,71 @@
11
package CallableAndFuture_13;
22

3+
import java.util.Random;
4+
import java.util.concurrent.*;
5+
36
/**
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
510
* your threads to throw exceptions. Plus, Future allows you to control your
611
* 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
1118
* 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>
1526
* 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>
1730
*
1831
* @author Z.B. Celik <[email protected]>
1932
*/
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-
2933
public class App {
3034

3135
public static void main(String[] args) throws InterruptedException {
3236
ExecutorService executor = Executors.newCachedThreadPool();
37+
3338
//anonymous call of Callable
3439
Future<Integer> future = executor.submit(new Callable<Integer>() {
3540

3641
@Override
3742
//return value is Integer
38-
public Integer call() throws Exception {
43+
public Integer call() throws TimeoutException {
3944
Random random = new Random();
4045
int duration = random.nextInt(4000);
4146
if (duration > 2000) {
42-
throw new IOException("Sleeping for too long.");
47+
throw new TimeoutException ("Sleeping for too long.");
4348
}
44-
System.out.println("Starting ...");
4549

50+
System.out.println("Starting ...");
4651
try {
4752
Thread.sleep(duration);
48-
} catch (InterruptedException e) {
49-
e.printStackTrace();
50-
}
53+
} catch (InterruptedException ignored) {}
5154
System.out.println("Finished.");
5255
return duration;
5356
}
5457
});
5558

5659
executor.shutdown();
57-
executor.awaitTermination(1, TimeUnit.DAYS);
60+
// executor.awaitTermination(1, TimeUnit.DAYS);
5861
try {
5962
//get returned value from call()
6063
System.out.println("Result is: " + future.get());
61-
} catch (InterruptedException e) {
62-
e.printStackTrace();
64+
65+
} catch (InterruptedException ignored) {
66+
6367
} catch (ExecutionException e) {
64-
IOException ex = (IOException) e.getCause();
68+
TimeoutException ex = (TimeoutException) e.getCause();
6569
System.out.println(ex.getMessage());
6670
}
6771
}

0 commit comments

Comments
 (0)