本文整理汇总了C#中ObjectPool.Get方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectPool.Get方法的具体用法?C# ObjectPool.Get怎么用?C# ObjectPool.Get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectPool
的用法示例。
在下文中一共展示了ObjectPool.Get方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: when_pooling
void when_pooling()
{
const string NAME = "Max";
ObjectPool<NameAgeComponent> objectPool = null;
before = () => {
objectPool = new ObjectPool<NameAgeComponent>(
() => new NameAgeComponent { name = NAME },
c => { c.name = null; c.age = -1; }
);
};
it["gets new instance from pool"] = () => {
var component = objectPool.Get();
component.name.should_be(NAME);
};
it["gets pooled instance"] = () => {
var component = new NameAgeComponent();
objectPool.Push(component);
objectPool.Get().should_be_same(component);
};
it["resets pushed instance"] = () => {
var component = new NameAgeComponent { name = NAME, age = 42 };
objectPool.Push(component);
component.name.should_be_null();
component.age.should_be(-1);
};
it["doesn't reset when reset method is null"] = () => {
objectPool = new ObjectPool<NameAgeComponent>(() => new NameAgeComponent { name = NAME });
var component = new NameAgeComponent { name = NAME };
objectPool.Push(component);
component.name.should_be(NAME);
objectPool.Get().should_be_same(component);
};
}
示例2: TFReaderLease
public TFReaderLease(ObjectPool<ITransactionFileReader> pool)
{
_pool = pool;
Reader = pool.Get();
}
示例3: TestPool
public static void TestPool()
{
var time = 2000000;
ObjectPool<byte[]> pool = new ObjectPool<byte[]>(() => new byte[1024], 10, 80);
pool.MaxCountExceed += pool_MaxCountExceed;
TimedMethod.RunMethod("Loop Fill With Pool", () => {
Parallel.For(0, time, i => {
using (var buffer = pool.Get()) {
using (var buffer1 = pool.Get()) {
using (var buffer2 = pool.Get()) {
using (var buffer3 = pool.Get()) {
using (var buffer4 = pool.Get()) {
using (var buffer5 = pool.Get()) {
using (var buffer6 = pool.Get()) {
using (var buffer7 = pool.Get()) {
using (var buffer8 = pool.Get()) {
using (var buffer9 = pool.Get()) {
using (var buffer10 = pool.Get()) {
using (var buffer11 = pool.Get()) {
using (var buffer12 = pool.Get()) {
using (var buffer13 = pool.Get()) {
buffer.Value.Fill<byte>(1);
buffer.Value.Fill<byte>(1);
buffer.Value.Fill<byte>(1);
buffer.Value.Fill<byte>(1);
buffer.Value.Fill<byte>(1);
buffer.Value.Fill<byte>(1);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
});
});
TimedMethod.RunMethod("Loop Fill Without Pool", () => {
Parallel.For(0, time, i => {
var buffer = new byte[1024];
buffer.Fill<byte>(1);
});
});
}