Flow Class

Definition

Interrelated interfaces and static methods for establishing flow-controlled components in which Publisher Publishers produce items consumed by one or more Subscriber Subscribers, each managed by a Subscription Subscription.

[Android.Runtime.Register("java/util/concurrent/Flow", ApiSince=30, DoNotGenerateAcw=true)]
public sealed class Flow : Java.Lang.Object
[<Android.Runtime.Register("java/util/concurrent/Flow", ApiSince=30, DoNotGenerateAcw=true)>]
type Flow = class
    inherit Object
Inheritance
Flow
Attributes

Remarks

Interrelated interfaces and static methods for establishing flow-controlled components in which Publisher Publishers produce items consumed by one or more Subscriber Subscribers, each managed by a Subscription Subscription.

These interfaces correspond to the reactive-streams specification. They apply in both concurrent and distributed asynchronous settings: All (seven) methods are defined in void "one-way" message style. Communication relies on a simple form of flow control (method Subscription#request) that can be used to avoid resource management problems that may otherwise occur in "push" based systems.

<b>Examples.</b> A Publisher usually defines its own Subscription implementation; constructing one in method subscribe and issuing it to the calling Subscriber. It publishes items to the subscriber asynchronously, normally using an Executor. For example, here is a very simple publisher that only issues (when requested) a single TRUE item to a single subscriber. Because the subscriber receives only a single item, this class does not use buffering and ordering control required in most implementations.

{@code
            class OneShotPublisher implements Publisher<Boolean> {
              private final ExecutorService executor = ForkJoinPool.commonPool(); // daemon-based
              private boolean subscribed; // true after first subscribe
              public synchronized void subscribe(Subscriber<? super Boolean> subscriber) {
                if (subscribed)
                  subscriber.onError(new IllegalStateException()); // only one allowed
                else {
                  subscribed = true;
                  subscriber.onSubscribe(new OneShotSubscription(subscriber, executor));
                }
              }
              static class OneShotSubscription implements Subscription {
                private final Subscriber<? super Boolean> subscriber;
                private final ExecutorService executor;
                private Future<?> future; // to allow cancellation
                private boolean completed;
                OneShotSubscription(Subscriber<? super Boolean> subscriber,
                                    ExecutorService executor) {
                  this.subscriber = subscriber;
                  this.executor = executor;
                }
                public synchronized void request(long n) {
                  if (!completed) {
                    completed = true;
                    if (n <= 0) {
                      IllegalArgumentException ex = new IllegalArgumentException();
                      executor.execute(() -> subscriber.onError(ex));
                    } else {
                      future = executor.submit(() -> {
                        subscriber.onNext(Boolean.TRUE);
                        subscriber.onComplete();
                      });
                    }
                  }
                }
                public synchronized void cancel() {
                  completed = true;
                  if (future != null) future.cancel(false);
                }
              }
            }}

A Subscriber arranges that items be requested and processed. Items (invocations of Subscriber#onNext) are not issued unless requested, but multiple items may be requested. Many Subscriber implementations can arrange this in the style of the following example, where a buffer size of 1 single-steps, and larger sizes usually allow for more efficient overlapped processing with less communication; for example with a value of 64, this keeps total outstanding requests between 32 and 64. Because Subscriber method invocations for a given Subscription are strictly ordered, there is no need for these methods to use locks or volatiles unless a Subscriber maintains multiple Subscriptions (in which case it is better to instead define multiple Subscribers, each with its own Subscription).

{@code
            class SampleSubscriber<T> implements Subscriber<T> {
              final Consumer<? super T> consumer;
              Subscription subscription;
              final long bufferSize;
              long count;
              SampleSubscriber(long bufferSize, Consumer<? super T> consumer) {
                this.bufferSize = bufferSize;
                this.consumer = consumer;
              }
              public void onSubscribe(Subscription subscription) {
                long initialRequestSize = bufferSize;
                count = bufferSize - bufferSize / 2; // re-request when half consumed
                (this.subscription = subscription).request(initialRequestSize);
              }
              public void onNext(T item) {
                if (--count <= 0)
                  subscription.request(count = bufferSize - bufferSize / 2);
                consumer.accept(item);
              }
              public void onError(Throwable ex) { ex.printStackTrace(); }
              public void onComplete() {}
            }}

The default value of #defaultBufferSize may provide a useful starting point for choosing request sizes and capacities in Flow components based on expected rates, resources, and usages. Or, when flow control is never needed, a subscriber may initially request an effectively unbounded number of items, as in:

{@code
            class UnboundedSubscriber<T> implements Subscriber<T> {
              public void onSubscribe(Subscription subscription) {
                subscription.request(Long.MAX_VALUE); // effectively unbounded
              }
              public void onNext(T item) { use(item); }
              public void onError(Throwable ex) { ex.printStackTrace(); }
              public void onComplete() {}
              void use(T item) { ... }
            }}

Added in 9.

Java documentation for java.util.concurrent.Flow.

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.

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.

(Inherited from Object)
ThresholdType

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

(Inherited from Object)

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
DefaultBufferSize()

Returns a default value for Publisher or Subscriber buffering, that may be used in the absence of other constraints.

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)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
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)

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, 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)

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