1.多线程执行方法
/// <summary> /// 随机数保存队列 /// </summary> private Queue<string> _randomQueue = new Queue<string>(); StringBuilder builder = new StringBuilder(); /// <summary> /// 生成随机数,保存到队列中 /// </summary> /// <param name="number"></param> private void BuilderRandom() { BuilderRandom(72, clickSum); } /// <summary> /// 生成随机数,保存到队列中 /// </summary> /// <param name="number"></param> private void BuilderRandom(int number, int clickSum) { builder.Insert(0, $"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_第【{clickSum}】次点击_生成【{number}】组数据_开始生成 "); //builder.AppendLine($"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_第【{clickSum}】次点击_生成【{number}】组数据_开始生成"); Random random = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < number; i++) { _randomQueue.Enqueue($"第【{clickSum}】次点击_生成【{number}】组数据_第【{number}】组数据_{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_{random.Next()}"); } builder.Insert(0, $"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_第【{clickSum}】次点击_生成【{number}】组数据_生成结束 "); }
2.线程调用
/// <summary> /// thread生成随机数-无参 /// </summary> /// <param name="number"></param> /// <param name="clickSum"></param> private void ThreadBuilderRandom() { Thread thread = new Thread(BuilderRandom); thread.Start(); } /// <summary> /// thread生成随机数-有参 /// </summary> /// <param name="number"></param> /// <param name="clickSum"></param> private void ThreadBuilderRandom(int number, int clickSum) { Thread thread = new Thread(() => { BuilderRandom(number, clickSum); }); thread.Start(); }