Sdílet prostřednictvím


ExecutorCompletionService Class

Definition

A CompletionService that uses a supplied Executor to execute tasks.

[Android.Runtime.Register("java/util/concurrent/ExecutorCompletionService", DoNotGenerateAcw=true)]
[Java.Interop.JavaTypeParameters(new System.String[] { "V" })]
public class ExecutorCompletionService : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.Util.Concurrent.ICompletionService
[<Android.Runtime.Register("java/util/concurrent/ExecutorCompletionService", DoNotGenerateAcw=true)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "V" })>]
type ExecutorCompletionService = class
    inherit Object
    interface ICompletionService
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
ExecutorCompletionService
Attributes
Implements

Remarks

A CompletionService that uses a supplied Executor to execute tasks. This class arranges that submitted tasks are, upon completion, placed on a queue accessible using take. The class is lightweight enough to be suitable for transient use when processing groups of tasks.

<b>Usage Examples.</b>

Suppose you have a set of solvers for a certain problem, each returning a value of some type Result, and would like to run them concurrently, processing the results of each of them that return a non-null value, in some method use(Result r). You could write this as:

{@code
            void solve(Executor e,
                       Collection<Callable<Result>> solvers)
                throws InterruptedException, ExecutionException {
              CompletionService<Result> cs
                  = new ExecutorCompletionService<>(e);
              solvers.forEach(cs::submit);
              for (int i = solvers.size(); i > 0; i--) {
                Result r = cs.take().get();
                if (r != null)
                  use(r);
              }
            }}

Suppose instead that you would like to use the first non-null result of the set of tasks, ignoring any that encounter exceptions, and cancelling all other tasks when the first one is ready:

{@code
            void solve(Executor e,
                       Collection<Callable<Result>> solvers)
                throws InterruptedException {
              CompletionService<Result> cs
                  = new ExecutorCompletionService<>(e);
              int n = solvers.size();
              List<Future<Result>> futures = new ArrayList<>(n);
              Result result = null;
              try {
                solvers.forEach(solver -> futures.add(cs.submit(solver)));
                for (int i = n; i > 0; i--) {
                  try {
                    Result r = cs.take().get();
                    if (r != null) {
                      result = r;
                      break;
                    }
                  } catch (ExecutionException ignore) {}
                }
              } finally {
                futures.forEach(future -> future.cancel(true));
              }

              if (result != null)
                use(result);
            }}

Added in 1.5.

Java documentation for java.util.concurrent.ExecutorCompletionService.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

ExecutorCompletionService(IExecutor, IBlockingQueue)

Creates an ExecutorCompletionService using the supplied executor for base task execution and the supplied queue as its completion queue.

ExecutorCompletionService(IExecutor)

Creates an ExecutorCompletionService using the supplied executor for base task execution and a LinkedBlockingQueue as a completion queue.

ExecutorCompletionService(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Poll()

Retrieves and removes the Future representing the next completed task, or null if none are present.

Poll(Int64, TimeUnit)

Retrieves and removes the Future representing the next completed task, waiting if necessary up to the specified wait time if none are yet present.

PollAsync()
PollAsync(Int64, TimeUnit)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
Submit(ICallable)
Submit(IRunnable, Object)
Take()

Retrieves and removes the Future representing the next completed task, waiting if none are yet present.

TakeAsync()
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to