本文整理汇总了C#中ConcurrentStack.Push方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.Push方法的具体用法?C# ConcurrentStack.Push怎么用?C# ConcurrentStack.Push使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentStack
的用法示例。
在下文中一共展示了ConcurrentStack.Push方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: TestBasicScenarios
public static void TestBasicScenarios()
{
ConcurrentStack<int> cs = new ConcurrentStack<int>();
cs.Push(1);
Task[] tks = new Task[2];
tks[0] = Task.Run(() =>
{
cs.Push(2);
cs.Push(3);
cs.Push(4);
});
tks[1] = Task.Run(() =>
{
int item1, item2;
var ret1 = cs.TryPop(out item1);
// at least one item
Assert.True(ret1);
var ret2 = cs.TryPop(out item2);
// two item
if (ret2)
{
Assert.True(item1 > item2, String.Format("{0} should greater than {1}", item1, item2));
}
else // one item
{
Assert.Equal(1, item1);
}
});
Task.WaitAll(tks);
}
示例3: Setup
public void Setup()
{
var pages = new ConcurrentStack<IGooglePageModel>();
Parallel.Invoke(() => pages.Push(new GooglePageModel<InternetExplorerGrid>()),
() => pages.Push(new GooglePageModel<FirefoxGrid>()));
var parallelPage = new ParallelPageModel<IGooglePageModel>(pages.ToArray());
_page = parallelPage.Cast();
_page.Search("SQL For .NET Programmers");
}
示例4: Setup
public void Setup()
{
stack = new ConcurrentStack<int>();
for (int i = 0; i < 10; i++) {
stack.Push(i);
}
}
示例5: 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();
}
示例6: PushTryPop
public void PushTryPop(int producerThreads, int consumerThreads)
{
var stack = new ConcurrentStack<int>();
var startEvent = new ManualResetEventSlim(false);
var finished = 0;
var stop = false;
var producerTasks = Enumerable.Range(0, producerThreads).Select(i => Task.Factory.StartNew(() =>
{
var count = iterations/producerThreads;
startEvent.Wait();
for (var j = 0; j < count; j++)
stack.Push(0);
Interlocked.Increment(ref finished);
if (finished >= producerThreads) stop = true;
}, TaskCreationOptions.LongRunning)).ToArray();
var consumerTasks = Enumerable.Range(0, consumerThreads).Select(i => Task.Factory.StartNew(() =>
{
int num;
startEvent.Wait();
while (!stop) stack.TryPop(out num);
}, TaskCreationOptions.LongRunning)).ToArray();
var stopwatch = Stopwatch.StartNew();
startEvent.Set();
stop = true;
Task.WaitAll(producerTasks);
Task.WaitAll(consumerTasks);
stopwatch.StopAndLog(iterations);
}
示例7: 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();
}
示例8: generatePaths
//simulate sim;
//simulateHist simHist;
public ConcurrentBag<double> generatePaths(double initialPrice, int numberOfPaths, double timeToExpiry)
{
ConcurrentBag<double> toReturn = new ConcurrentBag<double>{};
//var indices = Enumerable.Range(0, numberOfPaths);
var rnd = new Random(42);
ConcurrentStack<int> seeds = new ConcurrentStack<int> {};
for (int i = 0; i < numberOfPaths; ++i)
seeds.Push(rnd.Next(1, numberOfPaths - 1));
int steps = Convert.ToInt32 (Math.Floor(timeToExpiry / bm.deltaT));
Parallel.ForEach(seeds,
//new ParallelOptions { MaxDegreeOfParallelism = 2 },
seed =>
{
Thread.Sleep(1);
simulate mySim = new simulate(simulator.simulate);
double res = new double();
res = mySim(steps, initialPrice, seed, bm);
toReturn.Add(res);
}
);
return toReturn;
}
示例9: Method_Scenario_Result
public void Method_Scenario_Result()
{
try
{
var stack = new ConcurrentStack<int>();
// stack.PushRange(new[] { 1, 2, 3 });
stack.Push(1);
stack.Push(2);
stack.Push(3);
var values = new int[3];
var run = Task.Run(() =>
{
int counter = stack.Count;
for (int i = 0; i < counter; i++)
{
int popped;
var pop = stack.TryPop(out popped);
Thread.Sleep(1000);
Console.WriteLine(popped);
}
});
var task = Task.Run(() =>
{
stack.Push(4);
int popped2;
stack.TryPop(out popped2);
stack.TryPop(out popped2);
stack.TryPop(out popped2);
stack.TryPop(out popped2);
});
Task.WaitAll(run,task);
}
catch (AggregateException ex)
{
foreach (var innerException in ex.InnerExceptions)
{
Console.WriteLine(innerException.Message);
}
}
}
示例10: BufferManager
/// <summary>
///
/// </summary>
/// <param name="bufferSize"></param>
public BufferManager(int bufferSize, int chunkCount)
{
m_bufferSize = bufferSize;
m_freeOffset = new ConcurrentStack<int>();
m_bufferBlock = new byte[bufferSize * chunkCount];
for (int i = 0; i < chunkCount; i++)
m_freeOffset.Push(bufferSize * i);
}
示例11: SocketAsyncEventArgsPool
public SocketAsyncEventArgsPool(int count)
{
_pool = new ConcurrentStack<SocketAsyncEventArgs>();
for (var i = 0; i < count; i++)
{
_pool.Push(new SocketAsyncEventArgs());
}
}
示例12: TaskProducer
static async Task TaskProducer(ConcurrentStack<CustomTask> stack)
{
for (int i = 1; i <= 20; i++)
{
await Task.Delay(50);
var workItem = new CustomTask { Id = i };
stack.Push(workItem);
Console.WriteLine("Task {0} has been posted", workItem.Id);
}
}
示例13: PinnedMemoryBufferPool
public PinnedMemoryBufferPool(int totalBytes, int bufferBlockSize)
{
totalBufferSize = totalBytes;
bufferPool = new byte[totalBufferSize];
this.bufferBlockSize = bufferBlockSize;
bufferOffsetPool = new ConcurrentStack<int>();
int offset = 0;
while (offset < (totalBufferSize - bufferBlockSize))
{
bufferOffsetPool.Push(offset);
offset += bufferBlockSize;
}
}
示例14: SocketArgsPool
public SocketArgsPool(string name, int initialCount, Func<SocketAsyncEventArgs> socketArgsCreator)
{
if (socketArgsCreator == null)
throw new ArgumentNullException("socketArgsCreator");
if (initialCount < 0)
throw new ArgumentOutOfRangeException("initialCount");
Name = name;
_socketArgsPool = new ConcurrentStack<SocketAsyncEventArgs>();
_socketArgsCreator = socketArgsCreator;
for (var i = 0; i < initialCount; ++i)
_socketArgsPool.Push(socketArgsCreator());
}
示例15: 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);
}