本文整理汇总了C#中ConcurrentStack.PushRange方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.PushRange方法的具体用法?C# ConcurrentStack.PushRange怎么用?C# ConcurrentStack.PushRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentStack
的用法示例。
在下文中一共展示了ConcurrentStack.PushRange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: ExclusiveConnectionStrategy
/// <summary>
/// Initializes the strategy with the specified nodes and cluster configuration
/// </summary>
/// <param name="nodes"> The nodes. </param>
/// <param name="config"> The config. </param>
public ExclusiveConnectionStrategy(Ring nodes, CqlConnectionStringBuilder config)
{
_nodes = nodes;
_config = config;
_connections = new ConcurrentStack<Connection>();
_rndGen = new Random((int)DateTime.UtcNow.Ticks);
_connectionCount = _nodes.Sum(n => n.ConnectionCount);
if(_connectionCount > 0)
_connections.PushRange(_nodes.SelectMany(n => n).ToArray());
}
示例5: ActualizeAllArticlesAsync
public Task ActualizeAllArticlesAsync()
{
return ExecuteSafe(async () =>
{
await Initialize();
if (!await _permissionsService.CanDownload())
return;
var queue = new ConcurrentQueue<FeedModel>();
foreach (var activeSource in SourceManager.GetActiveSources())
foreach (var activeFeed in activeSource.ActiveFeeds)
queue.Enqueue(activeFeed);
var setting = await _settingsRepository.GetSettingByKeyAsync(SettingKey.ConcurrentThreads);
var threadNumber = setting is IntSettingModel ? ((IntSettingModel)setting).IntValue : 5;
var threads = new List<Task>();
if (await _permissionsService.CanDownloadFeeds())
{
_progressService.Start(ProgressType.Feed, queue.Count);
for (int i = 0; i < threadNumber; i++)
{
threads.Add(DoFeedQueue(queue));
}
await Task.WhenAll(threads.ToArray());
_progressService.Stop(ProgressType.Feed);
threads.Clear();
}
if (await _permissionsService.CanDownloadArticles())
{
var stack = new ConcurrentStack<ArticleModel>();
foreach (var activeSource in SourceManager.GetActiveSources())
foreach (var activeFeed in activeSource.ActiveFeeds)
{
var news = activeFeed.AllArticles.Where(a => a.LoadingState < LoadingState.Loaded).ToArray();
if (news.Length > 0)
stack.PushRange(news);
}
//reverse stack so
stack = new ConcurrentStack<ArticleModel>(stack.Reverse());
_progressService.Start(ProgressType.Article, stack.Count);
for (int i = 0; i < threadNumber; i++)
{
threads.Add(DoArticleStack(stack));
}
await Task.WhenAll(threads.ToArray());
_progressService.Stop(ProgressType.Article);
threads.Clear();
}
});
}
示例6: 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);
}
示例7: 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);
}
示例8: 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();
}
示例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
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();
}
示例11: EnumerateTypeTrees
public static IEnumerable<Type> EnumerateTypeTrees(this IEnumerable<Type> rootTypes)
{
if(rootTypes == null) yield break;
var alreadyChecked = new HashSet<Type>();
var stack = new ConcurrentStack<Type>(rootTypes);
Type currentType;
while (stack.TryPop(out currentType))
{
if (alreadyChecked.Contains(currentType)) continue;
alreadyChecked.Add(currentType);
yield return currentType;
var extractedTypes = currentType.ExtractTypes();
if (extractedTypes.Length > 0)
{
stack.PushRange(extractedTypes);
}
}
}
示例12: 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();
}
示例13: PushRangeTestCase
public void PushRangeTestCase()
{
var testStack = new ConcurrentStack<int>();
var testData = new int[] { 1, 2, 3, 4, 5 };
testStack.PushRange (testData);
Assert.AreEqual (testData.Length, testStack.Count);
}
示例14: WriteFloodAndBatchTogether
bool WriteFloodAndBatchTogether(CommandProcessorContext context, int timeOut, int batchSize,
int batchThreadCount, int floodThreadCount)
{
string streamId = Guid.NewGuid().ToString();
const string batchMsg = "BasicVerify-Batch-Test-Message";
const string floodMsg = "BasicVerify-Flood-Test-Message";
int batchCount = 0;
int floodCount = 0;
DateTime dt = DateTime.MaxValue;
var errors = new ConcurrentStack<string>();
var threads = new List<Task>();
for (int t = 0; t < batchThreadCount; t++)
{
var task = Task.Factory.StartNew(() =>
{
while (DateTime.Now < dt)
{
try
{
context.Client.EventStores.WriteEventsInLargeBatch(streamId,
Enumerable.Range(0, batchSize).Select(
x =>
{
var bytes = Encoding.UTF8.GetBytes(batchMsg);
return (bytes);
}));
Interlocked.Add(ref batchCount, 1);
}
catch (Exception ex)
{
errors.Push(ex.Message);
}
}
}, TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness);
threads.Add(task);
}
for (int t = 0; t < floodThreadCount; t++)
{
var task = Task.Factory.StartNew(() =>
{
while (DateTime.Now < dt)
{
try
{
context.Client.EventStores.WriteEvent(streamId, Encoding.UTF8.GetBytes(floodMsg));
Interlocked.Add(ref floodCount, 1);
}
catch (Exception ex)
{
errors.Push(ex.Message);
}
}
}, TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness);
threads.Add(task);
}
dt = DateTime.Now.AddSeconds(timeOut);
Task.WaitAll(threads.ToArray());
context.Log.Info("Add {0} flood messages", floodCount);
context.Log.Info("Add {0} batch", batchCount);
var readErrors = ReadAddMessages(context, streamId, batchMsg, floodMsg, batchCount * batchSize, floodCount).ToArray();
if (readErrors.Any())
errors.PushRange(readErrors);
foreach (var err in errors.ToArray())
context.Log.Error(err);
return errors.Count == 0;
}
示例15: RunConcurrentStackTest6_PushRange
//Tests COncurrentSTack.PushRange
private static bool RunConcurrentStackTest6_PushRange(int NumOfThreads, int localArraySize)
{
TestHarness.TestLog("* RunConcurrentStackTest6_PushRange({0},{1})", NumOfThreads, localArraySize);
ConcurrentStack<int> stack = new ConcurrentStack<int>();
Thread[] threads = new Thread[NumOfThreads];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread((obj) =>
{
int index = (int)obj;
int[] array = new int[localArraySize];
for (int j = 0; j < localArraySize; j++)
{
array[j] = index + j;
}
stack.PushRange(array);
});
threads[i].Start(i * localArraySize);
}
for (int i = 0; i < threads.Length; i++)
{
threads[i].Join();
}
//validation
for (int i = 0; i < threads.Length; i++)
{
int lastItem = -1;
for (int j = 0; j < localArraySize; j++)
{
int currentItem = 0;
if (!stack.TryPop(out currentItem))
{
TestHarness.TestLog(" > Failed, TryPop returned false");
return false;
}
if (lastItem > -1 && lastItem - currentItem != 1)
{
TestHarness.TestLog(" > Failed {0} - {1} shouldn't be consecutive", lastItem, currentItem);
}
lastItem = currentItem;
}
}
return true;
}