Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replaced the IOException with a TimeoutException (better example :P )
JavaDoc optimized
  • Loading branch information
IOAyman committed Jan 24, 2015
commit bfa2172b30396822b8e1829df70e62cec87af769
60 changes: 32 additions & 28 deletions JavaMultiThreadingCodes/src/CallableAndFuture_13/App.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,71 @@
package CallableAndFuture_13;

import java.util.Random;
import java.util.concurrent.*;

/**
* Callable and Future in Java to get results from your threads and to allow
* {@link java.util.concurrent.Callable} and
* {@link java.util.concurrent.Future}
* in Java to get results from your threads and to allow
* your threads to throw exceptions. Plus, Future allows you to control your
* threads, checking to see if they’re running or not, waiting for results and
* even interrupting them or descheduling them.
*
* Runnable is default abstraction for creating a task in Java. It has a single
* method run() that accepts no arguments and returns no value, nor it can throw
* even interrupting them or de-scheduling them.
* <p>
* {@link java.lang.Runnable}
* is the default abstraction for creating a task in Java. It has a single
* method {@link Runnable#run()}
* that accepts no arguments and returns no value, nor it can throw
* any checked exception. To overcome these limitations, Java 5 introduced a new
* task abstraction through Callable interface.
*
* Codes with minor comments are from http://www.caveofprogramming.com/youtube/
* task abstraction through {@link java.util.concurrent.Callable} interface.
* <br><br>
* Codes with minor comments are from
* <a href="http://www.caveofprogramming.com/youtube/">
* <em>http://www.caveofprogramming.com/youtube/</em>
* </a>
* <br>
* also freely available at
* https://www.udemy.com/java-multithreading/?couponCode=FREE
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
* </a>
*
* @author Z.B. Celik <[email protected]>
*/
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class App {

public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newCachedThreadPool();

//anonymous call of Callable
Future<Integer> future = executor.submit(new Callable<Integer>() {

@Override
//return value is Integer
public Integer call() throws Exception {
public Integer call() throws TimeoutException {
Random random = new Random();
int duration = random.nextInt(4000);
if (duration > 2000) {
throw new IOException("Sleeping for too long.");
throw new TimeoutException ("Sleeping for too long.");
}
System.out.println("Starting ...");

System.out.println("Starting ...");
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (InterruptedException ignored) {}
System.out.println("Finished.");
return duration;
}
});

executor.shutdown();
executor.awaitTermination(1, TimeUnit.DAYS);
// executor.awaitTermination(1, TimeUnit.DAYS);
try {
//get returned value from call()
System.out.println("Result is: " + future.get());
} catch (InterruptedException e) {
e.printStackTrace();

} catch (InterruptedException ignored) {

} catch (ExecutionException e) {
IOException ex = (IOException) e.getCause();
TimeoutException ex = (TimeoutException) e.getCause();
System.out.println(ex.getMessage());
}
}
Expand Down