public void GetOneRedPacketTestLoop() { for (int i = 0; i < 100; i++) { GetOneRedPacketTest(); RedPacketProvider.Reset(); } }
/// <summary> /// 多线程下运行 /// </summary> static void RunWithMultiThread() { Func <RedPacketConfig> func = new Func <RedPacketConfig>(GetRedPacketConfig); RedPacketConfig config = null; lock (configRWLock) { config = func.Invoke(); } //尝试提前初始化红包池 RedPacketProvider.TryInitPackagePool(func); for (int i = 0; i < config.PacketCount + 10; i++) { Thread thread = new Thread(() => { lock (configRWLock) { var money = RedPacketProvider.GetOneRedPacket(func); System.Console.WriteLine(money + ""); //模拟红包池意外清空的情况 if (i == 100 || i == 60 || i == 210) { RedPacketProvider.Reset(); } //模拟活动变更的情况 if (i == 190 || i == 105) { redPacketIdentity = Guid.NewGuid().ToString(); } //模拟数据库 更新当前已发的红包总金额与总数 currentAmount += money; currentPackageCount++; } }); thread.Start(); } System.Console.WriteLine("正在统计总金额..."); //暂停三秒 保证已经统计了所有金额 Thread.Sleep(3000); System.Console.WriteLine("Sum:{0},Count:{1}", currentAmount, currentPackageCount); System.Console.ReadKey(); }