我正在做一个产生几百个线程的项目。所有这些线程都处于“睡眠”状态(它们锁定在Monitor对象上)。我注意到,如果我增加“睡眠”线程的数量,程序会非常慢。 “有趣”的事情是,看着任务管理器看来,线程数越多,处理器就越自由。我已经将问题缩小到了对象创建。创建有很多线程对象的速度减慢
有人可以解释给我吗?
我制作了一个小样本来测试它。这是一个控制台程序。它为每个处理器创建一个线程,并通过一个简单的测试(一个“新的Object()”)来测量它的速度。不,“新的对象()”不会被打散(尝试如果你不信任我)。主线程显示每个线程的速度。按CTRL-C,程序会产生50个“睡眠”线程。减速开始只有50个线程。在250左右,在任务管理器中非常明显,CPU不是100%使用(在我的情况下,它是82%)。我已经尝试了三种方法来锁定“睡眠”线程:Thread.CurrentThread.Suspend()(坏的,坏的,我知道:-)),对已经锁定的对象和Thread.Sleep(超时。无穷)。一样的。如果我用新的Object()注释该行,并用Math.Sqrt替换它(或没有),则问题不存在。速度不随线程数量而改变。 其他人可以检查吗?有谁知道瓶颈在哪里?
啊......你应该在发布模式下测试它,而不需要从Visual Studio启动它。 我在双处理器(无HT)上使用XP sp3。我与.NET 3.5和4.0进行了测试(测试不同的框架运行时)
namespace TestSpeed
{
using System;
using System.Collections.Generic;
using System.Threading;
class Program
{
private const long ticksInSec = 10000000;
private const long ticksInMs = ticksInSec/1000;
private const int threadsTime = 50;
private const int stackSizeBytes = 256 * 1024;
private const int waitTimeMs = 1000;
private static List<int> collects = new List<int>();
private static int[] objsCreated;
static void Main(string[] args)
{
objsCreated = new int[Environment.ProcessorCount];
Monitor.Enter(objsCreated);
for (int i = 0; i < objsCreated.Length; i++)
{
new Thread(Worker).Start(i);
}
int[] oldCount = new int[objsCreated.Length];
DateTime last = DateTime.UtcNow;
Console.Clear();
int numThreads = 0;
Console.WriteLine("Press Ctrl-C to generate {0} sleeping threads, Ctrl-Break to end.", threadsTime);
Console.CancelKeyPress += (sender, e) =>
{
if (e.SpecialKey != ConsoleSpecialKey.ControlC)
{
return;
}
for (int i = 0; i < threadsTime; i++)
{
new Thread(() =>
{
/* The same for all the three "ways" to lock forever a thread */
//Thread.CurrentThread.Suspend();
//Thread.Sleep(Timeout.Infinite);
lock (objsCreated) { }
}, stackSizeBytes).Start();
Interlocked.Increment(ref numThreads);
}
e.Cancel = true;
};
while (true)
{
Thread.Sleep(waitTimeMs);
Console.SetCursorPosition(0, 1);
DateTime now = DateTime.UtcNow;
long ticks = (now - last).Ticks;
Console.WriteLine("Slept for {0}ms", ticks/ticksInMs);
Thread.MemoryBarrier();
for (int i = 0; i < objsCreated.Length; i++)
{
int count = objsCreated[i];
Console.WriteLine("{0} [{1} Threads]: {2}/sec ", i, numThreads, ((long)(count - oldCount[i])) * ticksInSec/ticks);
oldCount[i] = count;
}
Console.WriteLine();
CheckCollects();
last = now;
}
}
private static void Worker(object obj)
{
int ix = (int)obj;
while (true)
{
/* First and second are slowed by threads, third, fourth, fifth and "nothing" aren't*/
new Object();
//if (new Object().Equals(null)) return;
//Math.Sqrt(objsCreated[ix]);
//if (Math.Sqrt(objsCreated[ix]) < 0) return;
//Interlocked.Add(ref objsCreated[ix], 0);
Interlocked.Increment(ref objsCreated[ix]);
}
}
private static void CheckCollects()
{
int newMax = GC.MaxGeneration;
while (newMax > collects.Count)
{
collects.Add(0);
}
for (int i = 0; i < collects.Count; i++)
{
int newCol = GC.CollectionCount(i);
if (newCol != collects[i])
{
collects[i] = newCol;
Console.WriteLine("Collect gen {0}: {1}", i, newCol);
}
}
}
}
}
如果您关注性能,则不应该有比(cpucount)更多的线程。 (cpucount + 2)和(cpucount * 2)之间是很好的经验法则(并且在你的系统上,都是4)。使用异步I/O操作队列来保持少量线程繁忙而不是睡眠。线程应该等待的唯一时间是在争用锁时。 – 2011-02-11 14:23:08
我正在做一个“慢动作”协同程序。线程之间的“切换时间”是不相关的,所以我可以使用线程(我有一个“开关”/秒,所以即使它失去了一些毫秒,使旧线程和新线程之间切换,我没有任何问题)。总是有许多线程与处理器运行相同但是如果睡眠线程放慢了一切,那么我有一个问题。不,我不能使用MS的异步库,因为它是“假的”。它“重写”你的程序。我必须使用一些预先存在的库。 – xanatos 2011-02-11 14:49:33
您是否考虑过使用TPL而不是显式创建线程?这样框架可以决定最合适的原生线程数量来完成这项工作。 – 2011-09-10 10:11:42