From the course: Parallel and Concurrent Programming with Java 2
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Future: Java demo - Java Tutorial
From the course: Parallel and Concurrent Programming with Java 2
Future: Java demo
- [Instructor] To get the result back from an asynchronous task in Java I can use the callable interface which represents a task that returns a result of a specified type. The callable interface is similar to Java's runnable interface except that instead of having a method named run that returns void the callable interface has a single method named call which returns a value of some type and may throw an exception as well. To demonstrate creating and using a callable, I'll start with this basic shell of a Java program. First, I'll import the java.util.concurrent package into this program. Then I'll create a new class called HowManyVegetables that implements the callable interface. Within that class I'll create a public method named call that returns an integer value and throws an exception. My call method will print a message that Olivia is counting vegetables. Then I'll have it sleep for three seconds to simulate the time it takes for her to complete that task. And finally the method…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.