可以参考下面用法:
Array myArray = new int[] { 1, 2, 4 };
lock(myArray.SyncRoot) 
{
    foreach (Object item in myArray)
        Console.WriteLine(item);
}

解决方案 »

  1.   

    我把测试代码贴上,看看是不是我测试错了?
    public partial class Form1 : Form
        {
            public static object[] oba = new object[2];        
            
            public Form1()
            {
                InitializeComponent();
                oba[0] = new object();
                oba[1] = new object();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                Thread t1 = new Thread(new ThreadStart(accessoba1));
                Thread t2 = new Thread(new ThreadStart(accessoba2));
                t1.Start();
                t2.Start();
            }        private void accessoba1()
            {
                for (int i = 0; i < 1000000; i++)
                {                
                    DateTime dt1 = DateTime.Now;
                    oba[0].ToString();
                    TimeSpan ts = DateTime.Now - dt1;
                    if (ts.TotalMilliseconds > 0)
                    {
                        System.IO.File.AppendAllText("c:\\TestResult1.txt", ts.TotalMilliseconds.ToString() + Environment.NewLine);
                    }
                }
            }
            private void accessoba2()
            {
                for (int i = 0; i < 1000000; i++)
                {
                    DateTime dt1 = DateTime.Now;
                    oba[1].ToString();
                    TimeSpan ts = DateTime.Now - dt1;
                    if (ts.TotalMilliseconds > 0)
                    {
                        System.IO.File.AppendAllText("c:\\TestResult2.txt", ts.TotalMilliseconds.ToString() + Environment.NewLine);
                    }
                }
            }
      

  2.   

    测试结果:
    TestResult1.txt:
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    110.1584
    10.0144
    10.0144
    10.0144
    10.0144
    110.1584
    10.0144
    10.0144
    10.0144
    10.0144
    110.1584
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    130.1872
    10.0144
    20.0288
    20.0288
    10.0144
    10.0144
    10.0144
    10.0144
    20.0288
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    20.0288
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    10.0144
    20.0288
    10.0144
    20.0288
    50.072
    10.0144
    70.1008
    10.0144
    60.0864
    60.0864
    30.0432
    10.0144
    10.0144
      

  3.   

     你可以试试用1K个INT[] 和5个线程来做实验 让每个线程都循环輸出 INT[]的数组和线程的标识代号 你会发现 console輸出的是各个线程交叉着的  这表明 各个线程都能访问这个数组.... 假如console輸出是1个线程从头到尾一直輸出完 才到另一个线程从头到尾的輸出 那就是锁住了
      

  4.   

    class Program
        {
            public static int Total = 10000000;        public static object[] obj = new object[Total];         static void Main(string[] args)
            {
                Thread[] trd = new Thread[2];
                for (int i = 0; i < trd.Length; i++) {
                    trd[i] = new Thread(work);
                    trd[i].Start(i);
                }
            }        static void work(object i)
            {
                while (true) {
                    DateTime d1 = DateTime.MinValue;
                    DateTime d2 = DateTime.MinValue;
                    lock (obj.SyncRoot)//自己体会下有和没有lock的区别
                    {
                        d1 = System.DateTime.Now;                    for (int j = 0; j < obj.Length; j++)
                        {
                            if (j % 2 == (int)i)
                            {                            
                                {
                                    obj[j] = new object();
                                }
                            }
                        }
                        d2 = System.DateTime.Now;
                    }
                    
                    TimeSpan ts = d2 - d1;
                    {
                        Console.WriteLine(string.Format("No.{0} work from {1} to {2} used {3}",
                            i, d1.ToString("HH:mm:ss ffff"), d2.ToString("HH:mm:ss ffff"), ts.TotalMilliseconds));
                    }                Console.ReadLine();
                }
            }
           
        }