Java.util.concurrent: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(42 intermediate revisions by the same user not shown)
Line 1: Line 1:
<br>
<br>
<center>in process of being migrated from https://home.feodorov.com:9443/wiki/Wiki.jsp?page=JavaConcurrent</center>
=External=
=External=


Line 9: Line 5:
=Internal=
=Internal=


* [[Java#Subjects|java]]
* [[Java#Java_5|Java]]


=Overview=
=Overview=


<font>java.util.concurrent</font> is a collection of utility classes to use for concurrent programming.
Java 5 introduced concurrent collections and thread pools, as part of <font>java.util.concurrent</font>, which is a collection of utility classes to use for concurrent programming.
 
=Callable=
 
{{External|https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Callable.html}}
 
<syntaxhighlight lang='java'>
package java.util.concurrent;
 
@FunctionalInterface
public interface Callable<V> {
    V call() throws Exception;
}
</syntaxhighlight>
 
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 <tt>call()</tt>. 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]].
 
Usage pattern:
 
<syntaxhighlight lang='java'>
ExecutorService executor = ...;
 
final String arg = "something";
 
Future<String> future = executor.submit(new Callable<String>() {
 
    @Override
    public String call() throws Exception {
 
          return arg.toUpperCase();
    }
});
 
String result = future.get();
 
</syntaxhighlight>
 
The Callable has been designated as [[Java_8_Lambda_Expressions#Functional_Interface|functional interface]] in Java 8. Callable instances can be replaced with lambda expressions, the above example is equivalent with:
 
<syntaxhighlight lang='java'>
ExecutorService executor = ...;
 
final String arg = "something";
 
Future<String> future = executor.submit(() -> arg.toUpperCase());
 
String result = future.get();
 
</syntaxhighlight>
 
=Future=
 
{{External|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 [[Java.util.concurrent_Executors#future|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 <tt>Future.get()</tt>, only when the computation has completed. The call will block if the computation is not ready.
 
The computation may be cancelled <tt>Future.cancel()</tt>. 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 <tt>Future<?></tt> and return null as a result of the underlying task.
 
=Executors=
 
{{Internal|java.util.concurrent Executors|Executors}}
 
=Queues=
 
{{Internal|java.util.concurrent Queues|Queues}}
 
=Synchronizers=
 
Include CountDownLatch, Exchanger, etc. There is no "Synchronizer" interface though.
 
{{Internal|java.util.concurrent Synchronizers|Synchronizers}}
 
=CopyOnWriteArrayList=
{{External|https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/CopyOnWriteArrayList.html}}
<font color=darkgray>TODO. Use case: [[JMX#NotificationEmitter]]</font>
 
=TODO=
 
<font color=red>


=Subjects=
* Atomic*
* [[Java_7_Fork/Join_Framework|Java 7 Fork/Join Framework]]


* [[java.util.concurrent Executors|Executors]]
</font>
* [[java.util.concurrent Queues|Queues]]

Latest revision as of 21:43, 18 February 2020

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.

Callable

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

@FunctionalInterface
public interface Callable<V> {
    V call() throws Exception;
}

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.

Usage pattern:

ExecutorService executor = ...;

final String arg = "something";

Future<String> future = executor.submit(new Callable<String>() {

     @Override
     public String call() throws Exception {

          return arg.toUpperCase();
     }
});

String result = future.get();

The Callable has been designated as functional interface in Java 8. Callable instances can be replaced with lambda expressions, the above example is equivalent with:

ExecutorService executor = ...;

final String arg = "something";

Future<String> future = executor.submit(() -> arg.toUpperCase());

String result = future.get();

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.

Executors

Executors

Queues

Queues

Synchronizers

Include CountDownLatch, Exchanger, etc. There is no "Synchronizer" interface though.

Synchronizers

CopyOnWriteArrayList

https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/CopyOnWriteArrayList.html

TODO. Use case: JMX#NotificationEmitter

TODO