Skip to main content
 首页 » 编程设计

c#之多线程共享计数器和List集合变量

2024年01月26日31duanxz

场景,我有多个线程试图共享一个静态全局变量计数器。 之后我会将其添加到一个整数列表中,这个列表将在另一个线程中使用以检查一些细节。

我意识到即使在全局变量计数器上使用了 LOCK,我仍然得到重复的数字

请原谅我的解释,代码会说明更多。

问题是不同的线程可能会生成相同的计数器值(我不想要)。 我想要一个没有重复的流水号

class Test 
{ 
    private Object _thisLock = new Object(); 
    List<int> listing = new List<int>(); //shared LIST 
 
    public void Main() 
    { 
        //array of threads 
        for (int i = 0; i < 8; i++) 
        { 
            Thread th = new Thread(Work); 
            th.Name = "Thread" + i; 
            th.Start(); 
        } 
        Thread.Sleep(5000); 
        //Start checking for duplicates 
        Thread checker = new Thread(Checker); 
        checker.Start(); 
    } 
 
    private void Work() 
    { 
        Object _thisLock = new Object(); 
        while (true) 
        { 
            int a = Singleton.Instance.Counter++; 
            Console.WriteLine(Thread.CurrentThread.Name); 
            Console.WriteLine("WOrk : " + a); 
            lock (_thisLock) 
            { 
                listing.Add(a); 
            } 
            Thread.Sleep(1000); 
        } 
    } 
 
    private void Checker() 
    { 
        Object _thisLock = new Object(); 
        while (true) 
        { 
            lock (_thisLock) 
            { 
                List<int> selflist = new List<int>(); 
                selflist.AddRange(listing); ; 
 
                foreach (int p in selflist) 
                { 
 
                    if (selflist.FindAll(item => item.Equals(p)).Count() > 1) 
                    { 
                        Console.WriteLine("Check!!!!!!!!!!!!!!!!!! : " + p); 
                    } 
                } 
            } 
 
 
            Thread.Sleep(5000); 
        } 
    } 
} 
 
static void Main() 
{ 
    Test t = new Test(); 
    t.Main(); 
} 
 
public sealed class Singleton 
{ 
    private static volatile Singleton instance; 
    private static object syncRoot = new Object(); 
    private readonly Object _thisLock = new Object(); 
 
    private Singleton() { } 
 
    public static Singleton Instance 
    { 
        get 
        { 
            if (instance == null) 
            { 
                lock (syncRoot) 
                { 
                    if (instance == null) 
                        instance = new Singleton(); 
                } 
            } 
 
            return instance; 
        } 
    } 
    private volatile static int _counter; 
    public int Counter 
    { 
        get 
        { 
            lock (_thisLock) 
            { 
                return _counter; 
            } 
        } 
        set 
        { 
            lock (_thisLock) 
            { 
                _counter = value; 
            } 
        } 
    } 
} 

请您参考如下方法:

在您的Work 方法中,每个线程都有自己的锁对象_thisLock。 在你的工作方法中删除这条语句,让它使用类的私有(private)锁对象:

Object _thisLock = new Object();