C#静态方法在多用户并发访问时,运行情况到底是什么样的?是排队等待?还是并发的?
时间:2011-03-01 来源:轻量级快速开发平台
模拟并发的测试代码如下,希望对有需要的朋友提供的方便的参考
//--------------------------------------------------------------------// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd.
//--------------------------------------------------------------------
namespace DotNet.Example
{
using DotNet.BaseManager;
public class StaticTest
{
/// <summary>
/// 定义委托
/// </summary>
/// <param name="user">用户</param>
delegate void MakeStaticDelegate(string user);
/// <summary>
/// 这里是测试静态方法
/// </summary>
/// <param name="user">用户</param>
private static void MakeStaticTest(string user)
{
for (int i = 0; i < 10; i++)
{
// 输出当前的变量
System.Console.WriteLine(user + ":" + i.ToString());
System.Threading.Thread.Sleep(1000);
}
}
/// <summary>
/// 这里是模拟多用户同时点击并发
/// </summary>
public void DoTest()
{
// 模拟3个用户的并发操作
MakeStaticDelegate makeStaticDelegate1 = new MakeStaticDelegate(MakeStaticTest);
makeStaticDelegate1.BeginInvoke("user1", null, null);
MakeStaticDelegate makeStaticDelegate2 = new MakeStaticDelegate(MakeStaticTest);
makeStaticDelegate2.BeginInvoke("user2", null, null);
MakeStaticDelegate makeStaticDelegate3 = new MakeStaticDelegate(MakeStaticTest);
makeStaticDelegate3.BeginInvoke("user3", null, null);
System.Console.ReadLine();
}
}
}
相关阅读 更多 +