Kai,我提供了一个使用线程完成你所需的程序。它的许可证条款如下:您必须按每个CPU核心每小时0.0001美元的价格向我支付费用。费用应在每个日历月底支付。请尽快联系我获取我的PayPal帐户详细信息。
using System;
using System.Collections.Generic;
using System.Linq;
namespace GuidCollisionDetector
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0:u} - Building a bigHeapOGuids.", DateTime.Now);
var bigHeapOGuids = new HashSet<Guid>();
try
{
do
{
bigHeapOGuids.Add(Guid.NewGuid());
} while (true);
}
catch (OutOfMemoryException)
{
}
Console.WriteLine("{0:u} - Built bigHeapOGuids, contains {1} of them.", DateTime.Now, bigHeapOGuids.LongCount());
for (long k = 0; k < Int64.MaxValue; k++)
{
for (long j = 0; j < Int64.MaxValue; j++)
{
Console.WriteLine("{0:u} - Looking for collisions with {1} thread(s)....", DateTime.Now, Environment.ProcessorCount);
System.Threading.Tasks.Parallel.For(0, Int32.MaxValue, (i) =>
{
if (bigHeapOGuids.Contains(Guid.NewGuid()))
throw new ApplicationException("Guids collided! Oh my gosh!");
}
);
Console.WriteLine("{0:u} - That was another {1} attempts without a collision.", DateTime.Now, ((long)Int32.MaxValue) * Environment.ProcessorCount);
}
}
Console.WriteLine("Umm... why hasn't the universe ended yet?");
}
}
}
PS: 我想尝试使用Parallel扩展库。那很简单。
而将OutOfMemoryException用作控制流程感觉不对。
编辑
嗯,看来这仍然吸引了很多投票。所以我已经解决了GC.KeepAlive()问题,并改为在C# 4中运行。
为了澄清我的支持条款:支持仅在2010年2月28日提供。请使用时光机仅在当天进行支持请求。
编辑2
与我自己管理内存的所有以前尝试一样,GC在管理内存方面做得更好。