本文整理汇总了C#中ConcurrentStack.TryPeek方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.TryPeek方法的具体用法?C# ConcurrentStack.TryPeek怎么用?C# ConcurrentStack.TryPeek使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentStack
的用法示例。
在下文中一共展示了ConcurrentStack.TryPeek方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test0_Empty
public static void Test0_Empty()
{
ConcurrentStack<int> s = new ConcurrentStack<int>();
int item;
Assert.False(s.TryPop(out item), "Test0_Empty: TryPop returned true when the stack is empty");
Assert.False(s.TryPeek(out item), "Test0_Empty: TryPeek returned true when the stack is empty");
Assert.True(s.TryPopRange(new int[1]) == 0, "Test0_Empty: TryPopRange returned non zero when the stack is empty");
int count = 15;
for (int i = 0; i < count; i++)
s.Push(i);
Assert.Equal(count, s.Count);
Assert.False(s.IsEmpty);
}
示例2: ShouldRespectMaxDocCountInBatch
public void ShouldRespectMaxDocCountInBatch()
{
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
for (int i = 0; i < 100; i++)
{
session.Store(new Company());
}
session.SaveChanges();
}
var id = store.Subscriptions.Create(new SubscriptionCriteria());
var subscription = store.Subscriptions.Open(id, new SubscriptionConnectionOptions{ BatchOptions = new SubscriptionBatchOptions { MaxDocCount = 25 }});
var batchSizes = new ConcurrentStack<Reference<int>>();
subscription.BeforeBatch +=
() => batchSizes.Push(new Reference<int>());
subscription.Subscribe(x =>
{
Reference<int> reference;
batchSizes.TryPeek(out reference);
reference.Value++;
});
var result = SpinWait.SpinUntil(() => batchSizes.ToList().Sum(x => x.Value) >= 100, TimeSpan.FromSeconds(60));
Assert.True(result);
Assert.Equal(4, batchSizes.Count);
foreach (var reference in batchSizes)
{
Assert.Equal(25, reference.Value);
}
}
}
示例3: ShouldRespectMaxBatchSize
public void ShouldRespectMaxBatchSize()
{
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
for (int i = 0; i < 100; i++)
{
session.Store(new Company());
session.Store(new User());
}
session.SaveChanges();
}
var id = store.Subscriptions.Create(new SubscriptionCriteria());
var subscription = store.Subscriptions.Open(id, new SubscriptionConnectionOptions()
{
BatchOptions = new SubscriptionBatchOptions()
{
MaxSize = 16 * 1024
}
});
var batches = new ConcurrentStack<ConcurrentBag<RavenJObject>>();
subscription.BeforeBatch += () => batches.Push(new ConcurrentBag<RavenJObject>());
subscription.Subscribe(x =>
{
ConcurrentBag<RavenJObject> list;
batches.TryPeek(out list);
list.Add(x);
});
var result = SpinWait.SpinUntil(() => batches.ToList().Sum(x => x.Count) >= 200, TimeSpan.FromSeconds(10));
Assert.True(result);
Assert.True(batches.Count > 1);
}
}
示例4: RunConcurrentStackTest1_PushAndPop
// Pushes and pops a certain number of times, and validates the resulting count.
// These operations happen sequentially in a somewhat-interleaved fashion. We use
// a BCL stack on the side to validate contents are correctly maintained.
private static bool RunConcurrentStackTest1_PushAndPop(int pushes, int pops)
{
TestHarness.TestLog("* RunConcurrentStackTest1_PushAndPop(pushes={0}, pops={1})", pushes, pops);
Random r = new Random(33);
ConcurrentStack<int> s = new ConcurrentStack<int>();
Stack<int> s2 = new Stack<int>();
int donePushes = 0, donePops = 0;
while (donePushes < pushes || donePops < pops)
{
for (int i = 0; i < r.Next(1, 10); i++)
{
if (donePushes == pushes)
break;
int val = r.Next();
s.Push(val);
s2.Push(val);
donePushes++;
int sc = s.Count, s2c = s2.Count;
if (sc != s2c)
{
TestHarness.TestLog(" > test failed - stack counts differ: s = {0}, s2 = {1}", sc, s2c);
return false;
}
}
for (int i = 0; i < r.Next(1, 10); i++)
{
if (donePops == pops)
break;
if ((donePushes - donePops) <= 0)
break;
int e0, e1, e2;
bool b0 = s.TryPeek(out e0);
bool b1 = s.TryPop(out e1);
e2 = s2.Pop();
donePops++;
if (!b0 || !b1)
{
TestHarness.TestLog(" > stack was unexpectedly empty, wanted #{0} (peek={1}, pop={2})", e2, b0, b1);
return false;
}
if (e0 != e1 || e1 != e2)
{
TestHarness.TestLog(" > stack contents differ, got #{0} (peek)/{1} (pop) but expected #{2}", e0, e1, e2);
return false;
}
int sc = s.Count, s2c = s2.Count;
if (sc != s2c)
{
TestHarness.TestLog(" > test failed - stack counts differ: s = {0}, s2 = {1}", sc, s2c);
return false;
}
}
}
int expected = pushes - pops;
int endCount = s.Count;
TestHarness.TestLog(" > expected = {0}, real = {1}: passed? {2}", expected, endCount, expected == endCount);
return expected == endCount;
}
示例5: Test1_PushAndPop
// Pushes and pops a certain number of times, and validates the resulting count.
// These operations happen sequentially in a somewhat-interleaved fashion. We use
// a BCL stack on the side to validate contents are correctly maintained.
private static void Test1_PushAndPop(int pushes, int pops)
{
// It utilized a random generator to do x number of pushes and
// y number of pops where x = random, y = random. Removed it
// because it used System.Runtime.Extensions.
ConcurrentStack<int> s = new ConcurrentStack<int>();
Stack<int> s2 = new Stack<int>();
int donePushes = 0, donePops = 0;
while (donePushes < pushes || donePops < pops)
{
for (int i = 0; i < 10; i++)
{
if (donePushes == pushes)
break;
int val = i;
s.Push(val);
s2.Push(val);
donePushes++;
Assert.Equal(s.Count, s2.Count);
}
for (int i = 0; i < 6; i++)
{
if (donePops == pops)
break;
if ((donePushes - donePops) <= 0)
break;
int e0, e1, e2;
bool b0 = s.TryPeek(out e0);
bool b1 = s.TryPop(out e1);
e2 = s2.Pop();
donePops++;
Assert.True(b0);
Assert.True(b1);
Assert.Equal(e0, e1);
Assert.Equal(e1, e2);
Assert.Equal(s.Count, s2.Count);
}
}
Assert.Equal(pushes - pops, s.Count);
}
示例6: Cozy
public static void Cozy()
{
Console.WriteLine("\n-----------------------------------------------");
Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName);
Console.WriteLine("-----------------------------------------------");
//并发集合
//ConcurrentXXX这些集合都是线程安全的,实现了IProducerConsumerCollection<T>接口
//里面如下的方法
//[__DynamicallyInvokable]
//void CopyTo(T[] array, int index);
//[__DynamicallyInvokable]
//bool TryAdd(T item);
//[__DynamicallyInvokable]
//bool TryTake(out T item);
//[__DynamicallyInvokable]
//T[] ToArray();
//TryXX()方法返回一个bool值,说明操作是否成功
//ConcurrentQueue<T>
var concurrentQueue = new ConcurrentQueue<int>();
concurrentQueue.Enqueue(1);
int i;
var reslut = concurrentQueue.TryPeek(out i);
reslut = concurrentQueue.TryDequeue(out i);
//ConcurrentStack<T>
var concurrentStack = new ConcurrentStack<int>();
concurrentStack.Push(1);
reslut = concurrentStack.TryPeek(out i);
reslut = concurrentStack.TryPop(out i);
//ConcurrentBag<T>
var concurrentBag = new ConcurrentBag<int>();
concurrentBag.Add(1);
reslut = concurrentBag.TryPeek(out i);
reslut = concurrentBag.TryTake(out i);
//ConcurrentDictionary<TKey,TValue>
//该集合没实现IProducerConsumerCollection<T>,因此它的TryXX()是以非堵塞的方式访问成员
var concurrentDictionary = new ConcurrentDictionary<int, int>();
reslut = concurrentDictionary.TryAdd(1, 1);
reslut = concurrentDictionary.TryRemove(1, out i);
//BlockingCollection<T>,该集合的Add()和Taake()会阻塞线程并且一直等待
var blockingCollection = new BlockingCollection<int>();
var events = new ManualResetEventSlim[2];
var waits = new WaitHandle[2];
for (int j = 0; j < events.Length; j++)
{
events[j] = new ManualResetEventSlim(false);
waits[j] = events[j].WaitHandle;
}
new Thread(() =>
{
for (int j = 0; j < 300; j++)
{
blockingCollection.Add(j);
}
events[0].Set();
}).Start();
new Thread(() =>
{
for (int j = 0; j < 300; j++)
{
blockingCollection.Take();
}
events[1].Set();
}).Start();
if (!WaitHandle.WaitAll(waits))
{
Console.WriteLine("wait failed");
}
else
{
Console.WriteLine("reading/writing finished");
}
}
示例7: DemoConcurrentStack
private static void DemoConcurrentStack()
{
Console.WriteLine("Demo Concurrent Stack ----------------------");
var shirts = new ConcurrentStack<string>();
shirts.Push("Pluralsight");
shirts.Push("WordPress");
shirts.Push("Code School");
Console.WriteLine("After enqueuing, count = " + shirts.Count);
string item1; //= shirts.Dequeue();
bool success = shirts.TryPop(out item1);
if (success)
Console.WriteLine("\r\nRemoving " + item1);
else
Console.WriteLine("queue was empty");
string item2; //= shirts.Peek();
success = shirts.TryPeek(out item2);
if (success)
Console.WriteLine("Peeking " + item2);
else
Console.WriteLine("queue was empty");
Console.WriteLine("\r\nEnumerating:");
foreach (string item in shirts)
Console.WriteLine(item);
Console.WriteLine("\r\nAfter enumerating, count = " + shirts.Count);
}