本文整理汇总了C#中Pool.Push方法的典型用法代码示例。如果您正苦于以下问题:C# Pool.Push方法的具体用法?C# Pool.Push怎么用?C# Pool.Push使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Pool
的用法示例。
在下文中一共展示了Pool.Push方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestPushPop
public void TestPushPop()
{
var p0 = new Pool<Foo>();
var p1 = new Pool<Foo>(1);
var f = new Foo();
var g = new Foo();
p0.Push(f);
Assert.AreEqual(1, p0.Count);
p0.Push(g);
Assert.AreEqual(2, p0.Count);
p1.Push(f);
Assert.AreEqual(1, p1.Count);
// capacity overflow
p1.Push(g);
Assert.AreEqual(1, p1.Count);
Foo g1 = p0.Pop();
Assert.AreSame(g, g1);
Foo f1 = p0.Pop();
Assert.AreSame(f, f1);
Foo f2 = p1.Pop();
Assert.AreSame(f, f2);
// pop underflow
Foo g2 = p1.Pop();
Assert.AreSame(null, g2);
}
示例2: TestNullPush
public void TestNullPush()
{
var p = new Pool<Foo>();
Assert.Throws<ArgumentNullException>(() => { p.Push(null); });
}
示例3: TestNullPush
public void TestNullPush()
{
var p = new Pool<Foo>();
p.Push(null);
}
示例4: Main
static void Main(string[] args)
{
testStack(new Stack<int>(), false);
testStack(new StackWithArray<int>(), false);
testStack(new LockBasedStack<int>());
testStack(new LockFreeStack<int>());
testStack(new BalancerTreeStack<int>());
{
var s = new Pool<int>(1);
for (int i = 0; i < 1000; ++i)
s.Push(i, 0);
for (int i = 999; i >= 0; --i)
{
int v;
Debug.Assert(s.Pop(out v, 0));
Debug.Assert(v == i);
}
s = new Pool<int>(2);
Parallel.Invoke(
() =>
{
for (int i = 0; i < 1000000; ++i)
s.Push(i, 0);
},
() =>
{
int v;
for (int i = 0; i < 1000000; ++i)
s.Pop(out v, 0);
},
() =>
{
for (int i = 0; i < 1000000; ++i)
s.Push(i, 1);
},
() =>
{
int v;
for (int i = 0; i < 1000000; ++i)
s.Pop(out v, 1);
}
);
}
{
var s = new LockFreeBag<int>();
for (int i = 0; i < 1000; ++i)
s.Add(i);
for (int i = 999; i >= 0; --i)
{
int v;
Debug.Assert(s.TryTake(out v));
Debug.Assert(v == i);
}
s = new LockFreeBag<int>();
Parallel.Invoke(
() =>
{
for (int i = 0; i < 1000000; ++i)
s.Add(i);
},
() =>
{
int v;
for (int i = 0; i < 1000000; ++i)
s.TryTake(out v);
},
() =>
{
for (int i = 0; i < 1000000; ++i)
s.Add(i);
},
() =>
{
int v;
for (int i = 0; i < 1000000; ++i)
s.TryTake(out v);
}
);
}
Console.WriteLine("done");
}