I was using BlockingCollection to queue data received over network and process it. I was consuming from the blockingcollection at every 100 ms in a timer. But I found that consumer was slow and length of blockingcollection was ever increasing.
Since I was not quite sure, what caused this behavior, I wrote a simple console app as shown below. But still the BlockingCollection count is ever increasing. Note that this time I used a dedicated thread for consuming.
class Program
{
static Thread sm_producerThread;
static Thread sm_consumerThread;
static BlockingCollection<int> sm_blockingCollection = new BlockingCollection<int>();
static int sm_index;
static void Main(string[] args)
{
sm_producerThread = new Thread(Produce) { IsBackground = true };
sm_producerThread.Start();
sm_consumerThread = new Thread(Consume) { IsBackground = true };
sm_consumerThread.Start();
Console.ReadLine();
}
private static void Produce()
{
while (true)
{
sm_index++;
sm_blockingCollection.TryAdd(sm_index);
Console.WriteLine($"Added number {sm_index}. Collection count is {sm_blockingCollection.Count}");
}
}
private static void Consume()
{
while (true)
{
var num = sm_blockingCollection.Take();
Console.WriteLine($"Fetched number {num}. Collection count is {sm_blockingCollection.Count}");
}
}
}
Fragment of a sample output is shown below
Fetched number 74317. Collection count is 1426
Fetched number 74318. Collection count is 1425
Fetched number 74319. Collection count is 1424
Fetched number 74320. Collection count is 1423
Fetched number 74321. Collection count is 1422
Fetched number 74322. Collection count is 1421
Fetched number 74323. Collection count is 1420
Added number 75743. Collection count is 1453
Added number 75744. Collection count is 1420
Added number 75745. Collection count is 1421
Added number 75746. Collection count is 1422
Added number 75747. Collection count is 1423
Added number 75748. Collection count is 1424
Added number 75749. Collection count is 1425
Added number 75750. Collection count is 1426
Added number 75751. Collection count is 1427
Added number 75752. Collection count is 1428
Added number 75753. Collection count is 1429
Added number 75754. Collection count is 1430
Added number 75755. Collection count is 1431
Added number 75756. Collection count is 1432
Added number 75757. Collection count is 1433
Added number 75758. Collection count is 1434
Added number 75759. Collection count is 1435
Added number 75760. Collection count is 1436
Added number 75761. Collection count is 1437
Added number 75762. Collection count is 1438
Added number 75763. Collection count is 1439
Fetched number 74324. Collection count is 1419
Added number 75764. Collection count is 1440
Added number 75765. Collection count is 1440
Added number 75766. Collection count is 1441
Added number 75767. Collection count is 1442
Added number 75768. Collection count is 1443
Added number 75769. Collection count is 1444
Added number 75770. Collection count is 1445
Added number 75771. Collection count is 1446
Added number 75772. Collection count is 1447
Added number 75773. Collection count is 1448
Added number 75774. Collection count is 1449
Added number 75775. Collection count is 1450
Added number 75776. Collection count is 1451
Added number 75777. Collection count is 1452
Added number 75778. Collection count is 1453
Added number 75779. Collection count is 1454
Added number 75780. Collection count is 1455
Added number 75781. Collection count is 1456
Added number 75782. Collection count is 1457
Added number 75783. Collection count is 1458
Added number 75784. Collection count is 1459
Added number 75785. Collection count is 1460
Added number 75786. Collection count is 1461
Added number 75787. Collection count is 1462
Added number 75788. Collection count is 1463
Added number 75789. Collection count is 1464
Added number 75790. Collection count is 1465
Added number 75791. Collection count is 1466
Added number 75792. Collection count is 1467
Added number 75793. Collection count is 1468
Added number 75794. Collection count is 1469
Added number 75795. Collection count is 1470
Added number 75796. Collection count is 1471
Added number 75797. Collection count is 1472
Added number 75798. Collection count is 1473
Added number 75799. Collection count is 1474
Added number 75800. Collection count is 1475
Added number 75801. Collection count is 1476
Added number 75802. Collection count is 1477
Added number 75803. Collection count is 1478
Added number 75804. Collection count is 1479
Added number 75805. Collection count is 1480
Added number 75806. Collection count is 1481
Added number 75807. Collection count is 1482
Added number 75808. Collection count is 1483
Added number 75809. Collection count is 1484
Added number 75810. Collection count is 1485
Added number 75811. Collection count is 1486
Added number 75812. Collection count is 1487
Added number 75813. Collection count is 1488
Added number 75814. Collection count is 1489
Added number 75815. Collection count is 1490
Added number 75816. Collection count is 1491
Added number 75817. Collection count is 1492
Added number 75818. Collection count is 1493
As you can see that count actually reached 1490 by the time 75818 entries was processed. I was hoping that the count of items in the collection will be a small number (say around 100) at any point of time since both the threads might have almost same priority and same work load. What am I missing?