I'm playing around with the Flow API and, so far, I understand that the request()
method is used for back pressure. Most articles state that this is akin to controlling the speed of consumption.
However, almost every example code I see passes the value 1
into the request()
method, like, subscription.request(1)
. But I don't quite understand how does the request()
method control the speed of consumption.
I have tried to run a test by sending a bunch of items to the publisher and print the thread name and it seems like every single onNext()
is running on the same worker thread be it I was using request(1)
or request(50)
:
@Override
public void onNext(T item) {
System.out.println(Thread.getCurrent().getName());
Thread.sleep(5000);
subscription.request(50);
}
If the onNext()
were running in different threads, I can understand how that the n
value passed into request(n)
will affect the rate at which the items are being processed in parallel (running in n
threads). But it doesn't seem to be the case in my test as they are all running under the same thread name.
In this case, what's the difference between request(1)
and request(50)
when they are all still going to be run sequentially one after another on the same thread? Wouldn't the consumption rate still be the same then?