wjr2513 2015-08-11 09:32 采纳率: 0%
浏览 1924
已结题

Parallel.For并行录入10万条数据出错

用了一个CodeTimer测试性能,在循环方法体的时候,老大说让用Parallel.For代替for循环。

结果瞬间快了100倍的测试速度,但是又出现一个问题了,数据录不进去了。

经常循环10万次才录入一条,断点调试后发现就第一条录进去了,后面的都是数据库连接池错误。

我想问下用并行之后,这种情况是正常的吗,如果不正常,有什么方法解决呢

代码中的Time()的三个参数,第一个是方法名(无视),第二个是循环次数,第三个是方法体,关于Time()方法我也列出来,详情可以看http://blog.zhaojie.me/2009/03/codetimer.html

 Time("注册性能", 100, () =>
            {
                try
                {
                    User user = new User() { UserName = Guid.NewGuid().ToString(), Password = "123" };
                    ips.Register(user);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            });

图片说明

Timer方法体

 public static void Initialize()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Time("", 1, () => { });
        }

        public static void Time(string name, int iteration, Action action)
        {
            if (String.IsNullOrEmpty(name)) return;

            // 1.
            ConsoleColor currentForeColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(name);

            // 2.
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            int[] gcCounts = new int[GC.MaxGeneration + 1];
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                gcCounts[i] = GC.CollectionCount(i);
            }

            // 3.
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ulong cycleCount = GetCycleCount();
            //for (int i = 0; i < iteration; i++) action();
            Parallel.For(0, iteration, (i) => { action(); });
            ulong cpuCycles = GetCycleCount() - cycleCount;
            watch.Stop();

            // 4.
            Console.ForegroundColor = currentForeColor;
            Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
            Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));

            // 5.
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                int count = GC.CollectionCount(i) - gcCounts[i];
                Console.WriteLine("\tGen " + i + ": \t\t" + count);
            }

            Console.WriteLine();
        }

        private static ulong GetCycleCount()
        {
            ulong cycleCount = 0;
            QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
            return cycleCount;
        }

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();
  • 写回答

1条回答

  • lhl_lqc 2015-08-11 11:09
    关注

    可能你的连接池用完了,因为你用的并行,获取连接池的时候数据库的处理较慢,连接释放的慢,导致连接池里的连接不够用。

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划