测试条件:

开启2个并行执行任务,往同一个list对象写入值

测试代码:

static int maxNum = 1000000;

        static List<int> list = new List<int>();

        static void Main(string[] args)

        {

            //迭代次数

            int iterationNum = 3;

            CodeTimer.Initialize();

            CodeTimer.Time("List是否是线程安全的呢?", iterationNum, new Action(ListIsThreadSafe));

            //Console.Write(sbIsThreadSafe.ToString());

            Console.Read();

        }

 

        private static void ListIsThreadSafe()

        {

            Parallel.For(1, maxNum / 2, (i) =>

            {

                list.Add(i);

            });

            Parallel.For(maxNum / 2 + 1, maxNum, (i) =>

            {

                list.Add(i);

            });

        }
View Code

相关文章:

  • 2022-01-07
  • 2022-01-07
  • 2022-01-07
  • 2021-04-09
  • 2021-12-23
  • 2021-09-14
  • 2021-09-27
猜你喜欢
  • 2021-09-24
  • 2022-12-23
  • 2021-12-28
  • 2021-10-01
  • 2022-12-23
  • 2021-05-07
  • 2022-03-10
相关资源
相似解决方案