Threading In C Sharp | Coderz Product

Threading in CSharp

Last updated:

0 purchases

Threading in CSharp Image
Threading in CSharp Images
Threading in CSharp Images
Threading in CSharp Images

$5.00

Languages

C#

Categories

Add to Cart

Description:

This C# code implements a producer-consumer model using tasks and a blocking collection. The program simulates the concurrent production and consumption of items, demonstrating how multiple producers can generate items while multiple consumers process those items in a thread-safe manner.

Key Components

  1. Item Class:
    • Represents an item produced by the producers.
    • Each item has a unique identifier (Id) assigned during initialization.
  2. Producer Method:
    • The Producer method generates items and adds them to a BlockingCollection.
    • Each producer creates 5 items, each with a unique ID based on the producer's ID.
    • It simulates work by sleeping for a random duration after producing each item.
  3. Consumer Method:
    • The Consumer method processes items received from the BlockingCollection.
    • It continuously reads items from the collection until it is marked as complete.
    • It simulates work by sleeping for a random duration after consuming each item.
  4. Main Method:
    • BlockingCollection<Item> is created to hold the items produced by the producers.
    • An array of tasks is created for both producers and consumers.
    • Three producer tasks and two consumer tasks are started concurrently.
    • After all producers finish producing items, the collection is marked as complete to signal the consumers to stop.
    • The program waits for all consumer tasks to finish before printing a completion message.

Concurrency and Synchronization

  • The code utilizes C#'s Task class to create and manage concurrent execution for producers and consumers.
  • The BlockingCollection class ensures thread-safe operations when adding and removing items, preventing race conditions and allowing for efficient communication between producers and consumers.

Example Output

When executed, the program will produce output similar to the following:

javascript

 

Producer 0 produced item 0
Producer 1 produced item 10
Producer 2 produced item 20
Consumer 0 consumed item 0
Consumer 1 consumed item 10
...
All producers and consumers have finished.

This output demonstrates the interleaved execution of producers and consumers, showcasing the concurrent nature of the program.

Conclusion

This C# code effectively illustrates the producer-consumer problem using tasks and a synchronized blocking collection, providing a clear example of concurrent programming in C#. It highlights the importance of synchronization and safe communication when multiple tasks interact with shared resources, making it a valuable reference for understanding concurrency in C#.

Features:

  1. Item Class:
    • Represents an item produced by the producers.
    • Each item has a unique identifier (Id) assigned during initialization.
  2. Producer Method:
    • The Producer method generates items and adds them to a BlockingCollection.
    • Each producer creates 5 items, each with a unique ID based on the producer's ID.
    • It simulates work by sleeping for a random duration after producing each item.
  3. Consumer Method:
    • The Consumer method processes items received from the BlockingCollection.
    • It continuously reads items from the collection until it is marked as complete.
    • It simulates work by sleeping for a random duration after consuming each item.
  4. Main Method:
    • BlockingCollection<Item> is created to hold the items produced by the producers.
    • An array of tasks is created for both producers and consumers.
    • Three producer tasks and two consumer tasks are started concurrently.
    • After all producers finish producing items, the collection is marked as complete to signal the consumers to stop.
    • The program waits for all consumer tasks to finish before printing a completion message.

Requirements:

  • .NET Framework
  • Dotnet SDK

Instructions:

For best results use Visual Studio Community or Visual Studio Code with the appropriate extensions. 

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product: (if this is empty don't purchase this product)

Customer Reviews

There are no reviews.