线程没有等待

本文关键字:等待 线程 | 更新日期: 2023-09-27 18:06:02

请看下面的代码。我没有在main中得到"Count"总是为"5"。有人能帮帮忙吗?

    class Program
{
    private static void Main(string[] args)
    {
        Thread t1 = new Thread(() => new Test("a"));
        Thread t2 = new Thread(() => new Test("b"));
        Thread t3 = new Thread(() => new Test("c"));
        Thread t4 = new Thread(() => new Test("d"));
        Thread t5 = new Thread(() => new Test("e"));
        t1.Start();
        t2.Start();
        t3.Start();
        t4.Start();
        t5.Start();
        t1.Join();
        t2.Join();
        t3.Join();
        t4.Join();
        t5.Join();
        Console.WriteLine(Test.Names.Count);
    }
}
public class Test
{
    public static ListClass<string> Names { get; set; }
    public Test(string name)
    {
        Console.WriteLine(name);
        //Thread.Sleep(10);
        if (Names == null)
            Names = new ListClass<string>();
        Names.Add(name);
    }
}
public class ListClass<T>
{
    private List<T> mylist = new List<T>();
    private object myLock = new object();
    public void Add(T item)
    {
        lock (myLock)
        {
            mylist.Add(item);
        }
    }
    public int Count
    {
        get { return mylist.Count; }
        private set
        { }
    }
}

线程没有等待

有一个竞争条件的机会,这意味着在一个线程检查Names是否为空的时候,另一个线程已经初始化了它,从而覆盖它,减少了计数,因为它是一个新的集合。

在null检查周围加一个锁,或者在测试类中使用静态构造函数来初始化集合,这不是最好的解决方案,但可以工作。

if (Names == null) 
{
    lock(_sync)
    {
        if (Names == null)
        {
            Names = new ListClass<string>();
        }
    }
}

使用双锁技术来避免竞争条件。或者类Lazy<T>,这在这种情况下非常有用。