本文整理汇总了C#中ConcurrentStack.TryPopRange方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.TryPopRange方法的具体用法?C# ConcurrentStack.TryPopRange怎么用?C# ConcurrentStack.TryPopRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentStack
的用法示例。
在下文中一共展示了ConcurrentStack.TryPopRange方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
//Stack - Pilha (LIFO)
ConcurrentStack<int> stack = new ConcurrentStack<int>();
//Adiciona um item na pilha
stack.Push(42);
int result;
//metodo trypop retorna o ultimo item a ser adicionado na lista, caso não tenha mais item ele não dar por que ele "tenta(try)" pega um item
//quando usar o metodo trypop o item é removido da coleção
if (stack.TryPop(out result))
{
Console.WriteLine("Popped: {0}", result);
}
if (stack.TryPop(out result))
{
Console.WriteLine("Popped: {0}", result);
}
stack.PushRange(new int[] { 1, 2, 3 });
int[] values = new int[2];
//metod retorna uma coleção de itens da pilha
stack.TryPopRange(values);
foreach (var item in values)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
示例2: Main
static void Main(string[] args)
{
ConcurrentStack<int> stack = new ConcurrentStack<int>();
stack.Push(42);
int result;
if (stack.TryPop(out result))
{
Console.WriteLine(result);
}
stack.PushRange(new int[] { 1, 2, 3 });
int[] values = new int[2];
stack.TryPopRange(values);
foreach (var i in values)
{
Console.WriteLine(i);
}
Console.Write("Press a key to exit");
Console.ReadKey();
}
示例3: ConcurrentStackExampleRange
public int ConcurrentStackExampleRange ()
{
var stack = new ConcurrentStack<int> ();
stack.PushRange (new int[] { 1, 2, 3 });
return stack.TryPopRange (new int[2]);
}
示例4: 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);
}
示例5: Run
public void Run()
{
ConcurrentStack<int> stack = new ConcurrentStack<int>();
stack.Push(42);
int result;
if (stack.TryPop(out result))
Console.WriteLine("Popped: {0}", result);
stack.PushRange(new int[] { 1, 2, 3 });
int[] values = new int[2];
stack.TryPopRange(values);
foreach (int i in values)
Console.WriteLine(i);
}
示例6: RunConcurrentStack
private void RunConcurrentStack()
{
Console.WriteLine("Stack Run implementation along with Range implementations");
ConcurrentStack<int> stack = new ConcurrentStack<int>();
stack.Push(10);
int result;
if (stack.TryPop(out result))
Console.WriteLine(result);
stack.PushRange(new int[] { 1, 2, 3 ,4});
int[] values = new int[4];
Console.WriteLine(stack.TryPopRange(values));
foreach (int num in values) Console.WriteLine(num);
}
示例7: Main
private static void Main()
{
var stack = new ConcurrentStack<int>();
stack.Push(42);
int result;
if (stack.TryPop(out result)) {
Console.WriteLine(result);
}
stack.PushRange(new[] {1, 2, 3});
var values = new int[2];
stack.TryPopRange(values);
foreach (var i in values) {
Console.WriteLine(i);
}
Console.ReadLine();
}
示例8: Main
static void Main(string[] args)
{
ConcurrentStack<int> stack = new ConcurrentStack<int>();
stack.Push(67);
int result;
if (stack.TryPop(out result))
Console.WriteLine("Popped: {0}", result);
stack.PushRange(new int[] { 1, 2, 3 });
int[] values = new int[2];
stack.TryPopRange(values);
foreach (int i in values)
Console.WriteLine(i);
Console.WriteLine("\n\n----------------------------------------------------------------------\n\n");
Console.ReadLine();
var dict = new ConcurrentDictionary<string, int>();
if (dict.TryAdd("ERHAN",26))
{
Console.WriteLine("Added");
}
if (dict.TryUpdate("ERHAN", 30, 26))
{
Console.WriteLine("26 updated to 30");
}
dict["ERHAN"] = 32; // Overwrite unconditionally
Console.WriteLine("----------------------------------------------------------------------");
Console.ReadLine();
int r = dict.AddOrUpdate("ERHAN", 35, (S, I) => I * 2);
Console.WriteLine(r);
int r2 = dict.GetOrAdd("ERHAN", 37);
Console.WriteLine("----------------------------------------------------------------------");
Console.ReadLine();
}
示例9: Main
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
ConcurrentStack<int> stack = new ConcurrentStack<int>();
stack.Push(42);
int result;
if (stack.TryPop(out result))
Console.WriteLine("Popped: {0}", result);
stack.PushRange(new int[] { 1, 2, 3 });
int[] values = new int[2];
stack.TryPopRange(values);
foreach (int i in values)
Console.WriteLine(i);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
示例10: Main
public static void Main(string[] args)
{
ConcurrentStack<int> stack = new ConcurrentStack<int>();
stack.Push(42);
int result;
if (stack.TryPop(out result))
{
Console.WriteLine("Popped: {0}", result);
}
stack.PushRange(new int[] { 1, 2, 3 });
int[] values = new int[2];
stack.TryPopRange(values);
foreach (var i in values)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
示例11: TryPopRangeEmpty
public void TryPopRangeEmpty ()
{
stack = new ConcurrentStack<int>();
Assert.AreEqual (0, stack.TryPopRange (new int [1]));
}
示例12: RunConcurrentStackTest7_PopRange
//Tests ConcurrentStack.PopRange by pushing consecutove numbers and run n threads each thread tries to pop m itmes
// the popped m items should be consecutive
private static bool RunConcurrentStackTest7_PopRange(int NumOfThreads, int elementsPerThread)
{
TestHarness.TestLog("* RunConcurrentStackTest7_PopRange({0},{1})", NumOfThreads, elementsPerThread);
ConcurrentStack<int> stack = new ConcurrentStack<int>(Enumerable.Range(1, NumOfThreads * elementsPerThread));
Thread[] threads = new Thread[NumOfThreads];
int[] array = new int[threads.Length * elementsPerThread];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread((obj) =>
{
int index = (int)obj;
int res;
if ((res = stack.TryPopRange(array, index, elementsPerThread)) != elementsPerThread)
{
TestHarness.TestLog(" > Failed TryPopRange didn't return the full range ");
}
});
threads[i].Start(i * elementsPerThread);
}
for (int i = 0; i < threads.Length; i++)
{
threads[i].Join();
}
// validation
for (int i = 0; i < NumOfThreads; i++)
{
for (int j = 1; j < elementsPerThread; j++)
{
int currentIndex = i * elementsPerThread + j;
if (array[currentIndex - 1] - array[currentIndex] != 1)
{
TestHarness.TestLog(" > Failed {0} - {1} shouldn't be consecutive", array[currentIndex - 1], array[currentIndex]);
return false;
}
}
}
return true;
}
示例13: Test8_Exceptions
public static void Test8_Exceptions()
{
ConcurrentStack<int> stack = null;
Assert.Throws<ArgumentNullException>(
() => stack = new ConcurrentStack<int>((IEnumerable<int>)null));
// "Test8_Exceptions: The constructor didn't throw ANE when null collection passed");
stack = new ConcurrentStack<int>();
//CopyTo
Assert.Throws<ArgumentNullException>(
() => stack.CopyTo(null, 0));
// "Test8_Exceptions: CopyTo didn't throw ANE when null array passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.CopyTo(new int[1], -1));
// "Test8_Exceptions: CopyTo didn't throw AORE when negative array index passed");
//PushRange
Assert.Throws<ArgumentNullException>(
() => stack.PushRange(null));
// "Test8_Exceptions: PushRange didn't throw ANE when null array passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.PushRange(new int[1], 0, -1));
// "Test8_Exceptions: PushRange didn't throw AORE when negative count passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.PushRange(new int[1], -1, 1));
// "Test8_Exceptions: PushRange didn't throw AORE when negative index passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.PushRange(new int[1], 2, 1));
// "Test8_Exceptions: PushRange didn't throw AORE when start index > array length");
Assert.Throws<ArgumentException>(
() => stack.PushRange(new int[1], 0, 10));
// "Test8_Exceptions: PushRange didn't throw AE when count + index > array length");
//PopRange
Assert.Throws<ArgumentNullException>(
() => stack.TryPopRange(null));
// "Test8_Exceptions: TryPopRange didn't throw ANE when null array passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.TryPopRange(new int[1], 0, -1));
// "Test8_Exceptions: TryPopRange didn't throw AORE when negative count passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.TryPopRange(new int[1], -1, 1));
// "Test8_Exceptions: TryPopRange didn't throw AORE when negative index passed");
Assert.Throws<ArgumentOutOfRangeException>(
() => stack.TryPopRange(new int[1], 2, 1));
// "Test8_Exceptions: TryPopRange didn't throw AORE when start index > array length");
Assert.Throws<ArgumentException>(
() => stack.TryPopRange(new int[1], 0, 10));
// "Test8_Exceptions: TryPopRange didn't throw AE when count + index > array length");
}
示例14: Test7_PopRange
//Tests ConcurrentStack.PopRange by pushing consecutive numbers and run n threads each thread tries to pop m itmes
// the popped m items should be consecutive
private static void Test7_PopRange(int NumOfThreads, int elementsPerThread)
{
int lastValue = NumOfThreads * elementsPerThread;
List<int> allValues = new List<int>();
for (int i = 1; i <= lastValue; i++)
allValues.Add(i);
ConcurrentStack<int> stack = new ConcurrentStack<int>(allValues);
Task[] threads = new Task[NumOfThreads];
int[] array = new int[threads.Length * elementsPerThread];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = Task.Factory.StartNew((obj) =>
{
int index = (int)obj;
int res = stack.TryPopRange(array, index, elementsPerThread);
Assert.Equal(elementsPerThread, res);
}, i * elementsPerThread, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}
Task.WaitAll(threads);
// validation
for (int i = 0; i < NumOfThreads; i++)
{
for (int j = 1; j < elementsPerThread; j++)
{
int currentIndex = i * elementsPerThread + j;
Assert.Equal(array[currentIndex - 1], array[currentIndex] + 1);
}
}
}