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 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入