ArrayList lock

lock(array)
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections;

namespace Test
{
    class Program
    {
        private static ArrayList array = new ArrayList();
        public static void AddItem()
        {
            lock (array)
            {
                Console.WriteLine("AddItem()开始添加...   " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                for (int i = 0; i < 5; i++)
                {
                    array.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    Thread.Sleep(TimeSpan.FromSeconds(2));
                }

                Console.WriteLine("AddItem()结束添加...   " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }

            Console.WriteLine("AddItem()退出方法体... " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
        }


        static void Main()
        {
            Thread t1 = new Thread(new ThreadStart(AddItem));
            Thread t2 = new Thread(new ThreadStart(AddItem));

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();

Console.WriteLine(array.Count.ToString());
foreach (string item in array) { Console.WriteLine(item); } Console.WriteLine("执行完成"); Console.ReadLine(); } } }

 

/*
AddItem()开始添加...   2016-08-17 23:50:14
AddItem()结束添加...   2016-08-17 23:50:24
AddItem()退出方法体... 2016-08-17 23:50:24

AddItem()开始添加...   2016-08-17 23:50:24
AddItem()结束添加...   2016-08-17 23:50:34
AddItem()退出方法体... 2016-08-17 23:50:34

10
2016-08-17 23:50:14
2016-08-17 23:50:16
2016-08-17 23:50:18
2016-08-17 23:50:20
2016-08-17 23:50:22
2016-08-17 23:50:24
2016-08-17 23:50:26
2016-08-17 23:50:28
2016-08-17 23:50:30
2016-08-17 23:50:32
执行完成
 
*/

 

 lock(array.SyncRoot)

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections;

namespace Test
{
    class Program
    {
        private static ArrayList array = new ArrayList();
        public static void AddItem()
        {
            lock (array.SyncRoot)
            {
                Console.WriteLine("AddItem()开始添加...   " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                for (int i = 0; i < 5; i++)
                {
                    array.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    Thread.Sleep(TimeSpan.FromSeconds(2));
                }

                Console.WriteLine("AddItem()结束添加...   " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }

            Console.WriteLine("AddItem()退出方法体... " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
        }


        static void Main()
        {
            Thread t1 = new Thread(new ThreadStart(AddItem));
            Thread t2 = new Thread(new ThreadStart(AddItem));

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();


            Console.WriteLine(array.Count.ToString());
            foreach (string item in array)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("执行完成");
            Console.ReadLine();
        }
    }
}

 

/*
AddItem()开始添加...   2016-08-17 23:53:37
AddItem()结束添加...   2016-08-17 23:53:47
AddItem()退出方法体... 2016-08-17 23:53:47

AddItem()开始添加...   2016-08-17 23:53:47
AddItem()结束添加...   2016-08-17 23:53:57
AddItem()退出方法体... 2016-08-17 23:53:57

10
2016-08-17 23:53:37
2016-08-17 23:53:39
2016-08-17 23:53:41
2016-08-17 23:53:43
2016-08-17 23:53:45
2016-08-17 23:53:47
2016-08-17 23:53:49
2016-08-17 23:53:51
2016-08-17 23:53:53
2016-08-17 23:53:55
执行完成
 
*/

 

posted @ 2016-08-17 23:46  茗::流  阅读(152)  评论(0)    收藏  举报
如有雷同,纯属参考。如有侵犯你的版权,请联系我。