Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
 
public partial class Threadinf : System.Web.UI.Page
{
    List<int> garage = new List<int>();
    protected void Page_Load(object sender, EventArgs e)
    {
 
        Thread t1 = new Thread(new ThreadStart(mtd1));
        t1.Start();
        Thread t2 = new Thread(new ThreadStart(mtd2));
        t2.Start();
        while (t1.IsAlive == true || t2.IsAlive == true)
        {
            continue;
        }
        foreach (int val in garage)
        {
            Response.Write(val + "---");
            Response.Write("<br />");
        }
        Response.Write("Total Count :" + garage.Count);
    }
 

    public void mtd1()
    {
        for (int i = 1; i <= 2500; i++)
        {
            Thread.Sleep(1);
            garage.Add(i);
 
        }
    }
 
    public void mtd2()
    {
        for (int j = 25001; j <= 5000; j++)
        {
            Thread.Sleep(1);
            garage.Add(j);
        }
    }
}
Posted
Updated 15-Sep-14 18:52pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Sep-14 0:52am    
Where did you get this type, List? :-)
—SA
George Jonsson 16-Sep-14 0:55am    
Try not to put your whole question in the title.
In this case your title could be "Multithreading problem adding items to a List"
You also need to tell us what your expected and actual outcome is.
It is not like people here will take your code, try to figure out what you want to do, correct the code and then put it back.
Member 11057515 16-Sep-14 0:58am    
As per the code i should get the list array "Garage" count=5000 when the whole process is completed but sometiimes i am getting different output like 9478,8687 etc.
George Jonsson 16-Sep-14 1:06am    
Then you should update the question with this information.
Use the 'Improve question' widget.
Also correct this typo for (int j = 25001; j <= 5000; j++)
Member 11057515 16-Sep-14 1:16am    
Sorry while copy paste it happened. Its (int j=2501; j<=5000;j++)

Please read and understand this: http://en.wikipedia.org/wiki/Race_condition[^].

Also, you should understand that your list is a shared object. Generally, if add elements to it using lock statement.
If you use System.Generic.List<>, you should declare list as System.Generic.List<int>, not just as List (what is that?).

Any instance members are not guaranteed to be thread safe.
Therefore, use the lock:
http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

I cannot tell you how to correct your code because I don't know your goal. If your goal was to illustrate race condition, different results in different runs of the code would be exactly what you needed. Only write it correctly and add the lock. Having the different results is just the nature of your algorithm.

—SA
 
Share this answer
 
v2
Thanks, Every One.....Problem is solved.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900