C# .net 多線程中集合數據同步


from:http://www.cnblogs.com/GavinCome/archive/2008/04/09/1145250.html
C# .net 多線程中集合數據同步(轉)
集合類通常不是線程安全的,多個閱讀器可以安全的讀取集合.但是對集合的任何修改都將為訪問集合的所有線程生成不明確的結果.使用以下任何方法都可以令集合類是線程安全的
(1)    使用Synchronized 方法,則從該類派生包裝,並通過該包裝以獨占方式訪問集合
(2)    如果該類沒有Synchronized 方法,則從該類派生並使用SyncRoot屬性實現Synchronized 方法.
(3)    在訪問該集合時對SyncRoot屬性使用鎖定機制
這一段時間在公司做多線程的東西比較多,所以把一些心得寫了下來,對關注這一塊的朋友有個提示作用.
大家可以看看以下代碼:
    class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            //寫線程
            Thread t1 = new System.Threading.Thread(new ThreadStart(pg.t1fun));
            // 讀線程
            Thread t2 = new System.Threading.Thread(new ThreadStart(pg.t2fun));
            //刪線程
            Thread t3 = new System.Threading.Thread(new ThreadStart(pg.t3fun));
            t1.Start();
            t2.Start();
            t3.Start();
        }
        ArrayList arraylist = new ArrayList();
        public void t1fun()
        {
            while (true)
            {
                arraylist.Add("t1--寫入");
                System.Console.Out.WriteLine("寫入");
                System.Threading.Thread.Sleep(1000);
            }
        }
        public void t2fun()
        {
            while (true)
            {
                for (int i = arraylist.Count - 1; i >= 0; i--)
                {
                    System.Console.Out.WriteLine("t2讀取:"+(string)arraylist[i]);
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
        public void t3fun()
        {
            while (true)
            {
                for (int i = arraylist.Count - 1; i >= 0; i--)
                {
                    arraylist.RemoveAt(i);
                    System.Console.Out.WriteLine("t3刪除:t1"+i.ToString());
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
這個測試程序得簡單,大家一看就明白了你可以運行一下看看,程序一會就掛了,揭示異常:
未處理的異常: System.ArgumentOutOfRangeException: 索引超出范圍。必須為非負值並
小於集合大小。
這就是因為多線程中對共享的集合資源同步引起的
下面是改后的代碼:
    class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            //寫線程
            Thread t1 = new System.Threading.Thread(new ThreadStart(pg.t1fun));
            // 讀線程
            Thread t2 = new System.Threading.Thread(new ThreadStart(pg.t2fun));
            //刪線程
            Thread t3 = new System.Threading.Thread(new ThreadStart(pg.t3fun));
            t1.Start();
            t2.Start();
            t3.Start();
        }
        ArrayList arraylist = new ArrayList();
        public void t1fun()
        {
            while (true)
            { 
                lock (arraylist.SyncRoot)
                {
                    arraylist.Add("t1--寫入");
                }
                System.Console.Out.WriteLine("寫入");
                System.Threading.Thread.Sleep(1000);
            }
        }
        public void t2fun()
        {
            while (true)
            {
                lock (arraylist.SyncRoot)
                {
                    for (int i = arraylist.Count - 1; i >= 0; i--)
                    {
                        System.Console.Out.WriteLine("t2讀取:" + (string)arraylist[i]);
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
        public void t3fun()
        {
            while (true)
            {
                lock (arraylist.SyncRoot)
                {
                    for (int i = arraylist.Count - 1; i >= 0; i--)
                    {
                        arraylist.RemoveAt(i);
                        System.Console.Out.WriteLine("t3刪除:t1" + i.ToString());
                    }
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM