异步和多线程测试的问题

sgchen 2014-08-14 03:34:01
这两天在做项目中用到异步方法,结果出现以下问题,以下2个程序,
用异步时显示很慢,任务管理器的线程数也比较上,
用多线程则正常,显示刷新很快,任务管理器的线程数一下上到几百。
百思不得其解,上来问问各位高手。

1.用异步

using System;
using System.Threading;

namespace TestAsyncCallback
{
public delegate string GetReplyDelegate(int id, string msg);

static class Program1
{
internal static GetReplyDelegate d = null;

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
d = new GetReplyDelegate(GetReplyDelay);
Start();
Console.ReadLine();
}
static string GetReplyDelay(int id, string msg)
{
Console.WriteLine("add:" + id.ToString());
Thread.Sleep(10000);
return msg + id.ToString();
}
static void Start()
{
for (int i = 1; i <= 1000; i++)
{
//ParameterizedThreadStart t = new ParameterizedThreadStart(GetReplyProc);
//Thread thread = new Thread(t);
//thread.IsBackground = true;
//thread.Start(new object[] { i, "test" });

d.BeginInvoke(i, "test", Callback, null);
}
}
static void Callback(IAsyncResult ar)
{
string s = d.EndInvoke(ar);
Console.WriteLine(s);
}
private static void GetReplyProc(object obj)
{
object[] os = (object[])obj;

string s = GetReplyDelay((int)os[0], os[1].ToString());
Console.WriteLine(s);

}

}
}


效果图

因为延迟10秒才计算出结果,正常add和结果的显示不应该这么快在一起,就是因为运行慢才会这样

2.用多线程

using System;
using System.Threading;

namespace TestAsyncCallback
{
public delegate string GetReplyDelegate(int id, string msg);

static class Program1
{
internal static GetReplyDelegate d = null;

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
d = new GetReplyDelegate(GetReplyDelay);
Start();
Console.ReadLine();
}
static string GetReplyDelay(int id, string msg)
{
Console.WriteLine("add:" + id.ToString());
Thread.Sleep(10000);
return msg + id.ToString();
}
static void Start()
{
for (int i = 1; i <= 1000; i++)
{
ParameterizedThreadStart t = new ParameterizedThreadStart(GetReplyProc);
Thread thread = new Thread(t);
thread.IsBackground = true;
thread.Start(new object[] { i, "test" });

//d.BeginInvoke(i, "test", Callback, null);
}
}
static void Callback(IAsyncResult ar)
{
string s = d.EndInvoke(ar);
Console.WriteLine(s);
}
private static void GetReplyProc(object obj)
{
object[] os = (object[])obj;

string s = GetReplyDelay((int)os[0], os[1].ToString());
Console.WriteLine(s);

}

}
}


效果图

...全文
150 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgchen 2014-08-14
  • 打赏
  • 举报
回复
引用 4 楼 xiaozhi_5638 的回复:
线程池中的线程 不是你想什么时候创建 它就能创建的 一般情况下 每秒不超过2个线程 也就是说 就算你一下子往线程池中塞1000个线程 它每秒最多开始2个 解决这个问题 请使用 System.Threading.ThreadPool.SetMinThreads(500, 500); 在异步调用开始前 设置线程池的最小线程数 那么只要低于这个数 线程马上会创建好
正解呀,谢谢!
请叫我卷福 2014-08-14
  • 打赏
  • 举报
回复
使用ThreadPool或者异步编程时 如果遇见非常慢的情况 有两个原因 1)线程总数大于线程池的上限 大于的部分都得等待 2)线程池创建线程的速度有上限,如果最小线程数为100,那么大于100的其余线程 创建速度非常慢
请叫我卷福 2014-08-14
  • 打赏
  • 举报
回复
线程池中的线程 不是你想什么时候创建 它就能创建的 一般情况下 每秒不超过2个线程 也就是说 就算你一下子往线程池中塞1000个线程 它每秒最多开始2个 解决这个问题 请使用 System.Threading.ThreadPool.SetMinThreads(500, 500); 在异步调用开始前 设置线程池的最小线程数 那么只要低于这个数 线程马上会创建好
sgchen 2014-08-14
  • 打赏
  • 举报
回复
引用 2 楼 xiaozhi_5638 的回复:
线程池线程数 有上限 线程池资源耗尽了 适得其反
刚开始就慢,看图片就明白了
请叫我卷福 2014-08-14
  • 打赏
  • 举报
回复
线程池线程数 有上限 线程池资源耗尽了 适得其反
sgchen 2014-08-14
  • 打赏
  • 举报
回复
第一种情况用的是线程池,第二种不是。 但为什么线程池的会这么慢?

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧