Does BoundedCapacity include elements currently being processed in the TPL data stream?

BoundedCapacity restriction include only items in the input queue that are waiting to be processed, or does it also count the items currently being processed?

Take for example this ActionBlock :

 var block = new ActionBlock<int>( i => Console.WriteLine(i), new ExecutionDataflowBlockOptions { BoundedCapacity = 1000, MaxDegreeOfParallelism = 10, }); 

If 5 items are currently being processed in parallel. Does this mean that the input queue can contain 1000 items more on top , or only 995?

+6
source share
1 answer

Obviously, BoundedCapacity does include elements that are processed on top of elements waiting for an input queue. This can be easily demonstrated with an ActionBlock that has the same ExecutionDataflowBlockOptions with an action that never ends:

 var block = new ActionBlock<int>( _ => Task.Delay(-1), new ExecutionDataflowBlockOptions { BoundedCapacity = 1000, MaxDegreeOfParallelism = 10, }); for (int i = 0; i < 1001; i++) { Console.WriteLine("#{0} - InputCount={1}", i, block.InputCount); await block.SendAsync(i); } 

The result will be as follows, and then the application will block indefinitely:

 ... ... #990 - InputCount=980 #991 - InputCount=981 #992 - InputCount=982 #993 - InputCount=983 #994 - InputCount=984 #995 - InputCount=985 #996 - InputCount=986 #997 - InputCount=987 #998 - InputCount=988 #999 - InputCount=989 #1000 - InputCount=990 

That, since 1000 elements were added, 10 of them ( MaxDegreeOfParallelism ) are processed at the same time, the remaining 990 are waiting in the input queue, and element 1001 st will never be able to enter.

+7
source

Source: https://habr.com/ru/post/977294/


All Articles