VisualStudioFeeds

All Visual Studio blogs in one place

AddThis Social Bookmark Button
I’m leveraging a ConcurrentPriorityQueue – from http://code.msdn.microsoft.com/ParExtSamples . This class basically is a thread safe IProducerConsumerCollection wrapper for a binary heap that prioritizes smaller values. You use it as you would a dictionary, where the priority is the key, except you can have duplicate keys (ie values with the same priority). I needed to demonstrate to a customer that it worked. I set up my queue and my priority enum values:
var q = new ConcurrentPriorityQueue(); var priorityValues = Enum.GetValues(typeof(JobPriority));
I then randomly enqueued different priorities from different threads:
/>
XXXvar random = new Random(); Parallel.For(0, 1000, i => {     var randomPriority = (JobPriority) priorityValues.GetValue(random.Next(priorityValues.Length));      Debug.WriteLine("enqueueing:" + randomPriority);      q.Enqueue((int) randomPriority, randomPriority.ToString()); }); and finally I asserted that dequeueing occurs in order: while (q.TryDequeue(out printJobKVP)) {                      var jobPriority = (JobPriority)Enum.Parse(typeof(JobPriority), printJobKVP.Value);     Assert.IsTrue(jobPriority >= previousJobPriority);     previousJobPriority = jobPriority; }
  ...(Read whole news on source site)

Search

Subscribe

Enter your email address:

Delivered by FeedBurner

Increase your website traffic with Attracta.com

Contact

Email:
Subject:
Message:
Anti-spam: How many eyes has a typical person?

Follow us on FB

Home : Blog List : Josh Reuben : Unit Testing a ConcurrentPriorityQueue