本文整理汇总了C#中ObjectPool.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectPool.Initialize方法的具体用法?C# ObjectPool.Initialize怎么用?C# ObjectPool.Initialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectPool
的用法示例。
在下文中一共展示了ObjectPool.Initialize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestAllocateAndFree
public void TestAllocateAndFree()
{
var pool = new ObjectPool<TestElement>();
pool.Initialize();
Assert.IsTrue(pool.UsedListSize == 0 && pool.FreeListSize == 0);
// allocate some instances
var e1 = pool.Allocate();
var e2 = pool.Allocate();
Assert.IsTrue(pool.UsedListSize == 2 && pool.FreeListSize == 0);
Assert.IsTrue(e1 != e2);
Assert.IsTrue(e1.value == 0);
// free both and then allocate two more again - we should get both back
pool.Free(e1);
pool.Free(e2);
Assert.IsTrue(pool.UsedListSize == 0 && pool.FreeListSize == 2);
var e3 = pool.Allocate();
var e4 = pool.Allocate();
Assert.IsTrue(pool.UsedListSize == 2 && pool.FreeListSize == 0);
Assert.IsTrue(e3 == e2);
Assert.IsTrue(e4 == e1);
}
示例2: TestFactoryAndReset
public void TestFactoryAndReset()
{
var pool = new ObjectPool<TestElement>();
pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });
// allocate a new instance using this custom factory, make sure the factory code got called
var e1 = pool.Allocate();
Assert.IsTrue(e1.value == 42);
// free it, and make sure it got reset
pool.Free(e1);
Assert.IsTrue(e1.value == 0);
// now allocate again, this time since it's coming from the object pool,
// the factory code won't be called
var e2 = pool.Allocate();
Assert.IsTrue(e1 == e2);
Assert.IsTrue(e2.value == 0); // because this is coming from the pool, not factory
}
示例3: TestPoolRelease
public void TestPoolRelease()
{
var pool = new ObjectPool<TestElement>();
pool.Initialize(() => { var e = new TestElement(); e.value = 42; return e; });
// allocate a new instance using this custom factory, make sure the factory code got called
var e1 = pool.Allocate();
Assert.IsTrue(e1.value == 42);
Assert.IsTrue(pool.UsedListSize == 1);
Assert.IsTrue(pool.FreeListSize == 0);
// allocate and free an instance. this will grow the pool to size one
pool.Free(pool.Allocate());
Assert.IsTrue(pool.UsedListSize == 1);
Assert.IsTrue(pool.FreeListSize == 1);
// now release the entire object pool. this will clear out the free list, but will not
// affect the object already in circulation, since we're not tracking them.
pool.Release();
Assert.IsTrue(e1.value == 42);
Assert.IsTrue(pool.UsedListSize == 1);
Assert.IsTrue(pool.FreeListSize == -1);
}