SIGN UP MEMBER LOGIN:    
ARTICLE

Consumer/Producer Multithreaded Program

Posted by Indika M W Articles | Multithreading in C# November 04, 2002
This is simple multi-threading program that adds and removes elements in an ArrayList. Producer button will creates a producer thread that adds elements in to the ArrayList. The maximum number of elements can be added to this ArrayList are 100 elements and the producer thread has to wait until the consumer thread/threads (creates by pressing consumer button) removes elements from the ArrayList. Then Producer thread starts to add more elements to the ArrayList.
Reader Level:
Download Files:
 

I have used VS.NET to implement this program.

Double click on Produce_Consumer.Zip and extract all the files and folders in to a folder in C drive.  

Run the project using VS.NET:

Double click on MultiThread.sln (Prject file). Then run the project (press F5). The Form1 will appear as shown below. 

Run using command-line prompt.

Copy all the .cs files (From1.cs, Consumer.cs, Form1.cs and Producer.cs) in to a folder in C drive and comple it using 

C:\FolderName> csc *.cs

This will create a Form1.exe file inside the folder. Then double click it run it. 

This is simple multi-threading program that adds and removes elements in an ArrayList. Producer button will creates a producer thread that adds elements in to the ArrayList. The maximum number of elements can be added to this ArrayList are 100 elements and the producer thread has to wait until the consumer thread/threads (creates by pressing consumer button) removes elements from the ArrayList. Then Producer thread starts to add more elements to the ArrayList. 

public const int producerLimit = 200;

The maximum number of elements can be produce by Producer thread can be change using the constant variable producerLimit. 

Synchronized

public static ArrayList ItemContainer = ArrayList.Synchronized(new ArrayList());

Using Synchronized method we can make a Synchronized ArrayList (Thread-Safe). 

Interlocked provides atomic operations for variables that are shared by multiple threads. It contains methods such as Increment, Decrement, CompareExchange and Exchange. Interlocked.Increment(ref totalProduce); 

Volatile

In multithreading environment the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread. 

Pressing Consumer button multiple times can create multiple consumer threads.

Press Exit button to close the window. 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor