Using the BlockingCollection for thread-safe producer-consumer scenarios in .NET Part 5

In the previous post we finished the basics of building producer-consumer scenarios with BlockingCollection in .NET. In particular we saw how to send a signal to the consumer that the producers have completed their tasks and no more items will be inserted into the work queue.

In this final post I just want to draw your attention to a couple of other fine-tuning methods in the BlockingCollection object.

Upper limit on blocking collection size

The BlockingCollection constructor allows you to set an upper limit of elements stored in the underlying producer-consumer collection:

new BlockingCollection<WorkTask>(workTaskCollection, 5);

If this upper limit is reached then the producer won’t be able to add new items to the collection until a consumer has retrieved one from it. This implies that BlockingCollection.Add method will block the current thread until there’s enough space in the producer-consumer collection. This brings us to the next section.

TryAdd

BlockingCollection has a TryAdd method which returns true if the insertion was successful. Its parameterless version will return immediately with true or false. You can also provide a wait time during which the TryAdd method can try to add an item to the collection. In the following example TryAdd will wait for at most 10 seconds before returning:

bool insertionSuccess = _workQueue.TryAdd(workTask, 10000);

TryTake

The counterpart of TryAdd is BlockingCollection.TryTake. It returns true if the item retrieval was successful and also returns the item as an “out” parameter. The most simple overload will return immediately without blocking the current thread:

WorkTask workTask;
bool success = _workQueue.TryTake(out workTask);

You can also specify a maximum wait time in milliseconds before returning the result:

WorkTask workTask;
bool success = _workQueue.TryTake(out workTask, 10000); 

View the list of posts on the Task Parallel Library here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: