Java.util.concurrent: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 23: Line 23:
{{External|https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Callable.html}}
{{External|https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Callable.html}}


A callable represents a task that returns a result and may throw an exception. Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. The difference between a Runnable and a Callable is that the Runnable does not return a result and cannot throw a checked exception. Callable tasks can be submitted for execution to [[Java.util.concurrent_Executors#future|executors]].
A <tt>java.util.concurrent.Callable</tt> instance represents a task that returns a result and may throw an exception. Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. The difference between a Runnable and a Callable is that the Runnable does not return a result and cannot throw a checked exception. Callable tasks can be submitted for execution to [[Java.util.concurrent_Executors#future|executors]].
 
The Callable has been designated as functional interface in Java 8.


==Future==
==Future==

Revision as of 16:22, 23 March 2018

External

Internal

Overview

Java 5 introduced concurrent collections and thread pools, as part of java.util.concurrent, which is a collection of utility classes to use for concurrent programming.

Subjects

General Concepts

Callable

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Callable.html

A java.util.concurrent.Callable instance represents a task that returns a result and may throw an exception. Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. The difference between a Runnable and a Callable is that the Runnable does not return a result and cannot throw a checked exception. Callable tasks can be submitted for execution to executors.

The Callable has been designated as functional interface in Java 8.

Future

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html

A Future represents the result of an asynchronous computation. Future instances are returned by executors upon task submission. Having access to the Future instance, the caller may check if the computation is complete, wait for its completion, and retrieve the result of the computation. The result of the computation is retrieved with Future.get(), only when the computation has completed. The call will block if the computation is not ready.

The computation may be cancelled Future.cancel(). Once a computation has completed, the computation cannot be cancelled. Additional methods are provided to determine if the task completed normally or was cancelled. It is possible to use a Future with the only purpose of canceling the task, without any interest in the result. If that is the case, declare types of the form Future<?> and return null as a result of the underlying task.

TODO